-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_spanning_tree_O(ElogV)_.cpp
52 lines (38 loc) · 1.92 KB
/
minimum_spanning_tree_O(ElogV)_.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
// kruskal alg for finding minimum spanning tree.
// kruskal func will return the amount of MST, you can modify it to get the real MSP representation.
//Variants:
// 1. for finding maximum spanning tree just multiply weights of edges by -1 and run the alg.
// 2. for finding minimum spanning forest just consider this : every time an edge is taken number of components decreases by one, so run the alg and take edges while number of components are less that the desired number of components.
// 3. in order to find minimum spanning subgraph, just add fixed edges to ufds and run the alg.
// 4. in order to find second best spanning tree, first find MST, after that for each edge in MST temporary set it off so the kruskal alg wont select it, do this for every edge in MST and the best answer is second best spanning tree.
// 5. minmax is problem of finding minimum of maximum of edge weights among all possible pahts between i, j. in order to find min max (or maxmin) run the Kruskal alg and save the MST representation then traversing from i to j and finding the maximum weight is the answer.
vector<int> ufds;
vector<pair<int, ii>> graph;
void buildUfds(int n) {
ufds.clear(); ufds.resize(n);
for(int i = 0; i < n; i++) ufds[i] = i;
}
int findSet(int i) {
return (ufds[i] == i) ? i : (ufds[i] = findSet(ufds[i]));
}
bool isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
void joinSets(int i, int j) {
int a = findSet(i), b = findSet(j);
if (a < b) ufds[a] = b;
else ufds[b] = a;
}
int kruskal() {
int cost = 0;
for(int i = 0; i < graph.size(); i++) {
pair<int, ii> fr = graph[i];
if(!isSameSet(fr.second.first, fr.second.second)) {
cost += fr.first;
joinSets(fr.second.second, fr.second.first);
}
}
return cost;
}
// in main:
// sort edge list base on weight assending then call buildUfds(n) where n is number of verteces before calling kruskal alg.