Lecture 5: How to prove greedy algorithms
We have two Minimal Spanning Tree algorithms: Kruskal’s (many trees) and Prim’s (single tree). Since we covered the proof of Kruskal’s algorithm in the previous lecture, we’d like to show that Prim’s algorithm produces an MST.
Setup of proof:
TPrim = {p1, … , pn-1}
TMST = {m1, … , mn-1}
Basic strategy: assume that the two trees are different, and try to get a contradiction. Let pi be an edge not in TMST. If we add pi to the MST, we get a cycle. If we add pi then take out mj from the cycle we still have a spanning tree.
Specific cases:
- |pi| < |mj| => contradiction, because we have made a less cost MST
- |pi| = |mj| => we’ve built a new MST that uses pi: sloppy.
The easiest way to resolve this is define pi so that it can’t fit into any MST, so that then there is a contradiction. Just add, “Let pi be the first Prim edge that cannot be included with {p1, … , pi-1} in any MST.”
- |pi| > |mj| => want to say that we should have picked mj
Because pi was picked by Prim’s algorithm, one of pi’s endpoints is in the tree TPrim at step i-1.
- Make sure mj is connected
- and not already in the tree
Walk around the edges of cycle, passing edges in {p1, … , pi-1}. The next edge after these is connected, too. mj has to exist because entire cycle cannot be edges of {p1, … , pi}.
Implementation details:
We need to keep a priority queue (PQ) of vertices waiting to be added. We need extract_min and update_priority functions in O(log n) time, which can be done easily with a min/max heap. For each vertex, its priority is the length of the edge connecting it to the tree. Therefore, the runtime (asymptotic complexity) of Prim’s algorithm is O(m log n) with m edges and n vertices. Compare to Kruskal’s algorithm.
This entry was posted on Wednesday, February 2nd, 2005 at 12:25 pm and is tagged with pi lt, asymptotic complexity, minimal spanning tree, tmst, priority queue, implementation details, kruskal, tree algorithms, two trees, single tree, vertices, endpoints, mj, contradiction, vertex, algorithm, p1, heap, pq, proof. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.
One Response to 'Lecture 5: How to prove greedy algorithms'
Leave a Reply
Please take time to enjoy the archives: May 2005 (1) April 2005 (11) March 2005 (11) February 2005 (15) January 2005 (7)
Fresh, related resources:
- There were no results.

on May 14th, 2005 at 9:42 pm
[…] ains a proof that Kruskal’s algorithm produces an MST, lecture 5 proves the same for Prim’s algorithm, and lecture 6 carefully proves […]