Kappa
Kappa
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
Insertion Sort :
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
Merge Sort :
#include <stdio.h>
#include <limits.h>
void merge(int arr[], int temp[], int left, int mid, int right, int *min, int *max) {
int i = left, j = mid + 1, k = left;
void mergeSort(int arr[], int temp[], int left, int right, int *min, int *max) {
if (left < right) {
int mid = left + (right - left) / 2;
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
int temp[n];
return 0;
}
Quick Sort :
#include <stdio.h>
#include <limits.h>
void quickSort(int arr[], int low, int high, int *min, int *max) {
if (low < high) {
int pi = partition(arr, low, high);
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
#include <stdio.h>
#include <limits.h>
#define V 9
int main() {
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 0, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 0},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 0, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 0, 0, 0, 0, 6, 7, 0}
};
int source = 0;
dijkstra(graph, source);
return 0;
}
Prims Algo :
#include <stdio.h>
#include <limits.h>
#define V 5
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++) {
printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);
}
}
int main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph);
return 0;
}
Kruskals Algo :
#include <stdio.h>
#include <stdlib.h>
#define V 5
#define E 7
typedef struct {
int u, v, weight;
} Edge;
int parent[V];
int rank[V];
int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
if (rootX != rootY) {
if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
int mstWeight = 0;
printf("Edge \tWeight\n");
for (int i = 0; i < edgeCount; i++) {
int u = edges[i].u;
int v = edges[i].v;
int weight = edges[i].weight;
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV) {
printf("%d - %d \t%d\n", u, v, weight);
mstWeight += weight;
union_sets(rootU, rootV);
}
}
printf("Total weight of MST: %d\n", mstWeight);
}
int main() {
Edge edges[E] = {
{0, 1, 2}, {0, 3, 6}, {1, 2, 3}, {1, 3, 8},
{1, 4, 5}, {2, 4, 7}, {3, 4, 9}
};
kruskalMST(edges, E);
return 0;
}
Bellman Ford :
#include <stdio.h>
#include <limits.h>
typedef struct {
int u, v, weight;
} Edge;
int main() {
Edge edges[E] = {
{0, 1, -1}, {0, 2, 4}, {1, 2, 3}, {1, 3, 2},
{1, 4, 2}, {3, 2, 5}, {3, 4, -3}, {4, 3, 3}
};
int source = 0;
bellmanFord(edges, source);
return 0;
}
N-Queen :
#include <stdio.h>
#include <stdbool.h>
#define N 4
int board[N][N];
void printBoard() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int main() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = 0;
}
}
if (solveNQueen(0)) {
printBoard();
} else {
printf("Solution does not exist\n");
}
return 0;
}
Graph Coloring :
#include <stdio.h>
#include <stdbool.h>
#define V 4
bool isSafe(int graph[V][V], int color[], int v, int c) {
for (int i = 0; i < V; i++) {
if (graph[v][i] && color[i] == c) {
return false;
}
}
return true;
}
int main() {
int graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
};
int m = 3;
int color[V] = {0};
if (graphColoring(graph, m, color, 0)) {
printSolution(color);
} else {
printf("Solution does not exist\n");
}
return 0;
}
int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABAB";
naiveStringMatch(text, pattern);
return 0;
}
struct Item {
int value;
int weight;
float ratio;
};
return totalValue;
}
int main() {
struct Item items[] = {{60, 10, 0}, {100, 20, 0}, {120, 30, 0}};
int n = sizeof(items) / sizeof(items[0]);
int W = 50;
return 0;
}
Floyd Warshall :
#include <stdio.h>
#include <limits.h>
#define V 4
void printSolution(int dist[V][V]) {
printf("The shortest distances between every pair of vertices are:\n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INT_MAX) {
printf("INF ");
} else {
printf("%d ", dist[i][j]);
}
}
printf("\n");
}
}
printSolution(dist);
}
int main() {
int graph[V][V] = {
{0, 3, 0, 0},
{3, 0, 1, 0},
{0, 1, 0, 7},
{0, 0, 7, 0}
};
floydWarshall(graph);
return 0;
}
15 Puzzle Problem :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 4
#define MAX_QUEUE_SIZE 1000
typedef struct {
int state[N][N];
int cost; int heuristic; // Heuristic cost (Manhattan distance)
int emptyX, emptyY; // Position of the empty space
} Node;
typedef struct {
Node nodes[MAX_QUEUE_SIZE];
int front, rear;
} Queue;
Queue q;
void initQueue() {
q.front = q.rear = -1;
}
int isQueueEmpty() {
return q.front == -1;
}
int isQueueFull() {
return q.rear == MAX_QUEUE_SIZE - 1;
}
void swap(Node *node, int x1, int y1, int x2, int y2) {
int temp = node->state[x1][y1];
node->state[x1][y1] = node->state[x2][y2];
node->state[x2][y2] = temp;
}
while (!isQueueEmpty()) {
Node currentNode = dequeue();
if (isGoalState(currentNode)) {
printf("Goal state reached:\n");
printState(currentNode);
return;
}
expandNode(¤tNode);
}
int main() {
Node startNode = {
.state = {
{1, 2, 3, 4},
{5, 6, 0, 8},
{9, 10, 7, 11},
{13, 14, 15, 12}
},
.cost = 0,
.heuristic = 0,
.emptyX = 1,
.emptyY = 2
};
startNode.heuristic = calculateHeuristic(startNode);
printf("Initial state:\n");
printState(startNode);
branchAndBound(startNode);
return 0;
}