Loading algorithms…
Loading visualizer…
Grow a minimum spanning tree one vertex at a time using a min-priority queue — O(E log V).
Prim's algorithm builds a Minimum Spanning Tree (MST) by starting at a chosen root node and repeatedly attaching the cheapest edge that connects the growing tree to a vertex outside it.
The naive "scan every frontier edge on every step" approach is O(V²). Using a binary min-heap or a Fibonacci heap for the priority queue reduces the total cost to O(E log V), making Prim's practical on large dense graphs.
💡 Cut property: For any cut of the graph (a partition of nodes into two non-empty sets), the minimum-weight edge crossing that cut belongs to some MST. Prim's repeatedly exploits this cut property by treating the growing tree as one side of the cut and the frontier as the other.
Pick a root (or click a node) · Watch the tree grow via the cheapest reachable edge · Frontier edges show the priority queue