-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal.cpp
101 lines (83 loc) · 2 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include <algorithm>
/*
LIST OF ADA PRACTICALS DONE TILL NOW
1)prim
2)kruskal
3)knapsack
4)selectionsort,permutation,power
5)magic square
6)multi-stage-graph
7)djikstra
8)pivotelement
9)merge and quick
10)iterative quick sort
11)horners
*/
using namespace std;
struct Edge {
int u, v, weight;
};
bool compare(Edge a, Edge b) {
return a.weight < b.weight;
}
int find(int v, vector<int>parent) {
if (parent[v] == v)
return v;
return parent[v] = find(parent[v], parent);
}
void union_find(int u, int v, vector<int>parent, vector<int>rank) {
u = find(u, parent);
v = find(v, parent);
if (rank[u] < rank[v])
parent[u] = v;
else if (rank[v] < rank[u])
parent[v] = u;
else {
parent[v] = u;
rank[u]++;
}
}
void kruskal(vector<Edge> graph, int V) {
vector<Edge> mst;
vector<int>parent(V);
vector<int>rank(V);
for (int i = 0; i < V; i++) {
parent[i] = i;
rank[i] = 0;
}
sort(graph.begin(), graph.end(), compare);
for (int i = 0; i < graph.size(); i++) {
Edge e = graph[i];
int u = find(e.u, parent);
int v = find(e.v, parent);
if (u != v) {
mst.push_back(e);
union_find(u, v, parent, rank);
}
}
cout << "Minimum Spanning Tree: " << endl;
for (int i = 0; i < mst.size(); i++) {
cout << mst[i].u << " - " << mst[i].v << " == " << mst[i].weight << endl;
}
cout << "Minimum Cost: " << endl;
int minCost = 0;
for (int i = 0; i < mst.size(); i++) {
minCost += mst[i].weight;
}
cout << minCost << endl;
}
int main() {
int V = 5;
vector<Edge> graph;
graph.push_back({0, 3, 5});
graph.push_back({0, 1, 10});
graph.push_back({1, 2, 2});
graph.push_back({1, 3, 20});
graph.push_back({2, 3, 15});
graph.push_back({2, 4, 3});
graph.push_back({3, 4, 12});
kruskal(graph, V);
return 0;
}