Greedy Optimization and Prim’s Minimum Spanning Tree Algorithm

Greedy Optimization and Prim’s Minimum Spanning Tree Algorithm

Verified Sources
Sep 11, 2026

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

  1. Greedy algorithm - Wikipedia - Definition of greedy algorithms and examples including Prim’s algorithm.

  2. 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:

  1. A set of feasible solutions.
  2. An objective function.
  3. A goal to maximize or minimize that function.

If SS is a feasible solution and f(S)f(S) is its objective value, then:

  • For a minimization problem, an optimal solution SS^* satisfies
    f(S)f(S)f(S^*) \leq f(S)
    for every feasible solution SS.
  • For a maximization problem, an optimal solution SS^* satisfies
    f(S)f(S)f(S^*) \geq f(S)
    for every feasible solution SS.

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 G=(V,E)G=(V,E) must:

  • Include every vertex in VV.
  • Contain exactly V1|V|-1 edges.
  • Be connected.
  • Contain no cycle.

If TT is a spanning tree, its weight is:

w(T)=eTw(e)w(T)=\sum_{e\in T}w(e)

The objective of the minimum spanning tree problem is:

minT is a spanning treew(T)\min_{T\text{ is a spanning tree}} w(T)

Footnotes

  1. 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 SS and VSV-S, 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

  1. 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:

ComponentFunction
Candidate setStores choices that may be added
Selection functionChooses the locally best candidate
Feasibility testChecks whether the choice remains valid
Objective functionMeasures solution quality
Solution testDetermines 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

  1. 1
    Step 1

    Specify whether the problem seeks to minimize or maximize a measurable objective such as total cost, total weight, time, or number of resources.

  2. 2
    Step 2

    Identify the constraints that every valid solution must satisfy.

  3. 3
    Step 3

    Choose the candidate that appears best according to the current objective.

  4. 4
    Step 4

    Prove that at least one globally optimal solution contains the proposed local choice.

  5. 5
    Step 5

    Show that after the choice is made, the remaining problem has the same optimization structure.

  6. 6
    Step 6

    Use an exchange argument, induction, or a structural theorem such as the MST cut property.

  7. 7
    Step 7

    Determine the time and space required by the selection, feasibility, and update operations.

Greedy approach: common questions

5. Minimum spanning trees

Let G=(V,E)G=(V,E) be a connected, undirected, weighted graph. A minimum spanning tree TT is a subgraph satisfying:

TGT\subseteq G

and:

E(T)=V1|E(T)|=|V|-1

with minimum total weight:

w(T)=eE(T)w(e)w(T)=\sum_{e\in E(T)}w(e)

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:

FeaturePrim’s algorithmKruskal’s algorithm
Growth patternExpands one connected treeMerges separate components
Main choiceCheapest edge leaving current treeCheapest edge in the entire remaining graph
Cycle handlingSelects an outside vertexUses disjoint-set detection
Typical data structurePriority queueSorting plus union-find
Best viewpointVertex expansionEdge 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:

  • SS be the set of vertices already in the tree.
  • VSV-S be the unvisited vertices.
  • δ(S)\delta(S) be the set of edges with one endpoint in SS and the other in VSV-S.

At each iteration, Prim selects:

e=argmineδ(S)w(e)e^*=\arg\min_{e\in\delta(S)}w(e)

Then it adds ee^* 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

  1. Prim’s algorithm - Wikipedia - Algorithm description, pseudocode, and implementation variants.

Prim’s algorithm: manual procedure

  1. 1
    Step 1

    Select any vertex as the root. Mark it as visited and place it in the current tree.

  2. 2
    Step 2

    Write down every edge with exactly one endpoint in the current tree.

  3. 3
    Step 3

    Select the boundary edge with minimum weight. If tied, any tied edge may be selected.

  4. 4
    Step 4

    The selected edge must lead to an unvisited vertex. Ignore an edge whose endpoints are both already in the tree.

  5. 5
    Step 5

    Add the selected edge to the MST and mark its previously unvisited endpoint as visited.

  6. 6
    Step 6

    Add edges incident to the newly visited vertex and remove obsolete internal edges.

  7. 7
    Step 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:

Graph for Prim's Algorithm

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:

  1. Select the stated starting vertex.
  2. Record all edges incident to it.
  3. Choose the smallest incident edge.
  4. Add the newly reached vertex.
  5. Recompute the minimum edge crossing the current-tree boundary.
  6. Continue until every vertex is included.
  7. Add exactly V1|V|-1 edges.
  8. Sum the selected weights.

Use the following worksheet to transcribe the graph accurately:

