-
Notifications
You must be signed in to change notification settings - Fork 0
/
kruskal.cpp
79 lines (68 loc) · 1.65 KB
/
kruskal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
#define I 32767
#define n 6
#define V 7 // # of vertices in Graph
#define E 9 // # of edges in Graph
int set[8] = {-1, -1, -1, -1, -1, -1, -1, -1}; // Array for finding cycle
int res[2][6] = {0};
int visited[9] = {0};
void myUnion(int u, int v){
if(set[u] < set[v]){
set[u] += set[v];
set[v] =u;
} else {
set[v] += set[u];
set[u] =v;
}
}
void PrintMCST(int T[][V-1], int A[][E]){
cout << "\nMinimum Cost Spanning Tree Edges\n" << endl;
for (int i {0}; i<V-1; i++){
cout << "[" << T[0][i] << "]-----[" << T[1][i] << "]" << endl;
}
cout << endl;
}
int find(int u){
int x = u;
int v = 0;
while (set[x] > 0){
x = set[x];
}
while (u != x){
v = set[u];
set[u] = x;
u = v;
}
return x;
}
void KruskalsMCST(int edges[3][9]){
int i , j, k, u, v;
i = 0;
while(i < n){
int min =I;
for(j = 0; j < 9 ; ++j){
if(visited[j] ==0 && min > edges[2][j]){
min = edges[2][j];
k = j;
u = edges[0][j];
v = edges[1][j];
}
}
if(find(u) != find(v)){
res[0][i] = u;
res[1][i] = v;
myUnion(find(u), find(v));
++i;
}
visited[k] = 1;
}
PrintMCST(res, edges);
}
int main() {
int edges[3][9] = {{ 1, 1, 2, 2, 3, 4, 4, 5, 5},
{ 2, 6, 3, 7, 4, 5, 7, 6, 7},
{25, 5, 12, 10, 8, 16, 14, 20, 18}};
KruskalsMCST(edges);
return 0;
}