Skip to content

Commit 2ddb76c

Browse files
kruskal-mst-algo
1 parent c82934f commit 2ddb76c

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Program to calculate Minimum Spanning Tree using Kruskal's algorithm in C++.
2+
// Undirected graph is taken in consideration
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
struct Edge
11+
{
12+
int src, dest, weight;
13+
};
14+
15+
struct Graph
16+
{
17+
// V = Number of vertices, E = Number of edges
18+
int V, E;
19+
struct Edge *edge;
20+
};
21+
22+
struct Graph *createGraph(int V, int E)
23+
{
24+
struct Graph *g = (struct Graph *)malloc(sizeof(struct Graph));
25+
g->V = V;
26+
g->E = E;
27+
g->edge = (struct Edge *)malloc(g->E * sizeof(struct Edge));
28+
29+
return g;
30+
}
31+
32+
struct subset
33+
{
34+
int parent;
35+
int rank;
36+
};
37+
38+
int find(struct subset subsets[], int i)
39+
{
40+
if (subsets[i].parent != i)
41+
subsets[i].parent = find(subsets, subsets[i].parent);
42+
43+
return subsets[i].parent;
44+
}
45+
46+
void Union(struct subset subsets[], int x, int y)
47+
{
48+
int xroot = find(subsets, x);
49+
int yroot = find(subsets, y);
50+
51+
if (subsets[xroot].rank < subsets[yroot].rank)
52+
subsets[xroot].parent = yroot;
53+
else if (subsets[xroot].rank > subsets[yroot].rank)
54+
subsets[yroot].parent = xroot;
55+
else
56+
{
57+
subsets[yroot].parent = xroot;
58+
subsets[xroot].rank++;
59+
}
60+
}
61+
62+
int comp(const void *a, const void *b)
63+
{
64+
struct Edge *a1 = (struct Edge *)a;
65+
struct Edge *b1 = (struct Edge *)b;
66+
return a1->weight > b1->weight;
67+
}
68+
69+
// The function to construct MST
70+
void MST(struct Graph *g)
71+
{
72+
int V = g->V;
73+
struct Edge result[V];
74+
int e = 0;
75+
int i = 0;
76+
77+
qsort(g->edge, g->E, sizeof(g->edge[0]), comp);
78+
79+
struct subset *subsets = (struct subset *)malloc(V * sizeof(struct subset));
80+
81+
for (int v = 0; v < V; ++v)
82+
{
83+
subsets[v].parent = v;
84+
subsets[v].rank = 0;
85+
}
86+
87+
// Number of edges = V-1
88+
while (e < V - 1)
89+
{
90+
struct Edge next_edge = g->edge[i++];
91+
92+
int x = find(subsets, next_edge.src);
93+
int y = find(subsets, next_edge.dest);
94+
95+
if (x != y)
96+
{
97+
result[e++] = next_edge;
98+
Union(subsets, x, y);
99+
}
100+
}
101+
cout << "Edges in the constructed MST\n";
102+
for (i = 0; i < e; ++i)
103+
printf("%d -- %d == %d\n", result[i].src, result[i].dest,
104+
result[i].weight);
105+
return;
106+
}
107+
108+
// Driver program
109+
int main()
110+
{
111+
/* This graph is taken for consideration.
112+
22
113+
0--------1
114+
| \ |
115+
16| \5 |15
116+
| \ |
117+
2--------3
118+
4 */
119+
int V = 4; // Number of vertices in graph
120+
int E = 5; // Number of edges in graph
121+
struct Graph *g = createGraph(V, E);
122+
123+
// add edge 0-1
124+
g->edge[0].src = 0;
125+
g->edge[0].dest = 1;
126+
g->edge[0].weight = 22;
127+
128+
// add edge 0-2
129+
g->edge[1].src = 0;
130+
g->edge[1].dest = 2;
131+
g->edge[1].weight = 16;
132+
133+
// add edge 0-3
134+
g->edge[2].src = 0;
135+
g->edge[2].dest = 3;
136+
g->edge[2].weight = 5;
137+
138+
// add edge 1-3
139+
g->edge[3].src = 1;
140+
g->edge[3].dest = 3;
141+
g->edge[3].weight = 15;
142+
143+
// add edge 2-3
144+
g->edge[4].src = 2;
145+
g->edge[4].dest = 3;
146+
g->edge[4].weight = 4;
147+
148+
MST(g);
149+
return 0;
150+
}

0 commit comments

Comments
 (0)