Greedy Algorithms
Greedy Algorithms
Greedy Algorithms
Greedy algorithms are a class of algorithms that make a series of choices, each of
which looks best at the moment. The basic idea is to pick the locally optimal
solution at each step with the hope of finding a global optimum. Greedy
algorithms are typically used for optimization problems.
Dijkstra's algorithm
Dijkstra's is an algorithm used to find the shortest paths between nodes in a
graph, which may represent various types of networks such as road maps,
computer networks, or flight connections. Specifically, Dijkstra's algorithm
solves the single-source shortest path problem for a graph with non-negative
edge weights.
2
Greedy Approach:
1. Sort the activities by their finish times.
2. Select the first activity (which has the earliest finish time).
3. Iterate through the sorted list, and for each activity, if the start time is
greater than or equal to the finish time of the last selected activity, select it.
Reasoning: By always choosing the activity that finishes earliest, you leave as
much room as possible for the remaining activities, thus maximizing the number
of non-overlapping activities.
Example
Activity 1: (1, 4)
Activity 2: (3, 5)
Activity 3: (0, 6)
Activity 4: (5, 7)
Activity 5: (8, 9)
Activity 6: (5, 9)
4
Meaning
Steps:
1. Sort by finish time: (1, 4), (3, 5), (0, 6), (5, 7), (5, 9), (8, 9)
2. Select the first activity: (1, 4)
3. Next, (3, 5) is not selected as it overlaps with (1, 4)
4. (0, 6) is not selected as it overlaps with (1, 4)
5. (5, 7) is selected as it doesn't overlap with (1, 4)
6. (5, 9) is not selected as it overlaps with (5, 7)
7. (8, 9) is selected as it doesn't overlap with (5, 7)
While the activity selection problem is well-suited for a greedy approach, other scheduling
problems may require different strategies:
1. Job Scheduling with Deadlines and Profits: This may require dynamic programming or
other optimization techniques.
2. Interval Partitioning: Where the goal is to use the minimum number of resources to
schedule all activities. This is usually tackled by sorting and using a greedy approach,
similar to the activity selection problem.
3. Weighted Interval Scheduling: This requires dynamic programming since simply
selecting activities based on earliest finish time might not yield the optimal solution.
5
Exercises
Conference Room Scheduling Problem
Problem Statement: Given a set of meetings, each with a start and end time,
select the maximum number of non-overlapping meetings that can be scheduled
in the conference room.
Meetings:
Meeting 1: Team Standup (9:00 AM - 10:00 AM)
Meeting 2: Client Call (11:00 AM - 12:00 PM)
Meeting 3: Project Planning (1:00 PM - 3:00 PM)
Meeting 4: Code Review (2:00 PM - 4:00 PM)
Meeting 5: Marketing Sync (10:30 AM - 11:30 AM)
Meeting 6: HR Update (3:30 PM - 4:30 PM)
Meeting 7: Design Discussion (12:00 PM - 1:30 PM)
Meeting 8: Weekly Sync (8:00 AM - 9:00 AM)
Task:
1. Convert the meeting times to a 24-hour format and sort the meetings
based on their end times.
2. Select the maximum number of non-overlapping meetings using the greedy
approach.
6
7