Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2012:journals:jeanpaul:chapterfour_sectionv [2012/03/01 01:23] – [Designing the algorithm] mugabej | courses:cs211:winter2012:journals:jeanpaul:chapterfour_sectionv [2012/03/01 02:15] (current) – [Implementing Prim's Algorithm] mugabej | ||
|---|---|---|---|
| Line 11: | Line 11: | ||
| Three simple algorithms that efficiently finds the Minimum Spanning Tree:\\ | Three simple algorithms that efficiently finds the Minimum Spanning Tree:\\ | ||
| - | \\ | + | |
| === Prim's algorithm === | === Prim's algorithm === | ||
| Line 22: | Line 22: | ||
| * In each iteration: | * In each iteration: | ||
| * Grow S by one node, adding to S a node //v// whose cost = min< | * Grow S by one node, adding to S a node //v// whose cost = min< | ||
| - | * Also add the edge e =(u,v) that satisfies the above formula.\\ | + | * Also add the edge e =(u,v) that satisfies the above formula. |
| === Kruskal' | === Kruskal' | ||
| - | \\ | + | |
| * Start with T = φ,thus T = an empty tree | * Start with T = φ,thus T = an empty tree | ||
| * Consider edges in ascending order of cost | * Consider edges in ascending order of cost | ||
| * Insert edge e in T as long as e doesn' | * Insert edge e in T as long as e doesn' | ||
| * if e creates a cycle, we ignore it and move on to the next node | * if e creates a cycle, we ignore it and move on to the next node | ||
| - | \\ | + | |
| === Reverse-Delete algorithm === | === Reverse-Delete algorithm === | ||
| - | * Start with T = E | + | * Start with T = E, E the set of all the nodes in the graph |
| * Consider edges in descending order of cost | * Consider edges in descending order of cost | ||
| * Delete edge e from T unless doing so would disconnect T | * Delete edge e from T unless doing so would disconnect T | ||
| + | |||
| + | ==== Analyzing the Algorithms ==== | ||
| + | |||
| + | * The **Cut Property **: | ||
| + | * Assume all edge costs are distinct --> The MST is unique | ||
| + | * Now, let S be any subset of nodes that is neither empty nor equal to V | ||
| + | * And let edge e = (v,w) be the minimum cost edge with one end in S and the other in V-S | ||
| + | * Then an spanning tree contains the edge e | ||
| + | * This straight-out-of-the-book statement is the cut property of the Minimum Spanning Tree.Proof: | ||
| + | * So, we can insert nodes in the MST as long as we don't create cycle when doing so. | ||
| + | * Prim's and Kruskal' | ||
| + | * The **Cycle property**: | ||
| + | * Let C be any cycle, and let f be the maximum cost edge belonging to C | ||
| + | * Then MST does not contain f. | ||
| + | * So, we can delete a node from the MST as long as we don't disconnect our MST | ||
| + | * The Reverse-Delete Algorithm produces a Minimum Spanning Tree of the graph G | ||
| + | ==== Implementing Prim's Algorithm ==== | ||
| + | |||
| + | * We need to decide which node //v// to add next to the set S at each time | ||
| + | * We maintain the attachment costs a(//v//) = min< | ||
| + | * Keep the nodes in a Priority Queue(PQ) with a(//v//), the attachment costs, as the keys | ||
| + | * We use ** ExtractMin ** to extract the node //v// to add to S | ||
| + | * We use the ** ChangeKey ** operation to update the attachment costs | ||
| + | * The ** ExtractMin ** operation is performed n-1 times | ||
| + | * The ** ChangeKey ** operation is done at most once for each edge | ||
| + | * Thus using a PQ, we can implement Prim's algorithm in O(mlogn) time\\ | ||
| + | \\ | ||
| + | Implementation from our class' lecture:\\ | ||
| + | >>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| + | |||
| + | \\ | ||
| + | There are several applications of the Minimum Spanning Tree algorithms, many can be found in the book.\\ | ||
| + | This was by far the most interesting reading of the chapter!I give it a 9/10. | ||
