-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKruskal.cpp
87 lines (80 loc) · 1.64 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
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
#define I 65535
using namespace std;
class kruskal
{
public:
int edge[9][3] = {{1, 2, 28}, {1, 6, 10}, {2, 3, 16}, {2, 7, 14}, {3, 4, 12}, {4, 5, 22}, {4, 7, 18}, {5, 6, 25}, {5, 7, 24}};
int set[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
int included[9] = {0};
int t[2][7];
void join(int u, int v);
int find(int u);
};
void kruskal ::join(int u, int v)
{
if (set[u] < set[v])
{
set[u] += set[v];
set[v] = u;
}
else
{
set[v] += set[u];
set[u] = v;
}
}
int kruskal ::find(int u)
{
int x = u, v = 0;
while (set[x] > 0)
{
x = set[x];
}
while (u != x)
{
v = set[u];
set[u] = x;
u = v;
}
return x;
}
int main(int argc, const char *argv[])
{
kruskal ob;
int u = 0, v = 0, i, j, k = 0, min = 65535, n = 9;
i = 0;
while (i < 6)
{
min = 65535;
for (j = 0; j < n; j++)
{
if (ob.included[j] == 0 && ob.edge[j][2] < min)
{
u = ob.edge[j][0];
v = ob.edge[j][1];
min = ob.edge[j][2];
k = j;
}
}
if (ob.find(u) != ob.find(v))
{
ob.t[0][i] = u;
ob.t[1][i] = v;
ob.join(ob.find(u), ob.find(v));
ob.included[k] = 1;
i++;
// printf("%d %d %d %d\n",u,v,find(u),find(v));
}
else
{
ob.included[k] = 1;
}
}
printf("Spanning Tree\n");
for (i = 0; i < 6; i++)
{
printf("%d %d\n", ob.t[0][i], ob.t[1][i]);
}
return 0;
}