Prim Algorithm Visualizer

Paste undirected weighted edges, choose a start node, then step through Prim's frontier edge choices.

Nodes
6
Visited
1
Selected
0
Weight
0

Weighted graph input

Use one undirected edge per line: node A, node B, weight.

Step controls

Start at A. Add its 3 frontier edge(s).

Minimum spanning tree graph

Green edges are selected. Amber edges are in the frontier. Blue is under review.

Accepted Frontier Checking
Prim MST graph diagramUndirected weighted graph with 6 nodes and 10 edges.4231546237AvisitedBunvisitedCunvisitedDunvisitedEunvisitedFunvisited

Frontier queue

EdgeWeightStatus
A-F2frontier
A-C3frontier
A-B4frontier

All edges

B-C1pending
A-F2frontier
D-E2pending
A-C3frontier
E-F3pending
A-B4frontier
C-D4pending
B-D5pending
C-E6pending
D-F7pending

MST output

start = A
visited = 1
selected = 0
total_weight = 0
No MST edges selected yet.

What is a Prim Algorithm Visualizer?

A Prim algorithm visualizer shows how a minimum spanning tree grows from one start node by repeatedly choosing the cheapest frontier edge. It is useful for learning MST algorithms, greedy graph choices, priority queues, and weighted graph interview problems.

This tool lets you paste undirected weighted edges, choose a start node, then step through each frontier edge choice while watching the selected tree expand.

For the full graph and data structure set, browse the Data Structure Visualizers hub.

How to use this Prim visualizer

  • Paste one undirected weighted edge per line, such as A B 4.
  • Choose the node where Prim should start.
  • Click Run Prim to jump to the completed MST or forest component.
  • Step forward to see the next cheapest frontier edge.
  • Green edges are accepted into the MST, while blue marks the current edge under review.

Prim vs Kruskal

Prim grows one connected tree from a start node. Kruskal sorts every edge globally and accepts edges that connect different components. Both can produce a minimum spanning tree, but they expose different greedy choices.

Compare this page with the Kruskal Algorithm Visualizer to see the global edge-sorting approach. For direct disjoint-set operations, use the Union Find Visualizer.

Frequently Asked Questions

What input format does this Prim visualizer use?
Paste one undirected weighted edge per line with two endpoints and a weight, such as A B 4.
What does Prim's algorithm find?
Prim finds a minimum spanning tree for the connected component reachable from the chosen start node.
How does Prim choose the next edge?
Prim keeps a frontier of edges leaving the visited tree and repeatedly accepts the lowest-weight edge that reaches an unvisited node.
Can Prim handle disconnected graphs?
Prim only grows the component reachable from the start node. A disconnected graph needs another start node to cover another component.

Related tools