Kruskal algorithm
為了執行Kruskal Algorithm的一個採用C++11的演算法。 我下載了code::blocks的的13.12 版本。 安裝在電腦上。 code:::blocks 有一個好處是, 在你安裝的時候, 注意不要解除安裝之前安裝的code::blocks (之前我的是10.05版本), 那麼根據安裝提示, 新的code::blocks IDE 會保留(沿襲)下你已經安裝的code::blocks的所有設定。 這其中包括之前對Compiler 連結庫的設定(例如我對opencv的庫的設定)等等。 甚至是你之前執行的程式也會出現在start page 中。 也就是說所有的這些都會保留, 你不需要擔心要重新設定了。
如下Graph:
Kruskal 演算法如下:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Edge {
char vertex1;
char vertex2;
int weight; // edges is modeled by 2 vertices and a weight
Edge(char v1, char v2, int w): vertex1(v1), vertex2(v2), weight(w) {}
};
struct Graph {
vector<char> vertices; // a vector of vertices
vector<Edge> edges; // a vector of edges
};
unordered_map<char, char> PARENT;//to record the parental relationship between vertices
unordered_map<char, int> RANK; //RANK is used to record the depth of the tree.
// THe purpose of the RANK is used to flatten the treee so that we have
// the better performance of the find function.
char Find(char vertex) {
if (PARENT[vertex] == vertex)
return PARENT[vertex];
else
return Find(PARENT[vertex]);
}
void Union(char root1, char root2) {
if (RANK[root1] > RANK[root2]) {
PARENT[root2] = root1;
}
else if (RANK[root1] < RANK[root2]) {
PARENT[root1] = root2;
}
else {
PARENT[root1] = root2;
RANK[root2]++;
}
}
void MakeSet(char vertex) {
PARENT[vertex] = vertex;
RANK[vertex] = 0;
}
void Kruskal(Graph& g) {
vector<Edge> A;
for(auto c: g.vertices) {
MakeSet(c);
}
sort(g.edges.begin(), g.edges.end(), [](Edge x, Edge y) { return x.weight < y.weight;});
//O(E*log(E))
for (Edge e: g.edges) {
char root1 = Find(e.vertex1);
char root2 = Find(e.vertex2);
if(root1 != root2) {
A.push_back(e);
Union(root1, root2);
}
}
// print out the minimum spanning tree
for(Edge e: A) {
cout << e.vertex1 << "--" << e.vertex2 << " " << e.weight << endl;
}
}
int main() {
char t[] = {'a', 'b', 'c', 'd', 'e', 'f'};
Graph g; // created a graph
g.vertices = vector<char>(t, t + sizeof(t)/sizeof(t[0]));
g.edges.push_back(Edge('a', 'b', 4));
g.edges.push_back(Edge('a', 'f', 2));
g.edges.push_back(Edge('f', 'b', 5));
g.edges.push_back(Edge('c', 'b', 6));
g.edges.push_back(Edge('c', 'f', 1));
g.edges.push_back(Edge('f', 'e', 4));
g.edges.push_back(Edge('d', 'e', 2));
g.edges.push_back(Edge('d', 'c', 3));
// call the function Kruskal to find MST of graph g
Kruskal(g);
return 0;
}
執行:
即下圖:
相關文章
- algorithmGo
- Kruskal演算法演算法
- Kruskal 重構樹
- Kruskal重構樹
- Note - kruskal 重構樹
- 【模板】最小生成樹-kruskal
- Expectation Maximization AlgorithmGo
- Branch and Bound AlgorithmGo
- Introduction to AlgorithmGo
- Z-algorithmGo
- Algorithm assignment 1Go
- Algorithm in Javascript Bubble SortGoJavaScript
- Algorithm in Javascript Bucket SortGoJavaScript
- Adaboost Algorithm StepGo
- Algorithm演算法Go演算法
- CA Data Classification algorithmGo
- 【學習筆記】Kruskal 重構樹筆記
- kruskal重構樹學習筆記筆記
- 圖解Prim&Kruskal演算法圖解演算法
- Kruskal 最小生成樹演算法演算法
- Matlab生成Kruskal最小生成樹Matlab
- 【Algorithm】樹結構整理Go
- Algorithm for Maximum Subsequence Sum zGo
- Java Stream API:實現 Kruskal 演算法JavaAPI演算法
- 22. Generate Parentheses (recursion algorithm)Go
- 【Algorithm】快排分割槽方法Go
- 【Algorithm】全排列演算法Go演算法
- KMP(The Knuth-Morris-Pratt Algorithm)KMPGo
- Local dimming algorithm in matlabGoMatlab
- 演算法之路 - Way to Algorithm演算法Go
- [Algorithm] 1. A+B ProblemGo
- Implement Leader Election Algorithm With GoGo
- (EM演算法)The EM Algorithm演算法Go
- The LRU Algorithm and Full Table Scans (81)Go
- HDU 3080 The plan of city rebuild(prim和kruskal)Rebuild
- LZ4 compression algorithm on FPGAGoFPGA
- 維特比演算法(Viterbi Algorithm)維特比演算法ViterbiGo
- OI loves Algorithm——字尾陣列Go陣列