0% found this document useful (0 votes)
55 views

Minimum Spanning Trees: Fast Implementation of Prim's Algorithm

- Prim's algorithm finds a minimum spanning tree of a graph. It maintains a set X of vertices whose minimum spanning tree has been determined so far and a heap data structure. - The heap stores vertices in V-X and their key values, which are the costs of the cheapest edge from that vertex to X. This allows extracting the minimum key in O(log n) time each iteration. - Each iteration extracts the minimum key vertex v from the heap, adds it to X, and updates keys of neighbors of v still in the heap. This takes overall O(m log n) time, faster than a naive implementation.

Uploaded by

sirj0_hn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Minimum Spanning Trees: Fast Implementation of Prim's Algorithm

- Prim's algorithm finds a minimum spanning tree of a graph. It maintains a set X of vertices whose minimum spanning tree has been determined so far and a heap data structure. - The heap stores vertices in V-X and their key values, which are the costs of the cheapest edge from that vertex to X. This allows extracting the minimum key in O(log n) time each iteration. - Each iteration extracts the minimum key vertex v from the heap, adds it to X, and updates keys of neighbors of v still in the heap. This takes overall O(m log n) time, faster than a naive implementation.

Uploaded by

sirj0_hn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Minimum

Spanning Trees

Fast Implementation
Algorithms: Design
and Analysis, Part II of Prim’s Algorithm
Running Time of Prim’s Algorithm

- Initialize X = {s} [s ∈ V chosen arbitrarily]


- T = ∅ [invariant: X = vertices spanned by tree-so-far T ]
- While X 6= V
- Let e = (u, v ) be the cheapest edge of G with u ∈ X , v ∈
/ X.
- Add e to T , add v to X .

Running time of straightforward implementation:


- O(n) iterations [where n = # of vertices]
- O(m) time per iteration [where m = # of edges]
⇒ O(mn) time

BUT CAN WE DO BETTER?

Tim Roughgarden
Prim’s Algorithm with Heaps
[Compare to fast implementation of Dijkstra’s algorithm]
Invariant #1: Elements in heap = vertices of V − X .
Invariant #2: For v ∈ V − X , key[v ] = cheapest edge (u, v ) with
i ∈ X (or +∞ if no such edges exist).
2 key[v]=2
X V −X
4
v
5

Check: Can initialize heap with O( m + n log n ) = O(m log n)


preprocessing.
To compare keys n − 1 Inserts m ≥ n − 1 since G connected

Note: Given invariants, Extract-Min yields next vertex v ∈ / X and


edge (u, v ) crossing (X , V − X ) to add to X and T , respectively.
Tim Roughgarden
Quiz: Issue with Invariant #2
Question: What is: (i) current value of key[v ] (ii) current value of
key[w ] (iii) value of key[w ] after one more iteration of Prim’s
algorithm?
key[w]=10

10 new key[w]=1
w

2
1
4
v
5

new X
key[v]=2
X V −X

A) 11, 10, 4 C) 2, 10, 1


B) 2, 10, 10 D) 2, 10, 2
Tim Roughgarden
Maintaining Invariant #2
Issue: Might need to recompute some keys to maintain Invariant
#2 after each Extract-Min.

X V −X
Pseudocode: When v added to X :
- For each edge (v , w ) ∈ E :
- If w ∈ V − X → The only whose key might have changed
(Update key if needed:)
- Delete w from heap
- Recompute key[w ]:=min{key[w ],cvw }
- Re-Insert into heap
Subtle point/exercise:
Think through book-keeping needed to pull this off
Tim Roughgarden
Running Time with Heaps
- Dominated by time required for heap operations
- (n − 1) Inserts during preprocessing
- (n − 1) Extract-Mins (one per iteration of while loop)
- Each edge (v , w ) triggers one Delete/Insert combo
[When its first endpoint is sucked into X ]
⇒ O(m) heap operations [Recall m ≥ n − 1 since G connected]
⇒ O(m log n) time [As fast as sorting!]

Tim Roughgarden

You might also like