Escolar Documentos
Profissional Documentos
Cultura Documentos
Kruskals
and
Prims Algorithms,
Applications in Networking
Submitted by:
Hardik Parikh
Soujanya Soni
What is Tree ?
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
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
The Minimum Spanning Tree for a given graph is the Spanning Tree of
minimum cost for that graph.
Complete Graph
7
2
2
5
3
4
1
A connected,
undirected graph
Kruskals and
Prims
Kruskals Algorithm
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
15
8
13
13
1
14
16
12
2
7
5
4
12
16
14
3
14
15
14
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
16
{2,3}
16
Kruskals Algorithm
15
8
13
13
1
14
16
12
4
12
2
7
16
14
14
3
14
15
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
{2,3}
Kruskals Algorithm
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
16
{1,4}
16
16
{2,3}
16
8
13
14
8
13
13
1
14
16
12
4
12
2
7
16
14
14
3
14
15
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
{2,3}
8
13
13
1
14
16
12
4
12
2
7
16
14
3
14
15
14
4
12
16
14
14
3
14
15
Kruskals Algorithm
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
16
{1,4}
16
16
{2,3}
16
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
{2,3}
15
8
13
14
16
12
4
12
2
7
5
13
Kruskals Algorithm
15
16
12
5
13
Kruskals Algorithm
15
15
16
14
14
3
14
15
Kruskals Algorithm
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
16
{1,4}
16
16
{2,3}
16
15
8
13
13
1
14
16
12
4
12
2
7
16
14
3
14
15
14
Kruskals Algorithm
15
13
13
16
1
14
12
12
2
7
14
16
14
14
15
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
16
{2,3}
16
Kruskals Algorithm
8
13
13
16
1
14
12
12
2
7
4
14
16
14
14
15
MST is formed
{1,2}
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
{5,6}
14
{5,8}
15
{6,7}
15
{1,4}
16
{2,3}
16
15
13
14
12
16
14
14
14
15
12
{3,4}
12
{1,8}
13
{4,5}
13
{2,7}
14
{3,6}
14
{7,8}
14
Skip
{5,6}
14
Skip
{5,8}
15
{6,7}
15
{1,4}
16
{2,3}
16
Prims algorithm
15
8
1
16
14
5
13
4
12
2
6
16
{1,2}
Prims algorithm
3
14
15
14
1
14
4
12
13
4
12
2
7
16
16
12
Skip
13
1
14
13
Prims algorithm
8
13
Kruskals Algorithm
15
15
16
14
3
14
15
1
14
12
2
6
The best
choice is {1,2}
Whats next?
Weight:12
Prims algorithm
15
8
13
13
16
14
12
2
7
16
14
1
14
12
3
14
15
Prims algorithm
After {1,8} we may
choose {2,7} or
{7,8}
There are more
than one MST
Prims algorithm
8
5
16
14
4
12
16
15
14
Lets choose
{5,8} at this
moment
12
2
6
14
Prims algorithm
8
5
1
16
16
12
3
14
15
12
16
14
12
3
14
15
5
4
12
2
6
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
6
14
15
16
14
4
12
16
14
15
14
12
2
6
5
13
1
14
14
15
8
13
13
1
14
14
Prims algorithm
15
8
13
14
13
13
3
14
16
13
13
Prims algorithm
15
Weight:12+13
5
13
Whats next?
15
16
14
2
7
1
14
16
15
12
3
7
4
12
2
6
5
13
14
3
14
A MST is formed
Weight = 93
20
54
100
14
20
54
0
44
44
100
14
32
32
99
99
31
14
11
19
19
Prims algorithm
3
Prims algorithm
3
20
54
100
14
20
54
0
44
44
100
14
32
32
99
99
31
14
11
19
19
Prims algorithm
3
Prims algorithm
3
20
54
20
54
100
14
31
14
11
44
31
14
11
44
100
14
32
32
99
99
31
14
11
31
14
11
19
19
Prims algorithm
3
20
54
100
14
20
54
0
44
Prims algorithm
44
100
14
32
32
99
99
31
14
11
19
19
Prims algorithm
3
20
54
0
44
MST is formed
100
14
31
14
11
32
99
31
14
11
19
(m = edges, n = nodes)
(m = edges, n = nodes)
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.
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
Questions
The End
Thank you