2.1 GreedyAlgorithms
2.1 GreedyAlgorithms
ALGORITHMS
GREEDY
A greedy algorithm always makes the choice that looks
best at the moment
The algorithm is usually an iterative algorithm
The solution usually composed of elements. For example, a
path which composed of edges, a set which composed of
elements, etc.
In each iteration or step, the algorithm adds one
component/element to the solution set. After final step, the
solution set will be complete solution of the problem.
GREEDY ALGORITHM:
PRINCIPLES
A greedy algorithm works in phases. At each phase:
The algorithm chooses the best you can get right now, without
regard for future consequences
The algorithm hopes that by choosing a local optimum at each
step, it will end up at a global optimum
For some problems, the global optimum can be achieved
DO ALL PROBLEMS HAVE
GREEDY ALGORITHM?
The answer is ‘NO’. Only a few problems have greedy algorithms. For
example, LCS problem, Fibonacci problem, bellman ford algorithm,
mergesort, Quicksort, etc. are not greedy algorithms!
Usually, a greedy algorithm happens to be faster than other algorithms.
However, there are many exceptions. It may happen that greedy algorithm
is slower than other algorithms (e.g., dynamic algorithm) of the same
problem.
Consider the following algorithm for sorting numbers:
in [2] and so on
sorting really
greedy?
Two activities are compatible if and only if their intervals do not overlap
Output: a maximum-size subset of mutually compatible activities
ACTIVITY SCHEDULING
PROBLEM
Formally:
Input: A set of activities,
o Given a set of activities
o = start time of activity and
o = finish time activity
o where
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
can be scheduled
4
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
can be scheduled
4
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
can be scheduled
4
o shortest interval
ACTIVITY SCHEDULING
PROBLEM
The greedy choice can be applied to find solutions (the maximum number
of activities that can be performed) by using
o shortest interval
ACTIVITY SCHEDULING
PROBLEM
The greedy choice can be applied to find solutions (the maximum number
of activities that can be performed) by using
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 , 𝑎 4 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 , 𝑎 4 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 ,𝑎 4 ,𝑎 8 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴= {𝑎 1 ,𝑎 4 ,𝑎 8 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING
PROBLEM 1 2 3 4 5 6 7 8 9 10 11
2
1 3 0 5 3 5 6 8 8 2 12
4 5 6 7 8 9 10 11 12 13 14
3
4
𝐴={𝑎 1 ,𝑎 4 ,𝑎 8 , 𝑎 11 }
5
6
7
8
9
11 10
1
ACTIVITY SCHEDULING: EARLY
FINISH TIME
Greedy-Activity-Scheduling ()
𝐴= {𝑎 3 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 3 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 3 , 𝑎7 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 3 , 𝑎7 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴={𝑎3 , 𝑎7 , 𝑎 11 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴={𝑎3 , 𝑎7 , 𝑎 11 }
11 10 9 8 7 6 5 4 3 2
Not an optimal
solution
1
ACTIVITY SCHEDULING:
SHORTEST INTERVAL
Select the activity with the shortest interval
Eliminate the activities that could not be scheduled
Repeat!
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 2 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 3 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 3 , 𝑎11 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴={𝑎3 , 𝑎11 ,𝑎 4 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴={𝑎3 , 𝑎11 ,𝑎 4 }
11 10 9 8 7 6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {}
6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 2 }
6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 2 , 𝑎4 }
6 5 4 3 2
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 2 , 𝑎4 }
Not an optimal
6 5 4 3 2
solution
1
ACTIVITY SCHEDULING: EARLY
START TIME
Select the activity with the earliest start
Eliminate the activities that could not be scheduled
Repeat!
𝐴= {𝑎 2 , 𝑎4 }
Not an optimal
6 5 4 3 2
solution
1
Optimal solution is
CONTENT COURTESY
Dr. Abul Kashem Mia, Professor, CSE, BUET
Sukarna Barua, Assistant Professor, CSE, BUET