Download presentation
Presentation is loading. Please wait.
Published byΖαρά Αναγνωστάκης Modified over 5 years ago
1
Prim’s algorithm IDEA: 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 V key[v] ¥ for all v Î V key[s] 0 for some arbitrary s Î V while 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 p[v] u At the end, {(v, p[v])} forms the MST.
2
Example of Prim’s algorithm
Î A Î V – A 6 12 5 9 14 7 15 8 3 10
3
Example of Prim’s algorithm
Î A Î V – A 6 12 5 9 14 7 15 8 3 10
4
Example of Prim’s algorithm
Î A Î V – A 6 12 5 9 7 14 7 15 8 15 3 10 10
5
Example of Prim’s algorithm
Î A Î V – A 6 12 5 9 7 14 7 15 8 15 3 10 10
6
Example of Prim’s algorithm
12 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 15 3 10 10
7
Example of Prim’s algorithm
12 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 15 3 10 10
8
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 14 15 3 10 8
9
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 14 15 3 10 8
10
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 14 15 3 10 8
11
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 3 15 3 10 8
12
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 3 15 3 10 8
13
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 3 15 3 10 8
14
Example of Prim’s algorithm
6 Î A Î V – A 6 12 5 9 5 7 9 14 7 15 8 3 15 3 10 8
15
Analysis of Prim Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY Q V
key[v] ¥ for all v Î V key[s] 0 for some arbitrary s Î V while 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) p[v] u Q(V) total |V | times degree(u) times Handshaking Lemma Q(E) implicit DECREASE-KEY’s. Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
16
Analysis of Prim (continued)
Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total array O(V) O(1) O(V2) binary heap O(lg V) O(E lg V) Fibonacci heap O(lg V) amortized O(1) O(E + V lg V) worst case
17
MST algorithms Kruskal’s algorithm (see CLRS):
Uses the disjoint-set data structure (Lecture 20). Running time = O(E lg V). Best to date: Karger, Klein, and Tarjan [1993]. Randomized algorithm. O(V + E) expected time.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.