Greedy algorithms and optimal substructure
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Greedy algorithms are a significant class of algorithms used in solving optimization problems. These algorithms follow the problem-solving heuristic of making the locally optimal choice at each stage, with the hope that these local solutions will lead to a globally optimal solution. Greedy algorithms are intuitive and often provide efficient solutions to complex problems—under the right conditions.
Characteristics of Greedy Algorithms
1. Greedy Choice Property
This property implies that a globally optimal solution can be arrived at by making locally optimal (greedy) choices. Each step in the solution process involves making the best choice available at that moment without reconsidering previously selected choices. A canonical example is the Activity Selection Problem, where we select the maximum number of activities that do not overlap in time.
2. Optimal Substructure
For a problem to be solved by a greedy algorithm, it must exhibit the optimal substructure property, meaning the optimal solution to the problem contains within it optimal solutions to sub-problems.
Key Examples of Greedy Algorithms
Activity Selection Problem
Consider a set of activities each defined by a start and end time, and the task is to select the maximum number of mutually exclusive activities. The greedy choice property suggests that choosing the activity that ends the earliest will leave the most room for subsequent activities, which aligns with making a locally optimal choice.
- Prim's and Kruskal's Algorithms for constructing a minimum spanning tree
- Huffman Coding for optimal prefix-free coding
- Dijkstra's Algorithm for shortest paths in a weighted graph with non-negative weights
- Simplicity: Greedy solutions are often easier to conceptualize and implement.
- Efficiency: Greedy algorithms usually have lower time complexity than more exhaustive methods like dynamic programming, especially in scenarios where the greedy choice property holds.
- Suboptimal Solutions: In scenarios where greedy choice does not lead to the global optimum, it can provide incorrect results.
- Not Universally Applicable: Many problems require more complex approaches, such as dynamic programming, where greedy algorithms fail due to the violation of the greedy choice property or lack of optimal substructure.
Related reading
- Grokking Timsort
- Group n points in k clusters of equal size
- Group the numbers C
- GUI layout algorithms overview
- Guided mining of common substructures in large set of graphs
- Hamming numbers for ON speed and O1 memory
- handling unary minus for shunting-yard algorithm
- Has anyone actually implemented a Fibonacci-Heap efficiently?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.