IterationCurrent vertices SSCandidate crossing edgesSelected edgeWeightTotal
0Starting vertexAll incident edges00
1S1S_1Edges from S1S_1 to VS1V-S_1e1e_1w1w_1w1w_1
2S2S_2Edges from S2S_2 to VS2V-S_2e2e_2w2w_2w1+w2w_1+w_2
3S3S_3Edges from S3S_3 to VS3V-S_3e3e_3w3w_3w1+w2+w3w_1+w_2+w_3
\vdots\vdots\vdots\vdots\vdots\vdots
$V-1$VVNone$e_{

The final answer must have:

V1|V|-1

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 A,B,C,D,EA,B,C,D,E and the selected edges, after reading the graph labels, are determined as follows. The trace should be presented in this form:

StepVisited setBoundary edge weightsGreedy choiceMST weight
0{A}\{A\}AB:a, AC:b, AD:cAB:a,\ AC:b,\ AD:cMinimum of a,b,ca,b,c00
1{A,}\{A,\ldots\}Newly exposed boundary edgesLightest valid edgePrevious total ++ selected weight
2{}\{\ldots\}Updated boundaryLightest valid edgePrevious total ++ selected weight
3{}\{\ldots\}Updated boundaryLightest valid edgePrevious total ++ selected weight
4VVNoneStopFinal 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:

MST(G)={e1,e2,,eV1}\operatorname{MST}(G)=\{e_1,e_2,\ldots,e_{|V|-1}\}

and:

w(MST)=i=1V1w(ei)w(\operatorname{MST})=\sum_{i=1}^{|V|-1}w(e_i)

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 SS and the current tree is contained in some MST TT. Prim chooses the minimum-weight edge ee crossing the cut (S,VS)(S,V-S).

If ee already belongs to TT, the claim is immediate.

Otherwise, add ee to TT. This creates a cycle because TT was a tree. The cycle must contain another edge ff crossing the same cut. Since ee is the lightest edge crossing the cut:

w(e)w(f)w(e)\leq w(f)

Remove ff from the cycle. The result is another spanning tree:

T=T+efT' = T+e-f

Its weight satisfies:

w(T)=w(T)+w(e)w(f)w(T)w(T')=w(T)+w(e)-w(f)\leq w(T)

Because TT was already minimum, TT' is also an MST. Thus, there exists an MST containing ee.

Therefore, every edge selected by Prim is safe. After V1|V|-1 safe edges have been selected, the result is an MST. \square

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.

ImplementationTime complexitySpace 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, EV1|E|\geq |V|-1, so binary-heap implementations are often written as:

O(ElogV)O(|E|\log |V|)

The adjacency-matrix version can be competitive for dense graphs, where:

EV2|E|\approx |V|^2

The binary-heap version is usually preferable for sparse graphs.

Footnotes

  1. Prim’s Minimum Spanning Tree Algorithm - Complexity comparison for matrix, adjacency-list, and priority-queue implementations.

1PRIM(G, start): 2 for each vertex v in V: 3 key[v] = infinity 4 parent[v] = NIL 5 inMST[v] = false 6 7 key[start] = 0 8 Q = priority queue containing all vertices by key 9 10 while Q is not empty: 11 u = EXTRACT-MIN(Q) 12 13 if inMST[u] == true: 14 continue 15 16 inMST[u] = true 17 18 for each edge (u, v) with weight w: 19 if inMST[v] == false and w < key[v]: 20 parent[v] = u 21 key[v] = w 22 DECREASE-KEY(Q, v, key[v]) 23 24 return {(parent[v], v) : v != start}

11. Common mistakes

  1. Choosing the smallest edge in the entire graph
    Prim chooses the smallest edge crossing the current cut, not necessarily the globally smallest unused edge.

  2. Adding an edge between two visited vertices
    Such an edge is internal to the current tree and creates a cycle.

  3. Stopping too early
    A spanning tree requires every vertex. The algorithm stops after V1|V|-1 selected edges.

  4. 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.

  5. Adding more than V1|V|-1 edges
    More than V1|V|-1 edges in a connected structure necessarily creates a cycle.

  6. 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

1 / 8
Question · Term

What is an optimal solution?

Click to reveal
Answer · Definition

A feasible solution with the best objective value among all feasible solutions.

Knowledge Check

Question 1 of 5
Q1Single choice

What is meant by an optimal solution in a minimization problem?

Explore Related Topics

1

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 ∞.
2

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 O(V+E)O(V+E) time; BFS may need O(V)O(V) (or O(bd)O(b^d)) space, while DFS typically uses O(d)O(d) 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.
3

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 WW—is examined through four classic solution strategies: brute‑force, greedy, dynamic programming, and branch‑and‑bound.

  • Brute force checks all 2n2^n subsets, guaranteeing optimality but with exponential O(2n)O(2^n) time.
  • Greedy heuristics (e.g., highest vi/wiv_i/w_i first) run in O(nlogn)O(n\log n) but can miss the optimum because 0/1 knapsack lacks the greedy‑choice property.
  • Dynamic programming exploits optimal substructure, solving in O(nW)O(nW) time and O(nW)O(nW) (or O(W)O(W)) space, yet is pseudo‑polynomial and costly for large WW.
  • Branch‑and‑bound explores a decision tree, pruning nodes via fractional‑knapsack upper bounds; worst‑case O(2n)O(2^n) but often far faster on favorable instances.