Skip to content

Dijkstra-Prim Method

Definition

The Dijkstra-Prim method constructs a minimum spanning tree in a connected undirected network with nonnegative edge lengths. Let UU be the set of vertices reached so far; start with UU containing one arbitrary vertex and no selected edges. Repeatedly select a shortest edge joining a vertex in UU to a vertex outside UU, add that edge, and add the outside endpoint to UU. Stop when every vertex belongs to UU.


Interpretation

The method grows one connected tree from a start vertex. Each step adds the cheapest edge that brings in a vertex not yet reached, so the selected edges never form a cycle and never fall apart into separate pieces.

A weighted network from A to F with the minimum spanning tree edges highlighted and numbered in the order they are chosen, and an oval around the vertices A, B and C reached after two steps.

The numbers in brackets give the order of selection when the method starts at AA: first A−CA-C (2), then C−BC-B (1). The oval marks U={A,B,C}U=\{A,B,C\} after these two steps. The edges leaving UU are B−DB-D (5), C−DC-D (8) and C−EC-E (10), so B−DB-D is chosen next; A−BA-B is no longer a candidate because both its ends are in UU. Then D−ED-E and E−FE-F follow, for a total length of 13.

Only edges between UU and the rest are compared: an edge inside UU would close a cycle, and an edge with neither end in UU would start a second piece. The cheapest crossing edge is a safe choice: every spanning tree uses at least one edge between UU and the rest, and one can show that a cheapest such edge always belongs to some minimum spanning tree.

The shortest path algorithm grows a set UU in the same way but compares d(u)+ℓuvd(u)+\ell_{uv}, the full distance from the origin, instead of the length of the edge u−vu-v alone. Mixing up the two is a common slip: the Dijkstra-Prim method minimizes the total length of the tree, not the distance from the start vertex.


Key Results

  • Compare edges crossing from the whole set UU, not only from the last added vertex.
  • Ties can be broken arbitrarily.
  • The starting vertex can change the construction order without changing the minimum total value.

Examples

In the figure, each green edge label gives its weight and selection step in brackets. After reaching U={A,B,C}U=\{A,B,C\}, BD=5BD=5 is cheaper than CD=8CD=8 or CE=10CE=10; ABAB is ineligible because both endpoints are already in UU. The five selected edges have total length 13.

  • In the Euclidean example, starting with U={1}U=\{1\} selects edge 1−71-7 of length 13\sqrt{13}.
  • The next edge is 1−41-4, of length 5.

Prerequisites

Sources

Aliases: Prim-Jarník algorithm, Jarník's algorithm, Prim's method Keywords: Prim's algorithm Symbols: square root

Last updated: 20 October 2026 03:02