Question? Leave a message!




Prim’s algorithm

Prim’s algorithm
Dr.SethPatton Profile Pic
Dr.SethPatton,United Kingdom,Teacher
Published Date:22-07-2017
Website URL
Comment
ECE 250 Algorithms and Data Structures Prim’s algorithm Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharderalumni.uwaterloo.ca © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.Prim’s algorithm 2 Outline This topic covers Prim’s algorithm: – Finding a minimum spanning tree – The idea and the algorithm – An examplePrim’s algorithm 3 Strategy Suppose we take a vertex – Given a single vertex e , it forms a minimum spanning tree on one 1 vertex v 1Prim’s algorithm 4 Strategy Add that adjacent vertex v that has a connecting edge e of 2 1 minimum weight – This forms a minimum spanning tree on our two vertices and e must be 1 in any minimum spanning tree containing the vertices v and v 1 2 v 2 e 1 v 1Prim’s algorithm 5 Strategy Strategy: – Suppose we have a known minimum spanning tree on k n vertices – How could we extend this minimum spanning tree?Prim’s algorithm 6 Strategy Add that edge e with least weight that connects this minimum k spanning tree to a new vertex v k + 1 – This does create a minimum spanning tree on k + 1 nodes—there is no other edge we could add that would connect this vertex – Does the new edge, however, belong to the minimum spanning tree on all n vertices? v k + 1 e kPrim’s algorithm 7 Strategy Suppose it does not – Thus, vertex v is connected to the minimum spanning tree via k + 1 another sequence of edges v k + 1 e kPrim’s algorithm 8 Strategy Because a minimum spanning tree is connected, there must be a path from vertex v back to our existing minimum spanning tree k + 1  – It must be connected along some edge e v k + 1 e k ePrim’s algorithm 9 Strategy Let w be the weight of this minimum spanning tree – Recall, however, that when we chose to add v , it was because e was k + 1 k the edge connecting an adjacent vertex with least weight ee – Therefore where e represents the weight of the edge e k  ee  0 k v k + 1 e k ePrim’s algorithm 10 Strategy Consider, however, suppose we swap edges and instead choose to  e include e and exclude k – The result is still a minimum spanning tree, but the weight is now  w e e w k1 v k + 1 e k + 1 ePrim’s algorithm 11 Strategy  e Thus, by swapping e for , we have a spanning tree that has less k  e weight than the so-called minimum spanning tree containing  e – This contradicts our assumption that the spanning tree containing was minimal – Therefore, our minimum spanning tree must contain e k v k + 1 e k ePrim’s algorithm 12 Strategy Recall that we did not prescribe the value of k, and thus, k could be any value, including k = 1 v k + 1 e k ePrim’s algorithm 13 Strategy Recall that we did not prescribe the value of k, and thus, k could be any value, including k = 1 – Given a single vertex e , it forms a minimum spanning tree on one 1 vertex v 1Prim’s algorithm 14 Strategy Add that adjacent vertex v that has a connecting edge e of 2 1 minimum weight – This forms a minimum spanning tree on our two vertices and e must be 1 in any minimum spanning tree containing the vertices v and v 1 2 v 2 e 1 v 1Prim’s algorithm 15 Minimum Spanning Trees Prim’s algorithm for finding the minimum spanning tree states: – Start with an arbitrary vertex to form a minimum spanning tree on one vertex – At each step, add that vertex v not yet in the minimum spanning tree that has an edge with least weight that connects v to the existing minimum spanning sub-tree – Continue until we have n– 1 edges and n vertices Another possibility is Kruskal’s algorithmPrim’s algorithm 16 Prim’s Algorithm Associate with each vertex three items of data: – A Boolean flag indicating if the vertex has been visited, – The minimum distance to the partially constructed tree, and – A pointer to that vertex which will form the parent node in the resulting tree For example: – Add three member variables to the vertex class – Track three tablesPrim’s algorithm 17 Prim’s Algorithm Initialization: – Select a root node and set its distance as 0 – Set the distance to all other vertices as ∞ – Set all vertices to being unvisited – Set the parent pointer of all vertices to 0Prim’s algorithm 18 Prim’s Algorithm Iterate while there exists an unvisited vertex with distance ∞ – Select that unvisited vertex with minimum distance – Mark that vertex as having been visited – For each adjacent vertex, if the weight of the connecting edge is less than the current distance to that vertex: • Update the distance to equal the weight of the edge • Set the current vertex as the parent of the adjacent vertexPrim’s algorithm 19 Prim’s Algorithm Halting Conditions: – There are no unvisited vertices which have a distance ∞ If all vertices have been visited, we have a spanning tree of the entire graph If there are vertices with distance ∞, then the graph is not connected and we only have a minimum spanning tree of the connected sub- graph containing the rootPrim’s algorithm 20 Prim’s Algorithm Let us find the minimum spanning tree for the following undirected weighted graph