prim’s algorithm
DESCRIPTION
Prim’s algorithm. I DEA : 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 E XTRACT -M IN ( Q ) - PowerPoint PPT PresentationTRANSCRIPT
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
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
At the end, {(v, [v])} forms the MST.
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Example of Prim’s algorithm
A V – A
00
6 125
14
3
8
10
15
9
7
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Handshaking Lemma (E) implicit DECREASE-KEY’s.
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)[v] u
Analysis of Prim
degree(u)times
|V | times
(V) total
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
Analysis of Prim (continued)
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
Q TEXTRACT-MIN TDECREASE-KEYTotal
array O(V) O(1) O(V2)
binary heap O(lg V) O(lg V) O(E lg V)
Fibonacci heap
O(lg V)amortized
O(1)amortized
O(E + V lg V)worst case
© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson
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.