Greedy Algorithm – Report
1. Introduction
A Greedy Algorithm is a problem-solving paradigm that builds up a solution piece by piece, always
choosing the option that offers the most immediate benefit (locally optimal choice), hoping that
this leads to a globally optimal solution. It is simple, fast, and often effective for a wide range of
optimization problems.
2. Key Characteristics
To apply a greedy algorithm effectively, a problem must exhibit:
• Greedy Choice Property: A global optimum can be reached by choosing the local optimum
at each step.
• Optimal Substructure: A problem’s solution can be constructed efficiently from optimal
solutions of its subproblems.
3. General Steps
1. Define the objective function (what you want to optimize).
2. Make a greedy choice at each step (locally best option).
3. Check feasibility (does the choice keep the solution valid?).
4. Repeat until a complete solution is formed.
4. Example: Activity Selection Problem
Problem Statement:
Given n activities with start and end times, select the maximum number of non-overlapping
activities.
Greedy Strategy:
• Always pick the activity that finishes earliest.
Pseudocode:
python
CopyEdit
def activity_selection(activities):
activities.sort(key=lambda x: x[1]) # Sort by end time
selected = [activities[0]]
for i in range(1, len(activities)):
if activities[i][0] >= selected[-1][1]:
selected.append(activities[i])
return selected