-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathKruskalMST.h
More file actions
45 lines (39 loc) · 1.19 KB
/
KruskalMST.h
File metadata and controls
45 lines (39 loc) · 1.19 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
#ifndef KRUSKALMST_H_INCLUDED
#define KRUSKALMST_H_INCLUDED
#include"EdgeWeightedGraph.h"
#include"UnionFind.h"
/*
克鲁斯卡尔
idea: 不断从所有边中寻找最小值,要求不能构成环路(不在同一个连通集)
note: 相同于不断合并不同的连通分量
note: unionFind数据结构
*/
class KruskalMST{
private:
queue<Edge> result;
double totalWeight;
priority_queue<Edge,vector<Edge>,std::greater<Edge>> minpq; //初始加入所有边
public:
KruskalMST(EdgeWeightedGraph* G)
:totalWeight(0.0){
UnionFind* uf = new UnionFind(G->getV());
for(Edge e:G->getAllEdges())
minpq.push(e);
//生成MST
while(!minpq.empty() && result.size()<G->getV()-1){
Edge e = minpq.top();
minpq.pop();
int v=e.either(); int w=e.other(v);
//已经在同一个连通集合
if(uf->connected(v,w)) continue;
else{
uf->unionV(v,w);
result.push(e);
totalWeight += e.getWeight();
}
}
}
queue<Edge> getRes(){ return result;}
double getWeight(){return totalWeight;}
};
#endif // KRUSKALMST_H_INCLUDED