![prim algorithm prim algorithm](https://image1.slideserve.com/1903885/prim-s-algorithm-from-a-matrix3-l.jpg)
Here, the only vertices available are C and G. E is nearest, so we highlight the vertex E and the arc BE. C is 8 away from B, E is 7 away from B, and G is 11 away from F.
![prim algorithm prim algorithm](https://static.javatpoint.com/core/images/prims-algorithm-java.png)
In this case, we can choose between C, E, and G. Vertex B, which is 7 away from A, is highlighted. F is the smallest distance away, so we highlight the vertex F and the arc DF. B is 9 away from D and 7 away from A, E is 15, and F is 6. The next vertex chosen is the vertex nearest to either D or A. A is the vertex nearest to D and will be chosen as the second vertex along with the edge AD. Vertices A, B, E and F are connected to D through a single edge. Vertex D has been arbitrarily chosen as a starting point. The numbers near the edges indicate their weight. Output: V new and E new describe a minimal spanning tree Initialize: V new = Ĭhoose edge (u,v) with minimal weight such that u is in V new and v is not (if there are multiple edges with the same weight, choose arbitrarily but consistently) Input: A connected weighted graph with vertices V and edges E. The algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices. Therefore it is sometimes called the DJP algorithm, the Jarník algorithm, or the Prim-Jarník algorithm. Prim in 1957 and rediscovered by Edsger Dijkstra in 1959. The algorithm was developed in 1930 by Czech mathematician Vojtech Jarnik and later independently by computer scientist Robert C. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. Prim's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph.