Minimum Spanning Trees and Greedy Algorithms (VCE SSCE General Mathematics): Revision Notes
Minimum Spanning Trees and Greedy Algorithms
Introduction
When working with networks, we sometimes need to connect all points (vertices) together using the minimum amount of resources. This is different from finding the shortest path between two specific points.
Understanding the difference: Minimum spanning trees connect ALL vertices with minimum total weight, while shortest path algorithms find the minimum distance between TWO specific vertices.
Imagine connecting several towns to a water supply network. Rather than creating direct connections between every pair of towns, we want to minimise the total length of water pipes needed while still ensuring every town gets connected. This type of problem can be solved using a mathematical structure called a tree.
Trees
What is a tree?
A tree is a connected graph that has no cycles, no multiple edges, and no loops.
Key properties of trees:
- All vertices are connected
- There are no closed loops or circuits
- No two vertices are connected by more than one edge
- No vertex connects back to itself

The first two graphs shown are trees because they connect all vertices without creating any cycles. The third graph is not a tree because it contains a loop (a vertex connecting to itself), multiple edges between vertices, and cycles (closed paths).
The vertex-edge relationship
Trees have a special mathematical relationship between vertices and edges.
Fundamental Tree Formula
If a tree has vertices, it will have exactly edges.
This relationship ALWAYS holds true for trees. You cannot have more or fewer edges and still maintain the tree structure.
For example:
- A tree with vertices has edges
- A tree with vertices has edges
Spanning trees
What is a spanning tree?
A spanning tree is a tree that connects all vertices in a connected graph.
When you have a graph with multiple edges, you can often create several different spanning trees by removing some edges while keeping all vertices connected.
Graph 2 and 3 above are different spanning trees of Graph 1.

Finding spanning trees
To find a spanning tree for a graph:
- Count the vertices. If there are vertices, your spanning tree needs exactly edges.
- Remove edges from the graph while ensuring:
- All vertices remain connected
- No cycles are created
- No multiple edges or loops remain
- Multiple spanning trees usually exist for any given graph.
Example: Finding spanning trees in a graph
Consider a graph with five vertices and seven edges. A spanning tree will need five vertices and four edges.
To create spanning trees, we can remove three edges (since ), provided all vertices stay connected and no multiple edges or loops remain.
Different combinations of edge removal will produce different spanning trees for the same graph.
Multiple solutions: For most graphs, there are many possible spanning trees. Each valid spanning tree connects all vertices using exactly edges.
Minimum spanning trees
What is a minimum spanning tree?
For weighted graphs (where edges have numerical values representing distance, cost, time, etc.), we can calculate the total weight of each spanning tree.
A minimum spanning tree is a spanning tree with the smallest possible total weight.
To find the length of a spanning tree, add up all the edge weights in that tree.
For example, if a spanning tree includes edges with weights , , , , , , and :
Real-world applications
Minimum spanning trees solve practical problems where we want to minimise:
- The amount of cable needed for a computer network
- The length of water pipes for a housing estate
- The time needed to complete a project
- The cost of connecting locations
In each case, we want every location connected using the minimum total resource.
Algorithmic methods for determining minimum spanning trees
What is an algorithm?
An algorithm is a set of step-by-step instructions for solving a problem or completing a task.
In everyday life, a recipe is an algorithm. In mathematics, long division follows an algorithm.
For graphs and networks, algorithms provide systematic methods to find properties like minimum spanning trees, removing the need for trial-and-error inspection.
Prim's algorithm
Prim's algorithm provides a systematic way to find a minimum spanning tree for a weighted graph.
Steps for Prim's algorithm
- Choose any starting vertex.
- From this vertex, select the edge with the lowest weight. If multiple edges have the same weight, choose any. This edge and the two vertices it connects form the beginning of your minimum spanning tree.
- Look at all edges connected to vertices already in your tree. Choose the edge with the lowest weight that doesn't create a cycle (ignore edges that would connect the tree back to itself). Add this edge and its new vertex to your tree.
- Repeat step 3 until all vertices are connected.
- Calculate the total weight by adding all edge weights in your tree.
Key principle of Prim's algorithm: Always choose the smallest weighted edge from vertices ALREADY in your tree. This builds the tree outward from a starting point.
Worked example: Prim's algorithm
Worked Example: Applying Prim's Algorithm
Question: Apply Prim's algorithm to find the minimum spanning tree for this graph and determine its total weight.

Solution:
Step 1: Start at vertex A.
The smallest weighted edge from A is to B, with weight .

