博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algorithms: Kruskal's algorithm and Prim's algorithm for Minimum-spanning-tree
阅读量:4046 次
发布时间:2019-05-24

本文共 5391 字,大约阅读时间需要 17 分钟。

Reference: introduction to algorithm

Kruscal Algorithm O(ElgV) by Binary Heaps

Prim's algorithm O(E + VlgV) by Fibonacci heaps

A problem os a spanning tree

for connected, undirected graph G=(V,E) with a weight function w: E->R

 

Kruskal'a algorithm

Implementation by C++

#include 
#include
#include
#include
#include
using namespace std;class Solution{ // implementation by Weighted Quick Union. struct DisjSets { vector
nodes; vector
sizes; int count; // the default five member for DisjSets. DisjSets(int Count) : nodes(Count, -1), sizes(Count, 1), count(Count) {} DisjSets(const DisjSets &rhs) = default; DisjSets(DisjSets &&rhs) = default; DisjSets &operator=(const DisjSets &rhs) = default; DisjSets &operator=(DisjSets &&rhs) = default; // weighted quick union void unionSet(int p, int q) { int i = find(p); int j = find(q); if (i == j) return; if (sizes[i] < sizes[j]) { nodes[i] = j; sizes[j] += sizes[i]; } else { nodes[j] = i; sizes[i] += sizes[j]; } --count; } int find(int p) { while (nodes[p] != -1) p = nodes[p]; return p; } }; struct cmp { bool operator()(const tuple
& l, const tuple
& r) { return get<2>(l) > get<2>(r); } };public:/* Output: return a vector of vector. for the sub vector[0] for start point, vector[1] for the end point for the input sub vector Input: verticle the number of verticles. edges the input edges. start: edges[n][0] end: edges[n][1] weight: edges[n][3] */ vector
> mstKruskal(int verticle, vector
> &edges) { DisjSets Sets(verticle); vector
> minSpanTree; // the tuple store the start, end ,and weight for the edges. priority_queue
>, cmp> minHeap; for(auto & edge : edges) { minHeap.push(make_tuple(edge[0], edge[1], edge[2])); } while(!minHeap.empty()) { auto edge = minHeap.top(); int u = get<0>(edge), v = get<1>(edge); if(Sets.find(u) != Sets.find(v)) { minSpanTree.push_back(vector
{u, v}); Sets.unionSet(u, v); } minHeap.pop(); } return minSpanTree; }};int main(){ vector
> edges = { vector
{0, 1, 5}, vector
{1, 2, 4}, vector
{2, 3, 5}, vector
{3, 4, 3}, vector
{4, 5, 6}, vector
{5, 0, 8}, vector
{0, 2, 1}, vector
{0, 3, 5}, vector
{0, 4, 3} }; Solution sol; auto minSpanTree = sol.mstKruskal(6, edges); return 0;}

Prim's Algorithm

Prim implementation by C++ priority_queue

#include 
#include
#include
#include
#include
#include
using namespace std;class Solution {private: struct cmp{ bool operator ()(const pair
& l , const pair
&r) { return l.second > r.second; } };public:/* 1) Initialize keys of all vertices as infinite and parent of every vertex as -1.2) Create an empty priority_queue pq. Every item of pq is a pair (weight, vertex). Weight (or key) is used used as first item of pair as first item is by default used to compare two pairs.3) Initialize all vertices as not part of MST yet. We use boolean array inMST[] for this purpose. This array is required to make sure that an already considered vertex is not included in pq again. This is where Ptim's implementation differs from Dijkstra. In Dijkstr's algorithm, we didn't need this array as distances always increase. We require this array here because key value of a processed vertex may decrease if not checked.4) Insert source vertex into pq and make its key as 0.5) While either pq doesn't become empty a) Extract minimum key vertex from pq. Let the extracted vertex be u. b) Include u in MST using inMST[u] = true. c) Loop through all adjacent of u and do following for every vertex v. // If weight of edge (u,v) is smaller than // key of v and v is not already in MST If inMST[v] = false && key[v] > weight(u, v) (i) Update key of v, i.e., do key[v] = weight(u, v) (ii) Insert v into the pq (iv) parent[v] = u 6) Print MST edges using parent array. */ vector
> mstPrim(int verticle, vector
> &edges) { constexpr int MAX_INT = std::numeric_limits
::max(); vector
> results; vector
Keys(verticle, MAX_INT); // store the weight of each node vector
Parents(verticle, -1); // -1 means that the node doesn't have parent. vector
inMST(verticle, false); // at initialization we add all node to the Priority. // the pair.first for adjacent node, pair.second for the weight unordered_map
>> adjacents; // build the adjacents for(auto & edge : edges) { int u = edge[0], v = edge[1], weight = edge[2]; adjacents[u].push_back(make_pair(v, weight)); adjacents[v].push_back(make_pair(u, weight)); } Keys[0] = 0; //Assume node 0 is the root. // pair.first for node id, second for the weight. priority_queue
, vector
>, cmp> minHeap; minHeap.push(make_pair(0, 0)); while(!minHeap.empty()) { auto u = minHeap.top().first; minHeap.pop(); inMST[u] = true; for(auto & adj : adjacents[u]) { int v = adj.first; int weight = adj.second; if(inMST[v] == false && Keys[v] > weight) { // update key of v Keys[v] = weight; minHeap.push(make_pair(v, Keys[v])); Parents[v] = u; } } } for(int i = 0; i < Parents.size(); ++i) { if(Parents[i] != -1) { results.push_back(vector
{i, Parents[i]}); } } return results; }};int main(){ vector
> edges = { vector
{0, 1, 5}, vector
{1, 2, 4}, vector
{2, 3, 5}, vector
{3, 4, 3}, vector
{4, 5, 6}, vector
{5, 0, 8}, vector
{0, 2, 1}, vector
{0, 3, 5}, vector
{0, 4, 3} }; Solution sol; auto minSpanTree = sol.mstPrim(6, edges); return 0;}

 

转载地址:http://fwwci.baihongyu.com/

你可能感兴趣的文章
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
教育数字智能化能为现有体系带来新的起点
查看>>
媒体广告业如何将内容资产进行高效地综合管理与利用
查看>>
能源化工要怎么管控核心数据
查看>>
媒体广告业如何运用云盘提升效率
查看>>
企业如何运用企业云盘进行数字化转型-实现新发展
查看>>
司法如何运用电子智能化加快现代化建设
查看>>
iSecret&nbsp;1.1&nbsp;正在审核中
查看>>
IOS开发的开源库
查看>>
IOS开发的开源库
查看>>
Jenkins - sonarqube 代码审查
查看>>