Growing a Tree from a Starting Vertex
A minimum spanning tree connects all vertices of a connected weighted graph with minimum possible total edge weight. Kruskal’s algorithm builds one by considering edges across the graph. Prim’s algorithm takes a different approach: it starts at one vertex and grows a single connected tree, adding one new vertex at a time. At each step, it chooses a lightest edge from the vertices already reached to a vertex not yet reached.
Throughout, graphs are finite, simple, undirected and weighted, with real edge weights. The weight of a set of edges is the sum of their weights. A tree is a connected graph with no cycles; a spanning tree of \(G\) is a subgraph that is a tree and contains every vertex of \(G\). A minimum spanning tree is a spanning tree of minimum total weight. The algorithm permits negative weights as well as zero and positive weights.
An edge with exactly one endpoint in \(S\) is called a crossing edge for the cut \((S,V\setminus S)\). Only crossing edges are eligible. An edge with both endpoints in \(S\) cannot bring in a new vertex; an edge with both endpoints outside \(S\) does not touch the growing tree. Because each chosen edge adds a new vertex, the selected edges do not create a cycle.
The process assumes that a crossing edge exists whenever \(S\ne V\). This is guaranteed if the graph is connected. If the graph is disconnected, a run from \(r\) can reach only the connected component containing \(r\), and it stops when that component has been reached.
The Selection Rule in Practice
At every step, compare the weights of all crossing edges, not just the edges incident to the most recently added vertex. An edge from any vertex already in \(S\) may be the next choice. The following procedure records enough information to make each decision auditable.
Set \(S=\{r\}\) and \(T=\varnothing\). The starting vertex is the only reached vertex, and no edge has yet been selected.
Write down every edge with one endpoint in \(S\) and the other in \(V\setminus S\). Edges with both ends in either set are excluded.
Choose an eligible edge of minimum weight, add it to \(T\), and add its previously unreached endpoint to \(S\).
Recompute the crossing edges after each update. Stop when all vertices have been reached, or when no crossing edge exists.
The cut property, established in the earlier material on cuts in weighted graphs, explains why this local choice is useful: a minimum-weight edge crossing a cut is safe, meaning that there is a minimum spanning tree containing that edge. Prim’s algorithm applies the property to the cut defined by its current reached set. The traces below focus on carrying out the selection rule; a full correctness proof is a separate question.
Worked Example: Growing a Tree on Five Vertices
Let \(G=(V,E)\), where \(V=\{A,B,C,D,E\}\) and \(E=\{AB,AC,BC,BD,CD,CE,DE\}\). Give the edges weights \(w(AB)=4\), \(w(AC)=2\), \(w(BC)=1\), \(w(BD)=5\), \(w(CD)=8\), \(w(CE)=10\), and \(w(DE)=2\). Run Prim’s algorithm from \(A\).
| Step | Reached set before choice | Crossing edges with weights | Choice |
|---|---|---|---|
| 1 | \(\{A\}\) | \(AB:4,\ AC:2\) | \(AC:2\) |
| 2 | \(\{A,C\}\) | \(AB:4,\ BC:1,\ CD:8,\ CE:10\) | \(BC:1\) |
| 3 | \(\{A,B,C\}\) | \(BD:5,\ CD:8,\ CE:10\) | \(BD:5\) |
| 4 | \(\{A,B,C,D\}\) | \(CE:10,\ DE:2\) | \(DE:2\) |
For example, at step 2, \(AB\) is still eligible because \(A\in S\) and \(B\notin S\). The edges \(AC\) and \(BC\) are not eligible then: both endpoints of each are already in \(S\). After the four choices, \(S=\{A,B,C,D,E\}\) and \(T=\{AC,BC,BD,DE\}\).
Check: the selected edges connect all five vertices: \(A\) is joined to \(C\), \(C\) to \(B\), \(B\) to \(D\), and \(D\) to \(E\). Their total weight is \(2+1+5+2=10\). There are \(5-1=4\) selected edges, and the step-by-step construction adds a new vertex with each edge, so the result is a spanning tree.
Ties and the Starting Vertex
The minimum crossing edge need not be unique. In a tie, the algorithm may choose any edge attaining the minimum; different tie-breaking choices can produce different trees. A starting vertex is part of the run’s specification, but it does not mean that every later edge must touch that vertex. The only restriction is that each selected edge crosses the current reached set.
Worked Example: Tied Minimum Choices
Let \(V=\{1,2,3,4\}\) and \(E=\{12,13,23,24,34\}\), with weights \(w(12)=0\), \(w(13)=0\), \(w(23)=2\), \(w(24)=3\), and \(w(34)=3\). Start at vertex \(1\). When tied, choose \(12\) first and \(24\) at the final step if that tie occurs.
| Step | Reached set before choice | Crossing edges with weights | Choice |
|---|---|---|---|
| 1 | \(\{1\}\) | \(12:0,\ 13:0\) | \(12:0\) (tie) |
| 2 | \(\{1,2\}\) | \(13:0,\ 23:2,\ 24:3\) | \(13:0\) |
| 3 | \(\{1,2,3\}\) | \(24:3,\ 34:3\) | \(24:3\) (tie) |
The resulting edge set is \(T=\{12,13,24\}\). Its total weight is \(0+0+3=3\). If step 3 instead selects \(34\), the selected edges are \(\{12,13,34\}\), also with total weight \(0+0+3=3\). In either case each step adds a new vertex, and all four vertices are reached.
Check: the first tie is verified by comparing the two available weights, \(0=0\); the final tie is verified by \(3=3\). The two possible outputs are different edge sets, but each connects all four vertices with three edges and has total weight \(3\). This example illustrates that the selection rule does not prescribe a unique output when the minimum is tied.
What Happens in a Disconnected Graph?
If the graph is disconnected, there is no spanning tree: no connected subgraph can contain vertices from separate components, because there is no edge joining those components. Prim’s algorithm from a specified starting vertex still makes sense, but it stops after reaching the starting vertex’s entire component. In particular, an isolated starting vertex has no eligible edge and the run stops immediately.
Worked Example: A Run That Cannot Span the Graph
Let \(V=\{p,q,r,s,t\}\) and \(E=\{pq,qr,pr,st\}\), with weights \(w(pq)=1\), \(w(qr)=4\), \(w(pr)=7\), and \(w(st)=2\). Start at \(p\).
| Step | Reached set before choice | Crossing edges with weights | Choice |
|---|---|---|---|
| 1 | \(\{p\}\) | \(pq:1,\ pr:7\) | \(pq:1\) |
| 2 | \(\{p,q\}\) | \(pr:7,\ qr:4\) | \(qr:4\) |
| Stop | \(\{p,q,r\}\) | No crossing edges | None |
After the second choice, \(p,q,r\) have all been reached. The edge \(pr\) is not a crossing edge because both its endpoints are reached. The edge \(st\) is not a crossing edge because neither endpoint is reached. Thus the run stops with \(T=\{pq,qr\}\), of total weight \(1+4=5\). Vertices \(s\) and \(t\) are outside the reached set, so this is not a spanning tree of the full graph.
Check: the graph has two components, \(\{p,q,r\}\) and \(\{s,t\}\), and no edge joins them. Therefore no spanning tree of all five vertices exists. The run has instead produced a tree on the component containing its starting vertex.
Why the Process Produces a Tree
Proof. Let \(S\) be the reached set and \(T\) the selected edge set. Initially, \(S=\{r\}\) and \(T=\varnothing\); the selected subgraph on \(S\) is connected and has no cycle. Suppose the algorithm has made \(k\) choices. Each choice adds exactly one vertex not previously in \(S\), so \(|S|=k+1\). The new edge joins that vertex to a vertex already in \(S\). It therefore keeps the selected subgraph connected. It cannot create a cycle, because its new endpoint was not in the previous subgraph. Inductively, the selected subgraph on \(S\) is a tree with \(k\) edges.
If the original graph is connected and \(S\ne V\), choose a vertex outside \(S\). A path from \(r\) to that vertex must contain an edge with one endpoint in \(S\) and the other outside \(S\). Thus an eligible edge exists, and the process adds one more vertex. Since the graph is finite, this continues until \(S=V\), after exactly \(|V|-1\) choices. The selected subgraph is then connected, acyclic, and contains every vertex: it is a spanning tree. For a disconnected graph, an edge from the reached set to a vertex outside the component of \(r\) cannot exist, while every vertex in that component can be reached along a path from \(r\). Hence the run stops with exactly that component reached. If the component consists only of \(r\), it stops at initialization, with zero choices. \(\square\)
This proposition verifies the shape and stopping behavior of the output. The minimum-weight selection has an additional role: by the cut property, each chosen lightest crossing edge is safe for the current cut. The selected tree’s global minimum-weight guarantee requires connecting these local choices across the run; that full correctness argument belongs with the dedicated proof of Prim’s algorithm.
Common Mistakes
Choosing only from the newest vertex is not Prim’s rule. In the first example, after choosing \(AC\), the reached set is \(\{A,C\}\). The eligible edges are \(AB,BC,CD,CE\); the lightest is \(BC\) of weight \(1\). Looking only at edges incident to the newest vertex \(C\) happens to include \(BC\), but in other steps it can miss the best edge from an older reached vertex. Always compare all crossing edges.
Do not keep an edge whose endpoints are both reached in the candidate list. For example, after the first step of the five-vertex run, \(AC\) has both endpoints in \(S\), so it is ineligible even though it is an edge of small weight. Adding it would not bring in a new vertex and could create a cycle.
Do not treat a disconnected run as a spanning tree. If the algorithm stops while \(S\ne V\), inspect whether any crossing edge remains. No such edge means the starting component has been exhausted; it does not mean the unreached vertices have somehow been included.
Check Your Understanding
For each question, list the current crossing edges before deciding which edge Prim’s algorithm selects.
- Let \(V=\{a,b,c\}\), with edge weights \(w(ab)=5\), \(w(ac)=2\), and \(w(bc)=1\). Run Prim’s algorithm from \(a\), recording the reached set and choice at each step.
- In a run with reached set \(S=\{1,2,3\}\), the crossing edges have weights \(4,4,7\). Which edge or edges may be selected, and what does the tie permit?
- Explain why adding an edge with both endpoints already in \(S\) cannot be a step of Prim’s algorithm.
- Prove that if a connected graph has one vertex, Prim’s algorithm returns a spanning tree with no selected edges.
- In a disconnected graph, what does a run from \(r\) reach before it stops, and why can it not produce a spanning tree of the whole graph?