Min Span
Min Span
OverView
Tree definition Spanning Trees Minimum Spanning Trees Kruskals and Prims algorithms for Minimum Spanning Trees Comparison & Analysis of Kruskals and Prims Algorithms Why MST ???? Applications of MST in Sensor Networks Conclusion
What is Tree ?
Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore, any tree must be a simple graph. Theorem: An undirected graph is a tree if and only if there is a unique simple path between any of its vertices.
Trees
Example: Are the following graphs trees?
No.
Yes.
Yes.
No.
Spanning Trees
A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees
is a connected subgraph acyclic is a tree (|E| = |V| - 1) contains all vertices of G (spanning)
Spanning trees
Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction ...then a spanning tree of the graph is a connected subgraph in which there are no cycles Total of 16 different spanning trees for the graph below
Complete Graph
7 2 5 4 1 3
Kruskals algorithm
Edge first 1. Arrange all edges in a list (L) in nonnon-decreasing order 2. Select edges from L, and include that in set T, avoid cycle. 3. Repeat 3 until T becomes a tree that covers all vertices
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
2 7
14
16
3
14
15
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
14
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
1
12
16
2 7
14
16
3
14
2 7
14
16
3
14
15
15
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
14
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
1
12
16
2 7
14
16
3
14
2 7
14
16
3
14
15
15
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
14
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
1
12
16
2 7
14
16
3
14
2 7
14
16
3
14
15
15
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
Skip 14
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
Skip Skip
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
1
12
16
2 7
14
16
3
14
2 7
14
16
3
14
15
15
Kruskals Algorithm
{1,2} 12 12 13 13 14 14 14 14 15 15 16 16
Prims algorithm
Start form any arbitrary vertex Find the edge that has minimum weight form all known vertices Stop when the tree covers all vertices
8
13
15 13
5 4
12 14
{3,4} {1,8} {4,5} {2,7} {3,6} {7,8} {5,6} {5,8} {6,7} {1,4} {2,3}
1
14 12
16
2 7
14
16
3
14
15
MST is formed
Prims algorithm
8
13 15 13
Prims algorithm
8
13 15 13
5 4
12 14
5 4
12 14 12
1
14 12
16
1
14
1 2
16
1 2 6
2 7
14
16
3
14
16
3
14
15
14 15
Weight:12
Prims algorithm
8 1
14 15 13 16
Prims algorithm
After {1,8} we may choose {2,7} or {7,8} There are more than one MST Whats next?
5 4
12 14
8
13
8 1
14
15 13 16
5 4
12 14
8
13
1
12
1
12
Lets choose {2, 7} at this moment We are free to choose {5,8} or {6,7} but not {7,8} because we need to avoid cycle
2 7
14
16
3
14
2 6
2 7
16
3
14
2 6 7
14
15
Weight:12+13
15
Prims algorithm
8
13
Prims algorithm
15
5 1
16
8
13
8 1
14 16
5 4
12 14
8
13
15 13
5 4
The best choice is now {4,3}
4
12 14 12
1 2 6 7
14
14
2 7
16
3
14
Lets choose {5,8} at this moment The best choice is now {4,5}
1
12
2 7
16
3
14
2 6 7
14
15
15
Prims algorithm
8 1
14 16
Prims algorithm
15
5 4
14
8
13
5
13
8 1
14 16
5 4
14
8
13
15 13
5 4
12
1
12
4
12
2 7
16
3
14
2 6 7
14
1
12
2 7
16
3 6 7
2
14
3
14
15
15
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 20
Prims algorithm
3 54 0 44 14 8 5 100 32 99 14 11 19 31 MST is formed 20
Testing if an edge creates a cycle can be slow unless a complicated data structure called a union-find structure is used. It usually only has to check a small fraction of the edges, but in some cases (like if there was a vertex connected to the graph by only one edge and it was the longest edge) it would have to check all the edges. This algorithm works best, of course, if the number of edges is kept to a minimum.
If a heap is not used, the run time will be O(n^2) instead of O(m + n log n). However, using a heap complicates the code since youre complicating the data structure. A Fibonacci heap is the best kind of heap to use, but again, it complicates the code. Unlike Kruskals, it doesnt need to see all of the graph at once. It can deal with it one piece at a time. It also doesnt need to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges. For this algorithm the number of nodes needs to be kept to a minimum in addition to the number of edges. For small graphs, the edges matter more, while for large graphs the number of nodes matters more.
Minimizing costs
Suppose you want to provide solution to : electric power, Water, telephone lines, network setup To minimize cost, you could connect locations using MST However, MST is not necessary the shortest path and it does not apply to cycle
Conclusion
Kruskals has better running times if the number of edges is low, while Prims has a better running time if both the number of edges and the number of nodes are low.
So, of course, the best algorithm depends on the graph and if you want to bear the cost of complex data structures.
Questions
The End
Thank you