0% found this document useful (0 votes)
65 views17 pages

Prim's Algorithm Time Complexity Analysis

Prim's algorithm is a method for finding the Minimum Spanning Tree (MST) of a graph by maintaining a priority queue of vertices and updating their keys based on the least-weight edges connecting them to the MST. The algorithm iteratively extracts the minimum vertex from the queue and updates the keys of its adjacent vertices. The time complexity of Prim's algorithm varies based on the data structure used for the priority queue, with the best case being O(E + V log V) using a Fibonacci heap.

Uploaded by

rodrigo.silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views17 pages

Prim's Algorithm Time Complexity Analysis

Prim's algorithm is a method for finding the Minimum Spanning Tree (MST) of a graph by maintaining a priority queue of vertices and updating their keys based on the least-weight edges connecting them to the MST. The algorithm iteratively extracts the minimum vertex from the queue and updates the keys of its adjacent vertices. The time complexity of Prim's algorithm varies based on the data structure used for the priority queue, with the best case being O(E + V log V) using a Fibonacci heap.

Uploaded by

rodrigo.silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.
© 2001 by Charles E. Leiserson
Example of Prim’s algorithm

A 
6 12
V–A
5 9
  
14 7
8 15
 0 
3 10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 
6 12
V–A
5 9
  
14 7
8 15
 0 
3 10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 
6 12
V–A
5 9
 7 
14 7
8 15
 0 15
3 10
10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 
6 12
V–A
5 9
 7 
14 7
8 15
 0 15
3 10
10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 12
6 12
V–A
5 9
5 7 9
14 7
8 15
 0 15
3 10
10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 12
6 12
V–A
5 9
5 7 9
14 7
8 15
 0 15
3 10
10

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
14 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
14 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
14 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
3 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
3 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
3 0 15
3 10
8

© 2001 by Charles E. Leiserson


Example of Prim’s algorithm

A 6
6 12
V–A
5 9
5 7 9
14 7
8 15
3 0 15
3 10
8

© 2001 by Charles E. Leiserson


Analysis of Prim
QV
Q(V) key[v]   for all v  V
total key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
|V | for each v  Adj[u]
times degree(u) do if v  Q and w(u, v) < key[v]
times then key[v]  w(u, v)
p[v]  u
Handshaking Lemma  Q(E) implicit DECREASE-KEY’s.
Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
© 2001 by Charles E. Leiserson
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(lg V) O(E lg V)
Fibonacci O(lg V) O(1) O(E + V lg V)
heap amortized amortized worst case

© 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.

© 2001 by Charles E. Leiserson

You might also like