-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprim-MST.cc
78 lines (63 loc) · 1.94 KB
/
prim-MST.cc
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
#include<bits/stdc++.h>
using namespace std;
struct Cut {
int cost;
int end_point;
pair<int,int> edge;
Cut(int cost, int end_point, pair<int,int> edge){
this->cost = cost;
this->end_point = end_point;
this->edge = edge;
}
};
class CutComparison {
public:
bool operator()(const Cut& a, const Cut& b) {
return a.cost > b.cost;
}
};
struct MST {
int cost;
vector<pair<int, int> > edges;
};
MST PrimMST(int num_nodes, const vector<list<int> >& adjacency, const vector<vector<int> >& weight) {
priority_queue<Cut, vector<Cut>, CutComparison> to_use;
unordered_set<int> in_MST;
unordered_set<int> out_MST;
//vector<pair<int,int> > edges;
MST result;
result.cost = 0;
// Introduce first node in MST, rest of them in set of not used nodes
in_MST.insert(0);
for (int i = 1; i < num_nodes; i++)
out_MST.insert(i);
for (auto connected : adjacency[0])
to_use.push(Cut(weight[0][connected], connected, {0, connected}));
while (in_MST.size() != num_nodes) {
auto current = to_use.top();
to_use.pop();
while (out_MST.count(current.end_point) == 0) {
current = to_use.top();
to_use.pop();
}
for (auto connected : adjacency[current.end_point])
to_use.push(Cut(weight[current.end_point][connected],
connected,
{current.end_point, connected}));
result.edges.push_back(current.edge);
in_MST.insert(current.end_point);
out_MST.erase(current.end_point);
result.cost += current.cost;
}
return result;
}
int main() {
int num_nodes = 4;
const int INF = numeric_limits<int>::max();
vector<list<int> > adjacency = {{3}, {2, 3}, {1, 3}, {0, 1, 2}};
vector<vector<int> > weight = {{INF, INF, INF, 1},
{INF, INF, 6, 5},
{INF, 6, INF, 7},
{1, 5, 7, INF}};
cout << PrimMST(num_nodes, adjacency, weight).cost << endl;
}