14508_28. minimum spanning tree
Post on 21-May-2017
237 Views
Preview:
TRANSCRIPT
Data StructuresData StructuresLecture 28: Minimum Spanning Tree
Lovely Professional University, PunjabLovely Professional University, Punjab
Outlines
Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Minimum Spanning Tree Input: A connected, undirected graph G =
(V, E) with weight function w : E R.• For simplicity, we assume that all edge
weights are distinct.
• Output: A spanning tree T, a tree that connects all vertices, of minimum weight:
Tvu
vuwTw),(
),()(
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of MST
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Prim’s AlgorithmIDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in A.Q Vkey[v] for all v V key[s] 0 for some arbitrary s Vwhile Q
do u EXTRACT-MIN(Q)for each v Adj[u]
do if v Q and w(u, v) < key[v]then key[v] w(u, v) ⊳ DECREASE-KEY
[v] u
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
7
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
7
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
5 7
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
5 7
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
0
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
0
8
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
0
8
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
3 0
8
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
3 0
8
9
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A V – A
6
5 7
3 0
8
9
15
6 125
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm
It is a greedy algorithm.
In Kruskal’s algorithm, the set A is a forest.
The safe edge is added to A is always a least-weight edge in the graph that connects two distinct Components.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm The operation FIND-SET(u) returns a representative element from the set that contains u.
Thus we can determine whether two vertices u and v belong to the same tree by testing whether
FIND-SET(u) = FIND-SET(v)
The combining of trees is accomplished by the UNION procedure
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm1. A 2. for each vertex v V[G]3.do MAKE-SET(v)4.short the edges of E into non-decreasing order by weight.5.for each edge (u, v) E, taken in non-decreasing orderby weight.6.do if FIND-SET(u) != FIND-SET(v)7. then, A A U {(u, v)}8. UNION(u, v)9.return A
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ComplexityPrim’s Algorithm: O(E logV)
Kruskal’s Algorithm: O(E logV)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
top related