Activity Selection Problem
Activity Selection Problem
Problem
TEST TIME ON KRUSKAL'S
ALGORITHM
URL:
https://forms.gle/UEMmeQcjPSFFMrMq6
QR CODE:
GREEDY ALGORITHMS
Introduction
A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally
optimal choice at each stage
In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic
can yield locally optimal solutions that approximate a globally optimal solution in a reasonable
amount of time.
Example: In Travelling Sales Problem, the heuristic is "At each step of the journey, visit the
nearest unvisited city."
This problem has unreasonably many steps and greedy approach limits them
GREEDY ALGORITHMS
Example
Properties
Greedy Algorithm solves problems by making the best choice that seems best at the
particular moment.
Recap: At every step, we can make a choice that looks best at the
moment, and we get the optimal solution of the complete problem.
Problem Statement: You are given n activities with their start and finish
times. Select the maximum number of activities that can be performed by
a single person, assuming that a person can only work on a single activity
at a time.
ACTIVITY SELECTION PROBLEM
Introduction
Recap: At every step, we can make a choice that looks best at the
moment, and we get the optimal solution of the complete problem.
Problem Statement: You are given n activities with their start and finish
times. Select the maximum number of activities that can be performed by
a single person, assuming that a person can only work on a single activity
at a time.
ACTIVITY SELECTION PROBLEM
Examples
Output Output
{0,2} {0, 1, 3, 4}
Explanation Explanation
A person can perform at most two A person can perform at most four
activities. The maximum set of activities activities. The maximum set of activities
that can be executed is {0, 2} that can be executed is {0, 1, 3, 4}
These are indexes in start[] and finish[]
ACTIVITY SELECTION PROBLEM
Algorithm
The greedy choice is to always pick the next activity whose finish time is
least among the remaining activities and the start time is more than or
equal to the finish time of the previously selected activity.
Sample IO
Input
start = {1, 3, 0, 5, 8, 5};
end = {2, 4, 6, 7, 9, 9};
Output
0134