learngraphtheory.org

Interactive Graph Theory Learning

Guest User

Using app without sign in

Algorithm Selection

Prim's MST Algorithm

Finds minimum spanning tree by growing from a single vertex

Time: O((V + E) log V)
Space: O(V)
Use Case: Network design, clustering, approximation algorithms

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Prim's MST Algorithm

Prim's algorithm builds a minimum spanning tree (MST) of a weighted undirected graph, the subset of edges that connects every vertex with the smallest possible total weight. It grows a single tree outward from an arbitrary start vertex, always attaching the cheapest edge that reaches a new vertex.

How it works

The algorithm keeps a priority queue of edges that cross from the tree to the rest of the graph. At each step it extracts the minimum-weight crossing edge, adds its new endpoint to the tree, and inserts that vertex's edges into the queue. The cut property of MSTs guarantees each chosen edge belongs to some minimum spanning tree. With a binary heap the running time is O(E log V).

Applications

Prim's algorithm designs low-cost networks: electrical grids, fiber and telecom layouts, water pipelines, and chip wiring. It also supports clustering and image segmentation. Interviews often pair it with Kruskal's algorithm to test understanding of greedy correctness arguments.

Read the full article: Minimum Spanning Trees: Prim, Kruskal and Boruvka

Related algorithms: Kruskal's MST Algorithm, Borůvka's Algorithm, Dijkstra's Algorithm

Interactive Controls
Basic Actions
Double Click → Add Node
Drag → Move Nodes
Shift + Click → Connect Nodes
Right Click → Context Menu
Advanced
Ctrl + Click → Multi-Select
Delete Key → Remove Selected
Double Click Edge → Edit Weight
Ctrl + Drag → Pan View

Zoom Controls

100%
Nodes: 4
Edges: 4