Resource · Guide or article
Prim's Algorithm
Describes Prim's greedy minimum spanning tree algorithm: start anywhere and keep attaching the cheapest edge leaving the tree.
Reviewed
Open the original: Prim's Algorithm (opens the original site in a new tab)Practice this topicOpens the practice interviewer with a question on Graphs.
Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
In computer science, Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. Prim's algorithm starting at vertex A. In the third step, edges BD and AB both have weight 2, so BD is chosen arbitrarily. After that step, AB is no longer a candidate for addition to the tree because it links two nodes that are already in the tree.
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- Vertex-by-vertex growth from an arbitrary start
- A caption explaining a tie between edges BD and AB, and why AB then drops out
- MST and minimum spanning forest definitions, with an example of two equally minimal trees
How to use it
PrepKit study advice, not a claim made by the source.
Code Prim's with a min-heap and check its total weight on a test graph, then say when you would prefer it for dense graphs.
Scope and limitations
No data-structure choice, complexity or text walkthrough beyond one caption; the MST section is general background.
Access and format
Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/graph/prim/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.