Minimum Cost Spanning Tree
Sometime there may arise a case that we want to limit cost on a resource but want the graph to remain well-connected.
For example, suppose a fiber connection company wants to connect \(n\) cities and they want to use the lowest amount of optic cable to be utilised.
The solution to this kind of problems is Minimum Cost Spanning Tree.
Tip
A minimally connected graph is called a Spanning tree.
Important Notes on Spanning Trees
- Removing an edge from the tree disconnects a graph
- Adding an edge to the tree forms a loop.
- There can be more than one spanning tree for a graph
- A tree on \(n\) vertices has exactly \(n-1\) edges
- In a tree, each vertex is joined by a unique path.
Warning
We choose the spanning tree with minimum cost to solve the problem discussed at the start of the page.
Now to create or find a minimum cost spanning tree, there are two algorithms, - Prim's algorithm - Kruskal's Algorithm
Prim's Algorithm
Algorithm
- Consider two sets \(TE\) and \(TV,\) representing tree edges in MCST and tree vertices in MCST respectively.
- Initially, \(TV=TE=\phi\)
- Suppose we find an edge \(e\) with the lowest weight in the entire graph and it connects edges \(i\) and \(j.\)
- Now \(TV\) becomes \(\{i,j\}\) and \(TE\) becomes \(\{e\}\)
- Now find an edge \(t\) connecting vertices \(v_1\) and \(v_2\) such that it has the minimum weight and \(v_1\in TV\) and \(v_2 \notin TV.\)
- Now add \(v_2\) in \(TV\) and \(t\) in \(TE.\)
- Repeat steps \(5\) and \(6\) for \(n-2\) times
Implementation
Basic Implementation
A Better Approach
Time Complexity
Basic Implementation
The time complexity of the basic implementation is \(O(n^3)\)
Approach inspired by Djikstra's Algorithm
The second approach is inspired by the Djikstra's Algorithm.
This implementation has the time complexity of \(O(n^2)\) similar to that of Djikstra's Algorithm
Kruskal's Algorithm
Algortihm
- Consider \(n\) component, i.e., \(n\) vertices.
- After ordering the edges in ascending order, pick the smallest edge and add it in the tree, if it does not forms a cycle.
- Repeat steps \(1\) and \(2\) untill you found \(n-1\) edge.
Implementation
Naive Implementation
Using UnionFind Data Structure
Assume UnionFind
has be defined in the code written below:
Time Complexity
Basic Implementation
The current implementation has the time complexity of \(O(n^2)\)
Using Union Find Data Structure
The overall time complexity is $O((m+n)\log(n)), $ where \(m\) is the number of edges which is atmost \(n^2\) so time complexity becomes \(O(n \log (n))\)