D1 2.1 - Minimum spanning trees

D1 2.1 - Minimum spanning trees

Connect every vertex of a network with the least possible total edge weight, using Kruskal's and Prim's algorithms. You will apply Prim's method directly to a matrix, handle equal weights, and check that a proposed connector really is a spanning tree.

Kruskal's algorithm

If every site must be connected, should you simply take the shortest edges? You also need to avoid paying for a cycle while leaving a site unconnected.

Recall that a spanning tree includes every vertex, is connected and contains no cycles. On nn vertices it has n−1n-1 edges. The minimum spanning tree (MST), or minimum connector, minimises the sum of these edge weights. It is a set of connections, not a route which must return to its start.

Kruskal's algorithm

List edges in increasing weight order. Consider them in that order, accepting an edge unless it creates a cycle with edges already accepted. Reject such cycle-forming edges. Stop when n−1n-1 edges have been accepted and all vertices are connected.

The accepted edges can initially form separate groups. A new edge is useful when it joins two different groups; an edge whose endpoints already have a connecting path would close a cycle. Choosing the cheapest available connection between groups avoids replacing it later by a more expensive one. This is the reason the greedy choice produces a minimum connector.

[DIAGRAM: asset_name: A network and its minimum connector; asset_slug: minimum-connector; caption: Four accepted edges connect all five vertices; BD is rejected because it closes a cycle.; description: The left panel has weighted edges AB 6, AC 7, BC 2, BD 3, CD 1, CE 8, DE 4. The right panel highlights AB BC CD DE with total weight 13. Edge BD is shown dashed as the cycle-forming rejection after CD and BC.]
Four accepted edges connect all five vertices

Worked example 1: rejecting a short cycle edge

Use Kruskal's algorithm on vertices A,B,C,D,EA,B,C,D,E with edges AB=6,AC=7,BC=2,BD=3,CD=1,CE=8,DE=4AB=6,AC=7,BC=2,BD=3,CD=1,CE=8,DE=4.

Plan. Sort all edges, then test connectivity of the endpoints before each acceptance.

Edge in weight orderDecisionReason
CD=1CD=1AcceptJoins two vertices
BC=2BC=2AcceptJoins BB to the CDCD group
BD=3BD=3RejectB,C,D,BB,C,D,B would be a cycle
DE=4DE=4AcceptJoins EE to the group
AB=6AB=6AcceptJoins the remaining vertex AA

The remaining edges AC=7,CE=8AC=7,CE=8 need not be considered after four acceptances.

Answer. MST edges CD,BC,DE,ABCD,BC,DE,AB, with total weight 1+2+4+6=131+2+4+6=13.

Check. They form the chain A,B,C,D,EA,B,C,D,E: five vertices, four edges, connected, no cycle. Replacing either chosen edge BC=2BC=2 or CD=1CD=1 by the rejected BD=3BD=3 would increase the total.

Change only ABAB's weight from 66 to 99. It moves later in the ordered list, changing how AA is connected.

Worked example 2: a reweighted connection

Apply Kruskal's algorithm to the same network with AB=9AB=9.

Plan. Use the revised increasing order, retaining the same cycle test.

CD(1), BC(2), BD(3), DE(4), AC(7), CE(8), AB(9).CD(1),\ BC(2),\ BD(3),\ DE(4),\ AC(7),\ CE(8),\ AB(9).

Accept CD,BCCD,BC; reject BDBD because BB and DD are already connected through CC. Accept DEDE, then ACAC, which brings in AA. Stop at four accepted edges.

Answer. MST edges CD,BC,DE,ACCD,BC,DE,AC, total 1+2+4+7=141+2+4+7=14.

Check. Vertex AA has only two incident edges, now of weights 77 and 99. The selected connector uses the cheaper one, and all five vertices connect through CC or DD.

Rejection is part of the trace, even when the rejected edge has a small weight.

Watch out

Taking the four smallest weights 1,2,3,41,2,3,4 would include the cycle B,C,D,BB,C,D,B and leave AA out. Count accepted edges, not edges considered.

Show which edges are chosen in this smaller network.

Prim's algorithm on a network

Prim's algorithm grows one connected tree from a starting vertex, so its eligible edges differ from Kruskal's.

Prim's algorithm

Start at the specified vertex, or choose one if none is specified. Select the least-weight edge joining a vertex already in the tree to one outside it. Add that edge and its new vertex. Repeat until every vertex is included.

At each step consider edges from any included vertex, not just the last vertex added. Ignore edges between two included vertices and edges between two outside vertices. Every chosen edge adds a new vertex, so no cycle is formed. Selecting the cheapest edge across the included/outside boundary is the safe greedy choice.

Worked example 3: growing from A

Apply Prim's algorithm starting at AA to the original five-vertex network: AB=6,AC=7,BC=2,BD=3,CD=1,CE=8,DE=4AB=6,AC=7,BC=2,BD=3,CD=1,CE=8,DE=4.

Plan. Record the included set and select the cheapest edge crossing its boundary.

Included before selectionEdge selectedNew vertex
{A}\{A\}AB=6AB=6BB
{A,B}\{A,B\}BC=2BC=2CC
{A,B,C}\{A,B,C\}CD=1CD=1DD
{A,B,C,D}\{A,B,C,D\}DE=4DE=4EE

At the third selection, CD=1CD=1 is cheaper than BD=3BD=3, even though BDBD is also eligible.

Answer. Edges in order: AB,BC,CD,DEAB,BC,CD,DE; minimum total 6+2+1+4=136+2+1+4=13.

Check. The resulting edge set agrees with Kruskal's, though the order differs. Each selection introduced exactly one of the four vertices outside the start.

Change only the starting vertex from AA to EE. A different trace can give the same MST.

Worked example 4: growing from E

Apply Prim's algorithm to the same network starting at EE.

Plan. Rebuild the included set from EE, always considering the whole boundary.

Included before selectionSelected edgeNew vertex{E}ED=4D{E,D}DC=1C{E,D,C}CB=2B{E,D,C,B}BA=6A\begin{array}{c|c|c} \text{Included before selection}&\text{Selected edge}&\text{New vertex}\\\hline \{E\}&ED=4&D\\ \{E,D\}&DC=1&C\\ \{E,D,C\}&CB=2&B\\ \{E,D,C,B\}&BA=6&A \end{array}

For the final step, the cheapest connection to AA is BA=6BA=6, even though AA is not adjacent to the starting vertex EE.

Answer. ED,DC,CB,BAED,DC,CB,BA, total 1313.

Check. Reversing the names of undirected edges gives the same four edges as before. Changing the start has not changed the minimum possible weight.

The method is about a growing set of vertices, not a traveller moving from place to place.

Watch out

Do not choose the shortest edge from only the most recently added vertex. Reconsider all included vertices at every step, and do not add a return edge after all vertices are included.

Use a new network to record the growth explicitly.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account