Preview

Prim's Algorithm

Good Essays
Open Document
Open Document
625 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Prim's Algorithm
Prim’s algorithm Prim’s algorithm is an algorithm used to find out the minimum spanning tree. The cost or value of all the paths should be positive. For a given source vertex, this algorithm calculates the minimum distance that should be taken to all the nodes from the source vertex. Algorithm of Prim’s algorithm: 1. Initialize two sets S and V-S, where the set S will have the start vertex and V-S will have all the remaining vertexes. 2. Starting from the source vertex, traverse the adjacent vertexes and the adjacent vertex having the shortest path traversal should be considered and enter that vertex in the set S. 3. Make two containers d[v] and p[v], where Contains the length of the final edge and contains the predecessor of v in the path from s to v. 4. Repeat the step 2 and 3 until the set V-S becomes empty and make changes in the table if there exists another path from source node to another node. Consider the figure given below:

Consider the Philadelphia as the starting node. As this node is connected with only one node Pittsburgh with a distance of 320. So, a connection should be made between both nodes. Set S will have { Philadelphia, Pittsburgh } and Set V-S will have { Toledo, Detroit, Ann Arbor, Chicago, Ft. Wayne, Indianapolis, Cleveland, Columbus }. This can be shown as:

Now ,select the smallest distance edge form seleced nodes. At this stage set S will have { Philadelphia, Pittsburgh, Cleveland } and Set V-S will have {Toledo, Detroit, Ann Arbor, Chicago, Ft. Wayne, Indianapolis, Columbus }. At this stage it is Cleveland and will be connected to Pittsburg.

Now ,select the smallest distance edge form selected nodes. At this stage set S will have { Philadelphia, Pittsburgh, Cleveland, Toledo} and Set V-S will have { Detroit, Ann Arbor, Chicago, Ft. Wayne, Indianapolis, Columbus }. At this stage it is Toledo and will be connected to Cleveland.

Now ,select the smallest distance edge

You May Also Find These Documents Helpful