We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
Greedy Algorithms
Dr. AMIT KUMAR @JUETGreedy algorithms
* A greedy algorithm always makes the choice that
looks best at the moment
— My everyday examples:
* Driving
+ Playing cards
+ Invest on stocks
* Choose a university
— The hope: a locally optimal choice will lead to a
globally optimal solution
— For some problems, it works
* greedy algorithms tend to be easier to code
Dr. AMIT KUMAR @JUETAn Activity Selection Problem
(Conference Scheduling Problem)
Input: A set of activities S = {a,,..., a,}
Each activity has start time and a finish time
- a-(spf))
Two activities are compatible if and only if
their interval does not overlap
Output: a maximum-size subset of
mutually compatible activities
Dr AMIT KUMAR @JUETThe Activity Selection Problem
* Here are a set of start and finish times
i[i 2 3 4 5 6 7 8&8 9 0 UU
yj fl 3 0 5 3 5 6 8 8B 2
fi|4 5 6 7 8 8 0 Wt 12 13° «+
* What is the maximum number of activities that can be
completed?
* {a3, a9, a,;} can be completed
+ But so can {aj, a4, ag-a,,} which is a larger set
* But it is not unique, consider {a,, a4, 9-4\)}
Dr. AMIT KUMAR @JUETInterval Representation
23 4 6 7 8 9 100611
3°90 5 3 5 6 8 8 2 12,
S 6 7 9 10 I 12,13 «4
i |i
= fd
fil 4
Dr. AMIT KUMAR @JUET0 2 3 4 SbdesGurkfageug 10 11 12 132 3 4 SbdrsGurkfageug 10 11 12 132 3 4 SbdesGurkfageug 10 11 12 132 3 4 SbdesGurkfageug 10 11 12 13Early Finish Greedy
* Select the activity with the earliest finish
¢ Eliminate the activities that could not be
scheduled
* Repeat!
Dr. AMIT KUMAR @JUET0 2 3 4 SbdesGurkfageug 10 11 12 13i=
0 2 3 4 SbdresGurkfageug 10 11 12 132
3
4 SDL AGITEGMAaU
10 Wl 12 13 14—_—_-_
—_—_—_——
0 2 3 4 Soragireg@ageud 10 11 12 13 14—_—_
—_—_—_——
0 2 3 4 Soragireg@ageud 10 11 12 13 14Pee
0 2 3 4 Soragiregageud 10 11 12 13 140 203 4 Soragiregmagaugy 10 11 12 13 14 15Example:
The solution set = {1, 4, 8, 11}
Algorithm:
step: Sort inte nondecreasing order. After sorting, f,
sf, sf <... f;
then A — AU {a,,}
i each item is taken or not taken
Fractional knapsack > fractions of items can be taken
Both exhibit the optimal-substructure property
— 0-1: If item / is removed from an optimal packing, the remaining
packing is an optimal packing with weight at most W-w,
— Fractional: If w pounds of item is removed from an optimal
packing, the remaining packing is an optimal packing with weight
at most H/-w that can be taken from other n-/ items plus w;—w of
item Dr AMIT KUMAR @JUETGreedy Algorithm for Fractional
Knapsack problem
* Fractional knapsack can be solvable by the greedy
strategy
— Compute the value per pound v/v, for each item
— Obeying a greedy strategy, take as much as possible of the
item with the greatest value per pound.
— If the supply of that item is exhausted and there is still more
room, take as much as possible of the item with the next value
per pound, and so forth until there is no more room
— O(n lg 2) (we need to sort the items by value per pound)
— Greedy Algorithm?
— Correctness?
Dr. AMIT KUMAR @JUETThe Fractional knapsack problem
* n objects, each with a weight w, > 0
a profit p; > 0
capacity of knapsack: M
DPX
I