Greedy Algorithms
A greedy algorithm always makes the choice that looks best at the moment. That is, it makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution.
Let's ask ChatSonic
Greedy Algorithms are a problem-solving approach that selects the best available option at each step to solve a problem. It works in a top-down approach and makes locally optimal choices at each stage, without worrying about the overall optimal result. This algorithm can be used if the problem has greedy choice and optimal substructure properties.
However, this algorithm may not always produce the optimal solution and can be characterized as being "short-sighted" and "non-recoverable".
References:
Greedy Algorithms vs Dynamic Programming
| Greedy | Dynamic Programming | |
|---|---|---|
| Technique | Usually Top-Down | Usually Bottom-Up |
| Problems solved | Does not need to solve all the sub-problems | Solves all the sub-problems |
| Choices | We make whatever choice seems best at the moment and then solve the subproblem that remains | At each step we make a choice, it usually depends on the solutions to subproblems |
| Choices depend on | Previous choices, choices made so far, no future problems or other solutions | Future choices or on the solutions of the subproblems |
| Solution | Locally Optimal Solution | Optimal Solution |
Greedy:
- What if we could choose an activity to add to our optimal solution without having to first solve all the sub-problems? That could save us from having to consider all the choices inherent in recurrence. In fact, for the activity-selection problem, we need consider only one choice: the greedy choice.
- All we really need to do is argue that an optimal solution to the subproblem, combined with the greedy choice already made, yields an optimal solution to the original problem. This scheme implicitly uses induction on the subproblems to prove that making the greedy choice at every step produces an optimal solution.
An activity-selection problem - Introductory Example
Our first example is the problem of scheduling several competing activities that require exclusive use of a common resource, with a goal of selecting a maximum-size set of mutually compatible activities.
, a set of mutually compatible activities, , with . - Each activity tracks a starting time
and end time , with . - We can interpret an activity as interval
- Each activity tracks a starting time
- Two activities
are said to be compatible if they do not overlap!
Example with Gantt diagram:

Let's try and study an algorithm that extracts activities in a greedy way:
- Sort activities by end time
- Create a set
to regroup the maximum number of compatible activities - Initialize it with the longest activity
- For each remaining activity, choose the one which finishes first
- if the current activity
has a starting time, bigger than the first activity , add it to the group . - Set current activity for next comparison
- if the current activity
- Return
Greedy_Activity_Selector(Double[] s, Double[] f): #Arrays of times
n = s.lenght();
sort_activities(predicate = f)
A = {1};
j = 1;
for(i=2 to n):
if(s[i] >= f[j]):
A = A.union(i);
j = i;
return A;Greedy_Activity_Selector(Double[] s, Double[] f): #Arrays of times
n = s.lenght();
sort_activities(predicate = f)
A = {1};
j = 1;
for(i=2 to n):
if(s[i] >= f[j]):
A = A.union(i);
j = i;
return A;Final Time Complexity:
This algorithm is very similar to Kruskal, that is because they share a common structure.
Greedy Common Structure
The following structure is a very common structure between certain types of greedy algorithm. The algorithm we just saw, and Kruskal share this same "skeleton", but they are not the only ones.
- Sorting on some criteria
- Initialization of a data structure
- For each element (extracted following the sorting criteria)
- If
is OK (compatibility condition) - Add it to data structure
- If
- Return the data structure
Ambiguous points
The predicate used to sort the elements and the condition of compatibility are yet to be defined and can change for each problem we face.
Credits
We deeply suggest reading through the chapter 16 of the "Introduction to Algorithms" (CLRS).