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 <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
using namespace std;
class Solution
{
// implementation by Weighted Quick Union.
struct DisjSets
{
vector<int> nodes;
vector<int> 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<int, int, int> & l, const tuple<int, int, int> & 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<vector<int>> mstKruskal(int verticle, vector<vector<int>> &edges)
{
DisjSets Sets(verticle);
vector<vector<int>> minSpanTree;
// the tuple store the start, end ,and weight for the edges.
priority_queue<int, vector<tuple<int, int, int>>, 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<int>{u, v});
Sets.unionSet(u, v);
}
minHeap.pop();
}
return minSpanTree;
}
};
int main()
{
vector<vector<int>> edges = {
vector<int>{0, 1, 5},
vector<int>{1, 2, 4},
vector<int>{2, 3, 5},
vector<int>{3, 4, 3},
vector<int>{4, 5, 6},
vector<int>{5, 0, 8},
vector<int>{0, 2, 1},
vector<int>{0, 3, 5},
vector<int>{0, 4, 3}
};
Solution sol;
auto minSpanTree = sol.mstKruskal(6, edges);
return 0;
}
Prim's Algorithm
Prim implementation by C++ priority_queue
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <limits>
using namespace std;
class Solution {
private:
struct cmp{
bool operator ()(const pair<int, int> & l , const pair<int, int> &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<vector<int>> mstPrim(int verticle, vector<vector<int>> &edges) {
constexpr int MAX_INT = std::numeric_limits<int>::max();
vector<vector<int>> results;
vector<int> Keys(verticle, MAX_INT); // store the weight of each node
vector<int> Parents(verticle, -1); // -1 means that the node doesn't have parent.
vector<bool> 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<int, vector<pair<int, int>>> 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<pair<int, int>, vector<pair<int, int>>, 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<int>{i, Parents[i]});
}
}
return results;
}
};
int main()
{
vector<vector<int>> edges = {
vector<int>{0, 1, 5},
vector<int>{1, 2, 4},
vector<int>{2, 3, 5},
vector<int>{3, 4, 3},
vector<int>{4, 5, 6},
vector<int>{5, 0, 8},
vector<int>{0, 2, 1},
vector<int>{0, 3, 5},
vector<int>{0, 4, 3}
};
Solution sol;
auto minSpanTree = sol.mstPrim(6, edges);
return 0;
}