1
+ /*
2
+ Boruvka’s Algorithm is a greedy algorithm used for finding Minimum Spanning Tree (MST) for
3
+ a connected, weighted, and undirected graph.
4
+ The goal of the algorithm is to connect “components” using the shortest or cheapest edge between
5
+ the components. It begins with all of the vertices considered as separate components.
6
+ Boruvka’s algorithm is parallel in nature. It operates in phases. In each phase, it selects
7
+ the cheapest edge from each component, adds all of these to the MST at once, and then contracts
8
+ each connected component into a single component.
9
+ We make use of a Union-Find data structure to keep track of the components.
10
+ This program takes a connected, weighted, and undirected graph as input and prints the total weight of edges in MST.
11
+ */
12
+
13
+ #include < bits/stdc++.h>
14
+ using namespace std ;
15
+
16
+ typedef long long ll;
17
+
18
+ const int N = (int )1e5 + 7 ;
19
+
20
+ // a structure which represents a weighted graph edge
21
+ struct edge {
22
+ int v, u, cost;
23
+ };
24
+
25
+ // global declaration of number of nodes
26
+ int n;
27
+
28
+ // global declaration of number of edges
29
+ int m;
30
+
31
+ // global declaration of parent array
32
+ int p[N];
33
+
34
+ // Array to store the minimum or cheapest outgoing edge for each component of graph
35
+ int min_edge[N];
36
+
37
+ // Array to store all edges
38
+ edge g[N];
39
+
40
+ // Function to find root of a certain component for union find algorithm
41
+ int root (int v)
42
+ {
43
+ if (p[v] == v)
44
+ return v;
45
+
46
+ return (p[v] = root (p[v]));
47
+ }
48
+
49
+ // Function to perform union two components
50
+ bool merge (int v, int u)
51
+ {
52
+ v = root (v), u = root (u);
53
+ if (v == u)
54
+ return 0 ;
55
+
56
+ p[v] = u;
57
+ return 1 ;
58
+ }
59
+
60
+ // Function to initialize DSU
61
+ // Initially, all vertices are their own parents
62
+ void init_div ()
63
+ {
64
+ for (int i = 1 ; i <= n; i++) {
65
+ p[i] = i;
66
+ }
67
+ }
68
+
69
+ // Main function to find MST using Boruvka's algorithm
70
+ void boruvkaAlgorithm ()
71
+ {
72
+
73
+ // Function call to initialize DSU
74
+ init_div ();
75
+
76
+ // No. of components
77
+ int cnt_cmp = n;
78
+
79
+ // Weight of MST
80
+ ll MSTweight = 0 ;
81
+
82
+ // Keep combining components until all components are not combined into a single MST.
83
+ while (cnt_cmp > 1 ) {
84
+ // Everytime initialize cheapest array to -1 as initially there is no cheapest edge
85
+ for (int i = 1 ; i <= n; i++)
86
+ min_edge[i] = -1 ;
87
+
88
+ // Traverses through all edges to find the cheapest edge for each component
89
+ for (int i = 1 ; i <= m; i++) {
90
+ // If the nodes are in the same component then continue
91
+ if (root (g[i].v ) == root (g[i].u ))
92
+ continue ;
93
+
94
+ // Else take roots of 1st and 2nd components and check if current edge is cheapest edge
95
+ int r_v = root (g[i].v );
96
+ if (min_edge[r_v] == -1 || g[i].cost < g[min_edge[r_v]].cost ) {
97
+ min_edge[r_v] = i;
98
+ }
99
+ int r_u = root (g[i].u );
100
+ if (min_edge[r_u] == -1 || g[i].cost < g[min_edge[r_v]].cost ) {
101
+ min_edge[r_u] = i;
102
+ }
103
+ }
104
+
105
+ // Consider the above picked cheapest edges and add them
106
+ // to MST
107
+ for (int i = 1 ; i <= n; i++) {
108
+ if (min_edge[i] != -1 ) {
109
+
110
+ // If the two vertices that are connected with this edge
111
+ // can be merged then add its weight to the answer,i.e MSTweight
112
+ // and decrease the number of components by one
113
+ if (merge (g[min_edge[i]].v , g[min_edge[i]].u )) {
114
+ MSTweight += g[min_edge[i]].cost ;
115
+ cnt_cmp--;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ cout << " Total weight of Minimum Spanning Tree for the given graph: " << MSTweight << endl;
121
+ }
122
+ int main ()
123
+ {
124
+ ios_base::sync_with_stdio (false );
125
+ cin.tie (NULL );
126
+
127
+ cout << " Input number of nodes and edges" << endl;
128
+ cin >> n >> m;
129
+
130
+ cout << " Input source vertex, target vertex and weight for each edge" << endl;
131
+ for (int i = 1 ; i <= m; i++) {
132
+ cin >> g[i].v >> g[i].u >> g[i].cost ;
133
+ }
134
+
135
+ boruvkaAlgorithm ();
136
+
137
+ return 0 ;
138
+ }
139
+
140
+ /*
141
+ INPUT:
142
+ Input number of nodes and edges
143
+ 4 5
144
+ Input source vertex, target vertex and weight for each edge
145
+ 1 2 10
146
+ 2 3 15
147
+ 1 3 5
148
+ 4 2 2
149
+ 4 3 40
150
+ OUTPUT:
151
+ Total weight of Minimum Spanning Tree for the given graph: 17
152
+
153
+ Time Complexity: O(E log V)
154
+ E = Number of edges
155
+ V = Number of vertices
156
+ Space Complexity: O(N)
157
+ */
0 commit comments