Tree edges
Total length
Edges checked

  • Current
  • In the tree
  • Next to the tree
  • Best edge so far

More about Prim's algorithm

Prim's algorithm

Prim's algorithm builds a minimum spanning tree, the cheapest set of edges that connects every node, by growing a single tree from a start. At every step it adds the node that can be reached by the shortest edge from the tree. Click a node to start there and step through to see the waiting nodes, their best edges, and the tree grow. Press Play to watch the tree grow, or Step and Back to go one step at a time. Drag the step slider to jump anywhere. Click a node to make it the start: the tree grows from there. The number on each edge is its length. Under the graph are the nodes waiting to join, each with the length of its best edge to the tree, which is drawn dashed. New graph makes another graph; choose how many nodes and edges it has. With the board selected, the arrow keys step, the space bar plays and pauses, and Home and End jump to the start and the end.

How it works

  1. Click a node to make it the start: the tree grows from there.
  2. Press Play, or use Step and Back to move one step at a time. Drag the step slider to jump anywhere.
  3. Watch the waiting nodes under the graph, each with its best edge to the tree. New graph makes another graph to try.

How Prim's algorithm works

Start with a tree of one node. Every node outside the tree remembers its shortest edge to the tree, if it has one. Repeatedly take the node whose edge is the shortest, add it and that edge to the tree, and look at its edges: if one is shorter than a neighbour's best edge so far, it becomes that neighbour's best edge. When every node has joined, the tree has one edge fewer than there are nodes, and its total length is as small as possible.

Prim and Dijkstra

Prim's algorithm and Dijkstra's algorithm look almost the same: both grow a region from a start and keep the waiting nodes in a priority queue. The difference is the number in the queue. Dijkstra keeps each node's whole distance from the start; Prim keeps only the length of its single best edge to the tree. So Dijkstra builds a tree of shortest paths, and Prim a tree of the smallest total length, and the two trees are often different.

Frequently asked questions

What is Prim's algorithm?

A greedy algorithm that builds a minimum spanning tree of a weighted graph by growing one tree from a start node, always adding the shortest edge that connects the tree to a node outside it.

Does the start node matter?

Only for the order: from any start, Prim's algorithm ends with a spanning tree of the same smallest total length. When some edges have equal lengths, the tree itself may differ.

Who invented Prim's algorithm?

Vojtěch Jarník described it first, in 1930. Robert Prim rediscovered it in 1957 and Edsger Dijkstra in 1959, so it is also called Jarník's algorithm or the Prim–Jarník algorithm.

How fast is Prim's algorithm?

With a binary heap, the work grows with E log V for V nodes and E edges. With a simple array instead of a heap it takes about V² steps, which is better for dense graphs where almost every pair of nodes is joined.

What is the difference between Prim's and Kruskal's algorithm?

Prim's algorithm grows one tree from a start, always adding the shortest edge to a new node. Kruskal's algorithm sorts all edges and adds the shortest one that does not close a cycle, joining separate pieces. Both are greedy and reach the same total length; Prim suits dense graphs, Kruskal sparse ones and graphs that are not connected.

Prim 最小生成樹: how do I record a video?

Press the red Record button in the playback bar: it films what the stage shows. Press it again to stop, and the video is saved to your device.Recording is free; you only need to sign in. Without a subscription the video carries a small PictorX logo in a corner.The button beside Record opens the video options: the shape (as shown, square, story 9:16 or wide 16:9), the length (until you stop, or 5, 10, 15 or 30 seconds for social media) and the file: MP4, WebM or an animated GIF.You can add one of three music tracks: calm, bright or beat.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

前往編輯器