2.2.1 GreedyAlgorithms 2
2.2.1 GreedyAlgorithms 2
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
ACTIVITY SCHEDULING
PROBLEM
Input: A set of activities,
o Each activity has a start time and a finish time where
o By doing each job, you will get a constant amount of reward
o You want to maximize your rewards in a day
o This means you want to do as many jobs (e.g., classes) as possible
o Surely, we will do all jobs if it is possible. But job times will overlap, and it is not possible to do all
jobs.
o Some jobs will have to be sacrificed for other jobs
o Your task is to find the maximum number of doable jobs
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
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
2
4 5 6 7 8 9 10 11 12 13 14
3
4
5
6
7
8
9
11 10
ACTIVITY SCHEDULING
PROBLEM
1 2 3 4 5 6 7 8 9 10 11
1
1 3 0 5 3 5 6 8 8 2 12
2
4 5 6 7 8 9 10 11 12 13 14
3
𝐴={𝑎1 , 𝑎 4 , 𝑎 8 ,𝑎 11 }
4
5
6
7
8
9
11 10
ACTIVITY SCHEDULING: EARLY
FINISH TIME
Greedy-Activity-Scheduling ()
Which items
should I take?
THE FAMOUS KNAPSACK
PROBLEM
There are two versions of the problem:
The thief must choose among items, where the item worth vi dollars and weighs wi pounds
Carrying at most pounds, maximize value
Note: assume vi, wi, and W are all integers
“0-1” because each item must be taken or left in entirety
Think of items in 0-1 problem as gold ingots, in fractional problem as buckets of gold dust.
FRACTIONAL KNAPSACK
PROBLEM
Knapsack capacity:
There are items: item has a value of and wight
Goal:
Find such that for all
o
o is maximum
FRACTIONAL KNAPSACK
PROBLEM
Greedy strategy:
Pick the item with the maximum value per pound vi/wi
If the supply of that element is exhausted and the thief can carry more: take as much as
possible from the item with the next greatest value per pound
It is good to order items based on their value per pound
FRACTIONAL KNAPSACK
PROBLEM
Item 1 30 1 10 60 6
20
10 2 20 100 5
$60 $100 $120 3 30 120 4
$6/pound $5/pound $4/pound
FRACTIONAL KNAPSACK
PROBLEM
Item 1 30 1 10 60 6
20
10 2 20 100 5
$60 $100 $120 3 30 120 4
$6/pound $5/pound $4/pound
FRACTIONAL KNAPSACK
PROBLEM
Item 1 30 1 10 60 6 1
20
10 10 $60 2 20 100 5
$60 $100 $120 $60 3 30 120 4
$6/pound $5/pound $4/pound
FRACTIONAL KNAPSACK
PROBLEM
8 7
1 2 3
4 9
2
11 4
0 8 14 4
7
6
8 10
7 6 5
1 2
FINDING MST: KRUSKAL’S
ALGORITHM
8 7
1 2 3
4 9
2
Line 4 sorts the edges in time
11 4
0 8 14 4
Running time can be written as 7
6
8 10
7 6 5
1 2
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,3,4, 2
5,6,7,8 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
1,2,3,4, 2
0
5,6,7,8 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
2,3,4, 2
0,1
5,6,7,8 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
3,4, 2
0,1,2
5,6,7,8 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
3,4, 2
0,1,2,8
5,6,7 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
3,4, 2
0,1,2,8,5
6,7 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
3,4, 0,1,2,8,5, 2
7 6 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
3,4
6,7 4
0 11 8 14 4
7
6
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
3,4
6,7 4
0 11 8 14 4
7
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
4
6,7, 3 4
0 11 8 14 4
7
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
4
6,7, 3 4
0 11 8 14 4
8 10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
4
6,7, 3 4
0 11 8 14 4
10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
6,7, 3,4 4
0 11 8 14 4
10
7 6 5
1 2
unexplored explored
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
0,1,2,8,5, 2
6,7, 3,4 4
0 8 4
7 6 5
1 2
unexplored explored
MST with cost = 37
FINDING MST: PRIME’S
ALGORITHM
8 7
1 2 3
4 9
2
11 4
0 8 14 4
7
6
8 10
7 6 5
1 2
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
FINDING MST: PRIME’S
ALGORITHM
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
FINDING MST: PRIME’S
ALGORITHM
Fibonacci-Heap
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation
FINDING MST: PRIME’S
ALGORITHM
Fibonacci-Heap
BUILD-MIN-HEAP
times
each EXTRACT-MIN will take time
𝑂¿
DECREASE-KEY operation