Greedy Optimization and Prim’s Minimum Spanning Tree Algorithm
Learning objectives
By the end of this section, you should be able to:
- Define an optimal solution.
- Explain the greedy approach and its purpose.
- Distinguish local optimality from global optimality.
- Describe the greedy-choice property and optimal substructure.
- Explain why Prim’s algorithm is greedy.
- Construct a minimum spanning tree manually using Prim’s algorithm.
- Analyze the correctness and time complexity of Prim’s algorithm.
A greedy algorithm constructs a solution incrementally. At every stage, it selects the choice that appears most beneficial immediately and does not normally revise earlier decisions. Greedy algorithms are attractive because they are often simple and efficient, but they produce a globally optimal result only for problems with suitable structural properties.
Footnotes
-
Greedy algorithm - Wikipedia - Definition of greedy algorithms and examples including Prim’s algorithm. ↩
-
Greedy Algorithms: Definition & Examples - Greedy-choice property and optimal substructure. ↩
Prim’s Algorithm in 2 Minutes
1. What is an optimal solution?
An optimization problem consists of:
- A set of feasible solutions.
- An objective function.
- A goal to maximize or minimize that function.
If is a feasible solution and is its objective value, then:
- For a minimization problem, an optimal solution satisfies
for every feasible solution . - For a maximization problem, an optimal solution satisfies
for every feasible solution .
For a weighted graph, a minimum spanning tree is an optimal solution to the problem of connecting every vertex with minimum total cost.
A spanning tree of a connected graph must:
- Include every vertex in .
- Contain exactly edges.
- Be connected.
- Contain no cycle.
If is a spanning tree, its weight is:
The objective of the minimum spanning tree problem is:
Footnotes
-
Prim’s Algorithm – Explained with a Pseudocode Example - Definition and purpose of a minimum spanning tree. ↩
Local versus global optimality
A locally optimal choice is best at the current step. A globally optimal solution is best over the entire feasible solution space. A greedy algorithm is correct only when its local choices can be proven to lead to a global optimum.
2. What does “optimal solution in the greedy approach” mean?
In a greedy approach, an optimal solution is obtained by repeatedly making locally optimal choices that can be shown to preserve the possibility of reaching a globally optimal solution.
The phrase does not mean that every locally cheapest choice is automatically correct. Instead, it means that the problem has a mathematical property allowing the local choice to be safely committed.
For Prim’s algorithm, the local choice is:
Select the minimum-weight edge crossing from the vertices already in the partial tree to a vertex outside it.
This edge is safe because of the cut property of minimum spanning trees.
If a cut divides the vertices into two sets and , then a minimum-weight edge crossing that cut is called a light edge. At least one MST contains that light edge. Therefore, Prim’s locally minimum edge can be included without destroying optimality.
Footnotes
-
The Cut Property - Educational explanation of the cut property used to justify MST choices. ↩
3. Properties of the greedy approach
3.1 Greedy-choice property
The greedy-choice property states that an optimal solution can be obtained by making a locally best choice first and then solving the remaining problem.
For Prim’s algorithm:
- The current tree defines a cut.
- The lightest edge crossing that cut is selected.
- That edge is safe to add to an MST.
The algorithm does not need to examine all possible future trees before making the choice.
3.2 Optimal substructure
The optimal substructure property means that after making a correct choice, the remaining portion of the problem can be solved optimally.
For an MST, if an edge is safely added to the growing tree, the remaining task is to connect the unvisited vertices with minimum additional cost.
3.3 Feasibility preservation
A greedy choice must preserve feasibility. In Prim’s algorithm, a selected edge:
- Connects the current tree to a new vertex.
- Does not create a cycle.
- Maintains connectivity of the partial solution.
3.4 Irrevocable decisions
Greedy algorithms generally do not reconsider choices. Once Prim’s algorithm adds an edge to the MST, that edge remains in the final tree.
3.5 Incremental construction
The solution is built one component at a time. Prim’s algorithm starts with one vertex and grows a single connected tree until all vertices are included.
3.6 Objective-directed selection
A selection function determines which candidate is selected. For Prim’s algorithm, the selection function chooses the smallest edge crossing the current tree boundary.
3.7 Proof requirement
A greedy algorithm should be accompanied by a correctness argument. Common proof techniques include:
- Exchange arguments.
- Cut and cycle properties.
- Induction over the number of selected elements.
- Staying-ahead arguments.
4. Function of the greedy approach
The function of a greedy approach is to reduce a large optimization problem to a sequence of smaller decisions.
A general greedy algorithm has the following conceptual structure:
A greedy method usually contains these components:
| Component | Function |
|---|---|
| Candidate set | Stores choices that may be added |
| Selection function | Chooses the locally best candidate |
| Feasibility test | Checks whether the choice remains valid |
| Objective function | Measures solution quality |
| Solution test | Determines whether the construction is complete |
For a minimum spanning tree:
- Candidate set: Edges adjacent to the current tree.
- Selection function: Minimum edge weight.
- Feasibility test: The edge must connect to an unvisited vertex.
- Objective: Minimize total edge weight.
- Solution test: All vertices have been included.
How to determine whether a greedy method is appropriate
- 1Step 1
Specify whether the problem seeks to minimize or maximize a measurable objective such as total cost, total weight, time, or number of resources.
- 2Step 2
Identify the constraints that every valid solution must satisfy.
- 3Step 3
Choose the candidate that appears best according to the current objective.
- 4Step 4
Prove that at least one globally optimal solution contains the proposed local choice.
- 5Step 5
Show that after the choice is made, the remaining problem has the same optimization structure.
- 6Step 6
Use an exchange argument, induction, or a structural theorem such as the MST cut property.
- 7Step 7
Determine the time and space required by the selection, feasibility, and update operations.
Greedy approach: common questions
5. Minimum spanning trees
Let be a connected, undirected, weighted graph. A minimum spanning tree is a subgraph satisfying:
and:
with minimum total weight:
An MST is useful in:
- Network and cable design.
- Road and pipeline planning.
- Electrical distribution.
- Cluster analysis.
- Broadcast and communication networks.
Prim’s and Kruskal’s algorithms are both greedy MST algorithms, but they grow the solution differently:
| Feature | Prim’s algorithm | Kruskal’s algorithm |
|---|---|---|
| Growth pattern | Expands one connected tree | Merges separate components |
| Main choice | Cheapest edge leaving current tree | Cheapest edge in the entire remaining graph |
| Cycle handling | Selects an outside vertex | Uses disjoint-set detection |
| Typical data structure | Priority queue | Sorting plus union-find |
| Best viewpoint | Vertex expansion | Edge selection |
6. Prim’s algorithm
Prim’s algorithm begins with an arbitrary vertex and repeatedly adds the least-weight edge connecting the current tree to an unvisited vertex.
Let:
- be the set of vertices already in the tree.
- be the unvisited vertices.
- be the set of edges with one endpoint in and the other in .
At each iteration, Prim selects:
Then it adds and its outside endpoint to the growing tree.
Invariant
After every iteration:
The selected edges form a tree that is contained in at least one MST.
This invariant is preserved by the cut property.
Footnotes
-
Prim’s algorithm - Wikipedia - Algorithm description, pseudocode, and implementation variants. ↩
Prim’s algorithm: manual procedure
- 1Step 1
Select any vertex as the root. Mark it as visited and place it in the current tree.
- 2Step 2
Write down every edge with exactly one endpoint in the current tree.
- 3Step 3
Select the boundary edge with minimum weight. If tied, any tied edge may be selected.
- 4Step 4
The selected edge must lead to an unvisited vertex. Ignore an edge whose endpoints are both already in the tree.
- 5Step 5
Add the selected edge to the MST and mark its previously unvisited endpoint as visited.
- 6Step 6
Add edges incident to the newly visited vertex and remove obsolete internal edges.
- 7Step 7
When every vertex is included, the selected edges form the MST.
7. Applying Prim’s algorithm to the supplied graph
The supplied image is referenced as:

