Skip to content

Commit 0b0c33f

Browse files
committed
Added Greedy Kruskal’s Minimum Spanning Tree.
1 parent 0843ded commit 0b0c33f

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <bits/stdc++.h>
2+
// a structure to represent a weighted edge in graph
3+
struct Edge
4+
{
5+
int src, dest, weight;
6+
};
7+
8+
// a structure to represent a connected, undirected
9+
// and weighted graph
10+
struct Graph
11+
{
12+
// V-> Number of vertices, E-> Number of edges
13+
int V, E;
14+
struct Edge* edge;
15+
};
16+
17+
// Creates a graph with V vertices and E edges
18+
struct Graph* createGraph(int V, int E)
19+
{
20+
struct Graph* graph = new Graph;
21+
graph->V = V;
22+
graph->E = E;
23+
24+
graph->edge = new Edge[E];
25+
26+
return graph;
27+
}
28+
29+
// A structure to represent a subset for union-find
30+
struct subset
31+
{
32+
int parent;
33+
int rank;
34+
};
35+
36+
int find(struct subset subsets[], int i)
37+
{
38+
39+
if (subsets[i].parent != i)
40+
subsets[i].parent = find(subsets, subsets[i].parent);
41+
42+
return subsets[i].parent;
43+
}
44+
45+
void Union(struct subset subsets[], int x, int y)
46+
{
47+
int xroot = find(subsets, x);
48+
int yroot = find(subsets, y);
49+
50+
if (subsets[xroot].rank < subsets[yroot].rank)
51+
subsets[xroot].parent = yroot;
52+
else if (subsets[xroot].rank > subsets[yroot].rank)
53+
subsets[yroot].parent = xroot;
54+
55+
else
56+
{
57+
subsets[yroot].parent = xroot;
58+
subsets[xroot].rank++;
59+
}
60+
}
61+
62+
int myComp(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 main function to construct MST using Kruskal's algorithm
70+
void KruskalMST(struct Graph* graph)
71+
{
72+
int V = graph->V;
73+
struct Edge result[V];
74+
int e = 0;
75+
int i = 0;
76+
77+
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp);
78+
79+
// Allocate memory for creating V ssubsets
80+
struct subset *subsets =
81+
(struct subset*) malloc( V * sizeof(struct subset) );
82+
83+
// Create V subsets with single elements
84+
for (int v = 0; v < V; ++v)
85+
{
86+
subsets[v].parent = v;
87+
subsets[v].rank = 0;
88+
}
89+
90+
// Number of edges to be taken is equal to V-1
91+
while (e < V - 1)
92+
{
93+
94+
struct Edge next_edge = graph->edge[i++];
95+
96+
int x = find(subsets, next_edge.src);
97+
int y = find(subsets, next_edge.dest);
98+
99+
100+
if (x != y)
101+
{
102+
result[e++] = next_edge;
103+
Union(subsets, x, y);
104+
}
105+
// Else discard the next_edge
106+
}
107+
108+
109+
printf("Following are the edges in the constructed MST\n");
110+
for (i = 0; i < e; ++i)
111+
printf("%d -- %d == %d\n", result[i].src, result[i].dest,
112+
result[i].weight);
113+
return;
114+
}
115+
116+
int main()
117+
{
118+
119+
int V = 4; // Number of vertices in graph
120+
int E = 5; // Number of edges in graph
121+
struct Graph* graph = createGraph(V, E);
122+
123+
124+
// add edge 0-1
125+
graph->edge[0].src = 0;
126+
graph->edge[0].dest = 1;
127+
graph->edge[0].weight = 10;
128+
129+
// add edge 0-2
130+
graph->edge[1].src = 0;
131+
graph->edge[1].dest = 2;
132+
graph->edge[1].weight = 6;
133+
134+
// add edge 0-3
135+
graph->edge[2].src = 0;
136+
graph->edge[2].dest = 3;
137+
graph->edge[2].weight = 5;
138+
139+
// add edge 1-3
140+
graph->edge[3].src = 1;
141+
graph->edge[3].dest = 3;
142+
graph->edge[3].weight = 15;
143+
144+
// add edge 2-3
145+
graph->edge[4].src = 2;
146+
graph->edge[4].dest = 3;
147+
graph->edge[4].weight = 4;
148+
149+
KruskalMST(graph);
150+
151+
return 0;
152+
}

0 commit comments

Comments
 (0)