Step 2: Look at vertices A and B.
From either A or B, the smallest weighted edge is from A to D, with weight .
Step 3: Look at vertices A, B, and D.
The smallest weighted edge from these vertices is from D to C, with weight .
Step 4: Look at vertices A, B, D, and C.
The smallest weighted edge is from C to E, with weight .
Step 5: Look at vertices A, B, D, C, and E.
The smallest weighted edge is from E to F, with weight .
All vertices are now connected. This is our minimum spanning tree.
Step 6: Calculate the total weight.
The minimum spanning tree has a length of 17 units.
Kruskal's algorithm
Kruskal's algorithm is an alternative method for finding minimum spanning trees.
Difference from Prim's algorithm
Key Difference Between Algorithms:
- Prim's algorithm: Builds the tree by starting at one vertex and expanding outward
- Kruskal's algorithm: Sorts all edges by weight and adds them in order, regardless of location
Steps for Kruskal's algorithm
- Choose the edge with the smallest weight as your starting edge. If multiple edges share the smallest weight, choose any.
- From the remaining edges, select the edge with the smallest weight that doesn't form a cycle. If multiple edges share the smallest weight, choose any.
- Repeat step 2 until all vertices are connected. You now have a minimum spanning tree.
- Calculate the total weight by adding all selected edge weights.
Key principle of Kruskal's algorithm: Always choose the smallest available edge from ANYWHERE in the graph, as long as it doesn't create a cycle.
Worked example: Kruskal's algorithm
Worked Example: Applying Kruskal's Algorithm
Question: Apply Kruskal's algorithm to find the minimum spanning tree and calculate its length.

Solution:
Step 1: Choose the edge with the smallest weight.
There are two edges with weight : AB and EF. We'll choose AB.
Step 2: From remaining edges, choose the smallest weight.
Edge FE has weight . Add it to our tree.
Step 3: Continue with the next smallest weight.
Edge CD has weight . Add it to our tree.
Step 4: Continue with edges of weight .
There are two edges with weight : AD and CE. Neither creates a cycle, so we can choose either. We'll add edge AD.
Step 5: Add the remaining edge of weight .
Add edge CE. All vertices are now connected.
All vertices are connected, forming our minimum spanning tree.
Step 6: Calculate total weight.
The minimum spanning tree has a length of 17 units.
Notice that both Prim's and Kruskal's algorithms found minimum spanning trees with the same total weight, though the specific edges selected may differ.
Greedy algorithms
What is a greedy algorithm?
A greedy algorithm solves optimisation problems by making the best choice at each step, hoping this leads to the best overall solution.
Prim's and Kruskal's algorithms are both greedy algorithms because:
- At each step, they choose the edge with the smallest available weight
- They don't reconsider earlier decisions
- They build toward a solution step-by-step
Characteristics of greedy algorithms
Advantages of greedy algorithms:
- Simple and intuitive to understand
- Systematic approach that can be followed reliably
- Work well for certain types of problems (like minimum spanning trees)
Limitations:
- Can be time-consuming for large, complex networks
- Don't always produce the optimal solution for all problems
- However, Prim's and Kruskal's algorithms reliably find minimum spanning trees
Dijkstra's algorithm for shortest paths (Extension)
Dijkstra's algorithm is another greedy algorithm, but it finds the shortest path between two specific vertices rather than a minimum spanning tree.
Critical Difference: Dijkstra's algorithm finds the shortest path between TWO specific vertices, while Prim's and Kruskal's algorithms create a tree connecting ALL vertices.
Steps for Dijkstra's algorithm
- Assign the starting vertex a value of zero and circle it.
- For each vertex connected to the circled vertex, assign a value equal to the edge weight connecting it to the starting vertex. Circle the vertex with the lowest assigned value.
- From the newly circled vertex, assign values to connected vertices by adding the edge weight to the circled vertex's value. If a vertex already has a value and the new value is smaller, replace it. If a vertex is already circled, don't change its value. Circle the uncircled vertex with the lowest value.
- Repeat step 3 until the destination vertex is circled. The assigned value of the destination vertex is the length of the shortest path.
- Find the shortest path by backtracking: Start at the destination and work backward through circled vertices, choosing edges where the vertex value minus the edge weight equals the previous vertex value.
Remember!
Key Points to Remember:
-
A tree is a connected graph with no cycles, loops, or multiple edges. A tree with vertices has exactly edges.
-
A spanning tree connects all vertices in a graph while maintaining tree properties. Multiple spanning trees can exist for the same graph.
-
A minimum spanning tree has the smallest possible total weight among all spanning trees.
-
Prim's algorithm builds a minimum spanning tree by starting at a vertex and progressively adding the lowest-weight edges from already-connected vertices.
-
Kruskal's algorithm builds a minimum spanning tree by sorting all edges by weight and adding them in order while avoiding cycles.
-
Greedy algorithms make locally optimal choices at each step. Both Prim's and Kruskal's algorithms are greedy algorithms that successfully find minimum spanning trees.
-
Dijkstra's algorithm finds the shortest path between two specific vertices by progressively assigning and updating distance values.