COMS 482: unofficial class blog


Lecture 5: How to prove greedy algorithms

Posted in Class Notes by Elliott Back on February 2nd, 2005. [Del.icio.us]

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:

  1. |pi| < |mj| => contradiction, because we have made a less cost MST
  2. |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.”

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

    1. Make sure mj is connected
    2. 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.

Prim’s Algorithm Choosing Nodes

This entry was posted on Wednesday, February 2nd, 2005 at 12:25 pm and is tagged with , , , , , , , , , , , , , , , , , , , . 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'

  1. […] ains a proof that Kruskal’s algorithm produces an MST, lecture 5 proves the same for Prim’s algorithm, and lecture 6 carefully proves […]

Leave a Reply

Your blog, no spam: the thorough-paced Wordpress Anti-Spam Plugin.

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:

Supplied by Google Blog Search
  • There were no results.