-
Notifications
You must be signed in to change notification settings - Fork 3
/
mst.cpp
108 lines (101 loc) · 2.56 KB
/
mst.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
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
// Start UnionFind (for Kruskal)
class UnionFind{
public:
unordered_map<int, int> father;
UnionFind(int n){
for(int i = 0; i < n; i++){
father[i] = i;
}
}
int find(int x){
int parent = father[x];
while(parent != father[parent]){
parent = father[parent];
}
return parent;
}
void union_both(int x, int y){
int fa_x = find(x);
int fa_y = find(y);
if(fa_x != fa_y){
father[fa_x] = father[fa_y];
}
}
};
// End UnionFind
class Edge{
public:
int weight;
char start; // 'A' ~ 'Z'
char end; // 'A' ~ 'Z'
Edge(): weight(0), start(0), end(0){}
Edge(int w, int s, int e){
this->weight = w;
this->start = s;
this->end = e;
}
bool operator<(const Edge& obj) const {
return this->weight < obj.weight; //从小到大排序
}
bool operator()(const Edge& A, const Edge& B) const {
return A.weight < B.weight;
}
};
// Prime Algorithm - Minimum Spanning Tree
// Input : Edges
// Output : Total minimal weights and result that contains the set of edges in MST
int prime(vector<Edge>& result, vector<Edge>& edges){
}
// Kruskal Algorithm - Minimum Spanning Tree
// Input : Edges
// Output : Total minimal weights and result that contains the set of edges in MST
int kruskal(vector<Edge>& result, vector<Edge>& edges){
int totalWeight = 0;
sort(edges.begin(), edges.end());
UnionFind uf(128);
for(int i = 0; i < edges.size(); i++){
Edge edge = edges[i];
if(uf.find(edge.start) != uf.find(edge.end)){
uf.union_both(edge.start, edge.end);
result.push_back(edge);
totalWeight += edge.weight;
}
}
return totalWeight;
}
void printedges(vector<Edge>& edges){
for(auto edge : edges){
cout<<edge.start <<" -> "<<edge.end<<" ";
}
}
// test Minimum Spanning Tree
int main(void){
//data from http://www.cnblogs.com/adforce/p/3247437.html
vector<Edge> edges;
edges.push_back(Edge(4, 'A', 'B'));
edges.push_back(Edge(8, 'B', 'C'));
edges.push_back(Edge(7, 'C', 'D'));
edges.push_back(Edge(9, 'D', 'E'));
edges.push_back(Edge(10, 'E', 'F'));
edges.push_back(Edge(2, 'F', 'G'));
edges.push_back(Edge(1, 'G', 'H'));
edges.push_back(Edge(8, 'H', 'A'));
edges.push_back(Edge(11, 'B', 'H'));
edges.push_back(Edge(2, 'C', 'I'));
edges.push_back(Edge(7, 'I', 'H'));
edges.push_back(Edge(6, 'I', 'G'));
edges.push_back(Edge(4, 'C', 'F'));
edges.push_back(Edge(14, 'D', 'F'));
// Kruskal Algorithm
vector<Edge> mst;
int total = kruskal(mst, edges);
cout<<"Total minimal weight is "<<total<<endl;
printedges(mst);
return 0;
}