-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkruskal.cpp
98 lines (89 loc) · 2.23 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
/**
* @file kruskal.cpp
* @author prakash sellathurai
* @brief minimum spanning tree using Kruskal algorithm
* @version 0.1
* @date 2021-07-16
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <boost/pending/disjoint_sets.hpp>
#include <iostream>
#include <vector>
using namespace std;
/**
* @brief Edge pairs are stored in a vector
*
*/
class EdgePair {
public:
int x, y, weight;
EdgePair() {}
EdgePair(int x, int y, int weight) : x(x), y(y), weight(weight) {}
};
/**
* @brief Weighted Graph
*
*/
class Graph {
private:
int V; // No. of vertices
int E; // No. of edges
vector<vector<pair<int, int>>> edges; // Graph represented as adjacency list
public:
Graph(int V) : V(V+1), E(0) { edges.resize(V+1); }
void addEdge(int v, int w, int weight) {
edges[v].push_back(make_pair(w, weight));
edges[w].push_back(make_pair(v, weight));
E++;
}
static bool sortbysec(const EdgePair &e1, const EdgePair &e2) {
return e1.weight < e2.weight;
}
vector<EdgePair> to_edgearray() {
vector<EdgePair> res;
for (int i = 0; i < V; i++) {
for (auto edge : edges[i]) {
res.push_back(EdgePair(i, edge.first, edge.second));
}
}
return res;
}
/**
* @brief kruskal algorithm, for finding minimum spanning tree weight
*
* @return int
*/
int kruskal() {
int weight = 0; /*cost of minimum spanning tree*/
vector<EdgePair> e = to_edgearray();
sort(e.begin(), e.end(), sortbysec);
int vectorlist[V + 1];
int parentlist[V + 1];
boost::disjoint_sets<int *, int *> ds(vectorlist, parentlist);
for (int i = 0; i < V; i++) {
ds.make_set(i);
}
std::cout << std::endl;
for (auto edge:e) {
auto u = ds.find_set(edge.x);
auto v = ds.find_set(edge.y);
if (u != v) {
std::cout << "Edge : " << edge.x << " " << edge.y << std::endl;
weight += edge.weight;
ds.link(edge.x, edge.y);
}
}
return weight;
}
};
int main(int argc, const char **argv) {
Graph g(5);
g.addEdge(0, 1, 1);
g.addEdge(1, 2, 2);
g.addEdge(2, 3, 3);
std::cout << "Minimum spanning tree weight is : " << g.kruskal() << std::endl;
return 0;
}