|
| 1 | +# Prim's Algorithm |
| 2 | + |
| 3 | +In computer science, **Prim's algorithm** is a greedy algorithm that |
| 4 | +finds a minimum spanning tree for a weighted undirected graph. |
| 5 | + |
| 6 | +The algorithm operates by building this tree one vertex at a |
| 7 | +time, from an arbitrary starting vertex, at each step adding |
| 8 | +the cheapest possible connection from the tree to another vertex. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +Prim's algorithm starting at vertex `A`. In the third step, edges |
| 13 | +`BD` and `AB` both have weight `2`, so `BD` is chosen arbitrarily. |
| 14 | +After that step, `AB` is no longer a candidate for addition |
| 15 | +to the tree because it links two nodes that are already |
| 16 | +in the tree. |
| 17 | + |
| 18 | +## Minimum Spanning Tree |
| 19 | + |
| 20 | +A **minimum spanning tree** (MST) or minimum weight spanning tree |
| 21 | +is a subset of the edges of a connected, edge-weighted |
| 22 | +(un)directed graph that connects all the vertices together, |
| 23 | +without any cycles and with the minimum possible total edge |
| 24 | +weight. That is, it is a spanning tree whose sum of edge weights |
| 25 | +is as small as possible. More generally, any edge-weighted |
| 26 | +undirected graph (not necessarily connected) has a minimum |
| 27 | +spanning forest, which is a union of the minimum spanning |
| 28 | +trees for its connected components. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +A planar graph and its minimum spanning tree. Each edge is |
| 33 | +labeled with its weight, which here is roughly proportional |
| 34 | +to its length. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +This figure shows there may be more than one minimum spanning |
| 39 | +tree in a graph. In the figure, the two trees below the graph |
| 40 | +are two possibilities of minimum spanning tree of the given graph. |
| 41 | + |
| 42 | +## References |
| 43 | + |
| 44 | +- [Minimum Spanning Tree on Wikipedia](https://en.wikipedia.org/wiki/Minimum_spanning_tree) |
| 45 | +- [Prim's Algorithm on Wikipedia](https://en.wikipedia.org/wiki/Prim%27s_algorithm) |
| 46 | +- [Prim's Algorithm on YouTube by Tushar Roy](https://www.youtube.com/watch?v=oP2-8ysT3QQ) |
0 commit comments