Skip to content

Commit 1aaf5bf

Browse files
New Commit
1 parent 2243e90 commit 1aaf5bf

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

Kruskal's_algo.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define MAX_EDGES 1000
5+
6+
typedef struct Edge {
7+
int src, dest, weight;
8+
} Edge;
9+
10+
typedef struct Graph {
11+
int V, E;
12+
Edge edges[MAX_EDGES];
13+
} Graph;
14+
15+
typedef struct Subset {
16+
int parent, rank;
17+
} Subset;
18+
19+
Graph* createGraph(int V, int E) {
20+
Graph* graph = (Graph*) malloc(sizeof(Graph));
21+
graph->V = V;
22+
graph->E = E;
23+
return graph;
24+
}
25+
26+
int find(Subset subsets[], int i) {
27+
if (subsets[i].parent != i) {
28+
subsets[i].parent = find(subsets, subsets[i].parent);
29+
}
30+
return subsets[i].parent;
31+
}
32+
33+
void Union(Subset subsets[], int x, int y) {
34+
int xroot = find(subsets, x);
35+
int yroot = find(subsets, y);
36+
37+
if (subsets[xroot].rank < subsets[yroot].rank) {
38+
subsets[xroot].parent = yroot;
39+
} else if (subsets[xroot].rank > subsets[yroot].rank) {
40+
subsets[yroot].parent = xroot;
41+
} else {
42+
subsets[yroot].parent = xroot;
43+
subsets[xroot].rank++;
44+
}
45+
}
46+
47+
int compare(const void* a, const void* b) {
48+
Edge* a_edge = (Edge*) a;
49+
Edge* b_edge = (Edge*) b;
50+
return a_edge->weight - b_edge->weight;
51+
}
52+
53+
void kruskalMST(Graph* graph) {
54+
Edge mst[graph->V];
55+
int e = 0, i = 0;
56+
57+
qsort(graph->edges, graph->E, sizeof(Edge), compare);
58+
59+
Subset* subsets = (Subset*) malloc(graph->V * sizeof(Subset));
60+
for (int v = 0; v < graph->V; ++v) {
61+
subsets[v].parent = v;
62+
subsets[v].rank = 0;
63+
}
64+
65+
while (e < graph->V - 1 && i < graph->E) {
66+
Edge next_edge = graph->edges[i++];
67+
68+
int x = find(subsets, next_edge.src);
69+
int y = find(subsets, next_edge.dest);
70+
71+
if (x != y) {
72+
mst[e++] = next_edge;
73+
Union(subsets, x, y);
74+
}
75+
}
76+
77+
printf("Minimum Spanning Tree:\n");
78+
for (i = 0; i < e; ++i) {
79+
printf("(%d, %d) -> %d\n", mst[i].src, mst[i].dest, mst[i].weight);
80+
}
81+
}
82+
83+
int main() {
84+
int V, E;
85+
printf("Enter number of vertices and edges: ");
86+
scanf("%d %d", &V, &E);
87+
88+
Graph* graph = createGraph(V, E);
89+
90+
printf("Enter edges and their weights:\n");
91+
for (int i = 0; i < E; ++i) {
92+
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph->edges[i].weight);
93+
}
94+
95+
kruskalMST(graph);
96+
97+
return 0;
98+
}

Kruskal's_algo.exe

42.2 KB
Binary file not shown.

Prim's_algo.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include<stdio.h>
2+
#include<stdbool.h>
3+
4+
#define INF 9999999
5+
#define V 5
6+
7+
//The adjacency matrix
8+
/*int G[V][V] = {
9+
{0, 9, 75, 0, 0},
10+
{9, 0, 95, 19, 42},
11+
{75, 95, 0, 51, 66},
12+
{0, 19, 51, 0, 31},
13+
{0, 42, 66, 31, 0}};
14+
*/
15+
16+
void addEdge(int u, int v, int adjMatrix[V][V]){
17+
int w;
18+
printf("Enter weight of edge between %d and %d : ", u, v);
19+
scanf("%d", &w);
20+
adjMatrix[u][v] = w;
21+
adjMatrix[v][u] = w;
22+
}
23+
24+
void displayMatrix(int nodes, int adjMatrix[V][V]){
25+
int i, j;
26+
printf("Adjacency Matrix :\n");
27+
for(i=0; i<V; i++){
28+
for(j=0; j<V; j++)
29+
printf("%d\t",adjMatrix[i][j]);
30+
printf("\n");
31+
}
32+
}
33+
int main(){
34+
int i, j, u, v;
35+
int sum = 0;
36+
static int G[V][V];
37+
38+
int no_edge;// number of edges
39+
int selected[V];
40+
41+
for(i=0; i<V; i++){
42+
for(j=0; j<V; j++){
43+
addEdge(i, j, G);
44+
}
45+
}
46+
47+
displayMatrix(V, G);
48+
49+
memset(selected, false, sizeof(selected));
50+
51+
no_edge = 0;
52+
selected[0] = true;
53+
54+
int x; //row no
55+
int y; //col no
56+
57+
printf("Edge : Weight\n");
58+
59+
while(no_edge < V-1){
60+
int min = INF;
61+
x = 0;
62+
y = 0;
63+
64+
for(i=0; i<V; i++){
65+
if(selected[i]){
66+
for(j=0; j<V;j++){
67+
if(!selected[j] && G[i][j]){
68+
if(min > G[i][j]){
69+
min = G[i][j];
70+
x = i;
71+
y = j;
72+
}
73+
}
74+
}
75+
}
76+
}
77+
printf("%d - %d : %d\n", x, y, G[x][y]);
78+
sum += G[x][y];
79+
selected[y] = true;
80+
no_edge++;
81+
}
82+
printf("\nTotal Weight = %d\n", sum);
83+
return 0;
84+
}

Prim's_algo.exe

41.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)