The edge labels in the image should be read carefully before executing the algorithm. Since the graph image is externally hosted and its labels may not be available as machine-readable text in every rendering environment, the exact numerical MST depends on the visible vertex names and edge weights.
The correct procedure is therefore:
- Select the stated starting vertex.
- Record all edges incident to it.
- Choose the smallest incident edge.
- Add the newly reached vertex.
- Recompute the minimum edge crossing the current-tree boundary.
- Continue until every vertex is included.
- Add exactly edges.
- Sum the selected weights.
Use the following worksheet to transcribe the graph accurately:
| Iteration | Current vertices | Candidate crossing edges | Selected edge | Weight | Total |
|---|---|---|---|---|---|
| 0 | Starting vertex | All incident edges | — | — | |
| 1 | Edges from to | ||||
| 2 | Edges from to | ||||
| 3 | Edges from to | ||||
| $ | V | -1$ | None | $e_{ |
The final answer must have:
selected edges, no cycle, all vertices connected, and the smallest possible total weight.
Important when reading the supplied graph
Do not choose the globally smallest unused edge unless it crosses from the current tree to an unvisited vertex. Prim’s algorithm is boundary-based; selecting an internal edge can create a cycle or violate the algorithm’s rule.
8. Worked Prim trace format
Suppose the graph has vertices and the selected edges, after reading the graph labels, are determined as follows. The trace should be presented in this form:
| Step | Visited set | Boundary edge weights | Greedy choice | MST weight |
|---|---|---|---|---|
| 0 | Minimum of | |||
| 1 | Newly exposed boundary edges | Lightest valid edge | Previous total selected weight | |
| 2 | Updated boundary | Lightest valid edge | Previous total selected weight | |
| 3 | Updated boundary | Lightest valid edge | Previous total selected weight | |
| 4 | None | Stop | Final MST weight |
For an actual numerical solution, replace each symbolic weight with the number printed beside the corresponding edge in the supplied image.
A compact final presentation should be:
and:
9. Correctness proof for Prim’s algorithm
We prove correctness using the cut property.
Claim
At every iteration, the edge selected by Prim’s algorithm is safe and can belong to an MST.
Proof
Assume the current selected vertices are and the current tree is contained in some MST . Prim chooses the minimum-weight edge crossing the cut .
If already belongs to , the claim is immediate.
Otherwise, add to . This creates a cycle because was a tree. The cycle must contain another edge crossing the same cut. Since is the lightest edge crossing the cut:
Remove from the cycle. The result is another spanning tree:
Its weight satisfies:
Because was already minimum, is also an MST. Thus, there exists an MST containing .
Therefore, every edge selected by Prim is safe. After safe edges have been selected, the result is an MST.
Typical time complexity of Prim’s algorithm
Complexity depends on the graph representation and priority-queue implementation.
10. Complexity analysis
The complexity depends on the representation.
| Implementation | Time complexity | Space complexity |
|---|---|---|
| Adjacency matrix with linear minimum search | $O( | V |
| Binary heap and adjacency list | $O(( | V |
| Fibonacci heap and adjacency list | $O( | E |
For a connected graph, , so binary-heap implementations are often written as:
The adjacency-matrix version can be competitive for dense graphs, where:
The binary-heap version is usually preferable for sparse graphs.
Footnotes
-
Prim’s Minimum Spanning Tree Algorithm - Complexity comparison for matrix, adjacency-list, and priority-queue implementations. ↩
11. Common mistakes
-
Choosing the smallest edge in the entire graph
Prim chooses the smallest edge crossing the current cut, not necessarily the globally smallest unused edge. -
Adding an edge between two visited vertices
Such an edge is internal to the current tree and creates a cycle. -
Stopping too early
A spanning tree requires every vertex. The algorithm stops after selected edges. -
Forgetting that the graph must be connected
If the graph is disconnected, no single spanning tree exists. Prim instead produces a minimum spanning forest for the reachable component. -
Adding more than edges
More than edges in a connected structure necessarily creates a cycle. -
Assuming the MST is always unique
Equal-weight edges can produce multiple valid MSTs with the same total weight.
Exam technique
After every Prim iteration, write the visited set and list only edges that leave it. This makes the cut explicit and prevents accidental selection of an internal edge.
Greedy algorithms and Prim’s algorithm
Knowledge Check
What is meant by an optimal solution in a minimization problem?
Explore Related Topics
Negative Weight Cycle and the Bellman-Ford Algorithm for Single-Source Shortest Distance
Negative weight cycles are cycles whose total edge weight is negative, and the Bellman‑Ford algorithm computes single‑source shortest distances while detecting such cycles.
- A reachable negative weight cycle makes the shortest‑distance problem undefined because repeated traversal can lower the path cost without bound.
- Bellman‑Ford initializes distances, relaxes all edges |V|‑1 times, then performs one extra pass to detect any further relaxation.
- It correctly handles negative edges but reports failure when a reachable negative cycle exists.
- The algorithm runs in O(V E) time and uses O(V) extra space.
- Vertices unreachable from the source keep a distance of ∞.
Graph Traversals: Breadth-First Search (BFS) vs. Depth-First Search (DFS)
This content contrasts Breadth‑First Search (BFS) and Depth‑First Search (DFS), outlining their traversal order, complexity, and typical use cases.
- BFS uses a FIFO queue, visits nodes level by level (A→B→C→D→E→F); DFS uses a LIFO stack, dives deep (A→B→D→E→C→F).
- Both run in time; BFS may need (or ) space, while DFS typically uses stack depth.
- BFS guarantees the shortest path in unweighted graphs, suited for routing, web crawling, and level‑order serialization.
- DFS excels in memory‑limited, wide graphs and in tasks like topological sort and cycle detection, but deep recursion can cause stack overflow.
Solving the 0/1 Knapsack Problem: Brute Force, Greedy, Dynamic Programming, and Branch-and-Bound
The 0/1 knapsack problem—selecting whole items to maximize value under capacity —is examined through four classic solution strategies: brute‑force, greedy, dynamic programming, and branch‑and‑bound.
- Brute force checks all subsets, guaranteeing optimality but with exponential time.
- Greedy heuristics (e.g., highest first) run in but can miss the optimum because 0/1 knapsack lacks the greedy‑choice property.
- Dynamic programming exploits optimal substructure, solving in time and (or ) space, yet is pseudo‑polynomial and costly for large .
- Branch‑and‑bound explores a decision tree, pruning nodes via fractional‑knapsack upper bounds; worst‑case but often far faster on favorable instances.