This repository was archived by the owner on Nov 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathkruskal.cpp
More file actions
95 lines (80 loc) · 1.82 KB
/
kruskal.cpp
File metadata and controls
95 lines (80 loc) · 1.82 KB
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
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX = 1e4 + 5;
ll parent[MAX], ranks[MAX], vertex, edges;
pair <ll, pair<ll, ll> >graph[MAX];
void initialize()
{
for(ll i = 0;i < MAX;++i)
{
parent[i] = i;
ranks[i] = 0;
}
}
ll find(ll x)
{
while(parent[x] != x)
{
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
void Union(ll x, ll y)
{
ll p = find(x);
ll q = find(y);
if (ranks[p] < ranks[q])
parent[p] = parent[q];
else if (ranks[p] > ranks[q])
parent[q] = parent[p];
else
{
parent[p] = parent[q];
ranks[q]++;
}
}
ll kruskal(pair<ll, pair<ll, ll> >graph[])
{
ll x, y, e=0, i=0;
ll cost, minWeight = 0;
pair<ll, pair<ll, ll> >result[vertex];
pair<ll, pair<ll, ll> >next;
while(e < vertex-1)
{
next = graph[i++];
x = next.second.first;
y = next.second.second;
cost = next.first;
if(find(x) != find(y))
{
result[e++] = next;
minWeight += cost;
Union(x, y);
}
}
cout<< "The edges in the spanning tree: "<<endl;
for (i = 0; i < e; ++i)
cout<<result[i].second.second<<" -- "<<result[i].second.first<<" == "<<result[i].first<<endl;
return minWeight;
}
int main()
{
ll x, y;
ll weight, minWeight;
initialize();
cout<<"Enter the number of vertices: ";
cin >> vertex;
cout<<"Enter the number of edges: ";
cin >> edges;
for(int i=0;i<edges;i++)
{
cin >> x >> y >> weight;
graph[i] = make_pair(weight, make_pair(x, y));
}
sort(graph, graph+edges);
minWeight = kruskal(graph);
cout << "The weight of the spanning tree = " << minWeight << endl;
return 0;
}