diff options
| author | Paul Jungeblut <paul.jungeblut@gmail.com> | 2016-10-06 19:38:51 +0200 |
|---|---|---|
| committer | Paul Jungeblut <paul.jungeblut@gmail.com> | 2016-10-06 19:38:51 +0200 |
| commit | a3e12953f34198004a960473538d9ed28be387dc (patch) | |
| tree | 9cb25a3f10b67ebba7541108e5dfb0d7385a3f8f /graph/kruskal.cpp | |
| parent | 4faf0483a66578774d95830695f665169469d96f (diff) | |
Replacing Kruskal code to be compatible with union find implementation.
Diffstat (limited to 'graph/kruskal.cpp')
| -rw-r--r-- | graph/kruskal.cpp | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/graph/kruskal.cpp b/graph/kruskal.cpp index 124c8bb..a955632 100644 --- a/graph/kruskal.cpp +++ b/graph/kruskal.cpp @@ -1,15 +1,9 @@ -//Takes a Graph g (EdgeList!!!) with N nodes and computes the MST and Cost of it. Runtime: O(|E|*log(|E|)) -//Requires UnionFind-Datastructure!!! -pair<graph,int> buildMST(int N, graph& g) { - UnionFind uf(N); - graph mst; int mst_cost = 0; int M = g.size(); - sort(g.begin(),g.end()); - for(int i = 0; i < M; i++) { - int u = g[i].second.first, v = g[i].second.second; - if(uf.findSet(u) != uf.findSet(v)) { - mst.push_back(g[i]); mst_cost += g[i].first; - uf.unionSets(u,v); - } - } - return make_pair(mst,mst_cost); -} +// Union-Find Implementierung von oben. Laufzeit: O(n*log(n)) +sort(edges.begin(), edges.end()); +vector<ii> mst; int cost = 0; +for (auto &e : edges) { + if (findSet(e.from) != findSet(e.to)) { + unionSets(e.from, e.to); + mst.push_back(ii(e.from, e.to)); + cost += e.cost; +}} |
