Download Greedy Algorithms - Program and Data Representation - Slides | CS 2150 and more Study notes Computer Science in PDF only on Docsity!
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans
Lecture 7:
Greedy
Algorithms
http://www.cs.virginia.edu/cs216 UVa CS216 Spring 2006 - Lecture 7: Greed is Good 2
Menu
“Life is uncertain. Eat dessert first.” Ernestine Ulmer
La vita e incerta - mangia il dolce per primo.
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 3
Greed is Good?
- Adam Smith, An Inquiry into the Nature and Causes of the Wealth of Nations (1776) “It is not from the benevolence of the butcher, the brewer, or the baker, that we expect our dinner, but from their regard to their own interests.”
- Invisible hand: individuals acting on personal greed produces (nearly) globally optimal results
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 4
Greedy Algorithms
- Make the locally best choice “myopically” at each step - Need to figure out what “dessert” is
- Hope it leads to a globally good solution - Sometimes, can prove it leads to an optimal solution - Other times (like phylogeny), non- optimal, but usually okay if you get lucky
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 5
Interval Scheduling Problem
- Input: R , a set of n resource
requests:
{< s 0 , f 0 >, < s 1 , f 1 >, …, < sn -1, f n -1>}
- Output: a subset S of R with no
overlapping requests ( si > sj < fj for
any < si , fi >, < sj , fj > ∈ S ) such that
| S | ≥ | T | for any T ⊆ R with no
overlapping requests
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 6
Example R = {<0, 3>, <1, 2>, <1, 5>, <2, 5>, <2.5, 4>, <4, 4.5> }
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 7
Solution R = {<0, 3>, <1, 2>, <1, 5>, <2, 5>, <2.5, 4>, <4, 4.5> }
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 8
Brute Force Algorithm
- Try all possible subsets
- Filter out ones with overlapping intervals
- Pick the largest subset
- Running time
- How many subsets?
- Constant work for each subset
- Θ(2 n )
2 n
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 9
Greedy Approaches
- Need to pick best subset by making myopic decisions, one element at a time
- Many possible criteria for making myopic decision - Earliest starting time? - Latest ending time? - Shortest?
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 10
Greedy Approach:
Earliest Starting
Not optimal: | S | < |best| = 3
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 11
Greedy Approach:
Earliest Finishing
Not optimal: | S | < |best| = 3
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 12
Greedy Approach:
Shortest Length
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 19
Induction Proof: fri ≤ fqj
- Basis: fr 0 ≤ fq 0
- Greedy algorithm choose r 0 as the element with the earliest finishing time
- So, fr 0 ≤ fj for all j
- Induction: fri -1 ≤ fqj -1 ⇒ fri ≤ fqj
- Since fri -1 ≤ fqj -1 we know sqi ≥ fri -
- So, greedy algorithm could choose qi
- If fqi < frj , greedy algorithm would have chosen fqj instead of frj
R = { r 0 , …, rk -1 } Q = { q 0 , …, qk -1, qk }
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 20
Knapsack Problems
- You have a collection of items, each has a value and weight
- How to optimally fill a knapsack with as many items as you can carry Scheduling: weight = time, one deadline for all tasks Budget allocation: weight = cost
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 21
General Knapsack Problem
- Input: a set of n items {, …, }, and maxweight
- Output: a subset of the input items such that the sum of the weights of all items in the output set is ≤ maxweight and there is no subset with weight sum ≤ maxweight with a greater value sum
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 22
Brute Force Knapsack def knapsack (items, maxweight): best = {} bestvalue = 0 for s in allPossibleSubsets (items): value = 0 weight = 0 for item in s: value += item.value weight += item.weight if weight <= maxweight: if value > bestvalue: best = s bestvalue = value return best
(Defining and analyzing this might be a good Exam 1 question)
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 23
Brute Force Knapsack Analysis
- How many subsets are there?
- How much work for each subset?
2 n
for item in s: value += item.value weight += item.weight Average size of each subset is n / (there are as many subsets with size c and of size n – c ) Running time ∈ Θ( n 2 n ) UVa CS216 Spring 2006 - Lecture 7: Greed is Good 24
Dynamic Programming
- Section this week: dynamic programming solution to the knapsack problem
- Running time in
O( maxweight * n )
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 25
Greedy Knapsack Algorithm
- Repeat until no more items fit:
- Add the most valuable item that fits
- “Greedy”: always picks the most valuable item that fits first
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 26
Greedy Knapsack Algorithm def knapsack_greedy (items, maxweight): result = [] weight = 0 while True:
try to add the best item
weightleft = maxweight - weight bestitem = None for item in items: if item.weight <= weightleft and (bestitem == None or item.value > bestitem.value): bestitem = item if bestitem == None: break else: result.append (bestitem) weight += bestitem.weight return result
Running Time ∈Θ( n^2 )
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 27
Is Greedy Algorithm Correct?
No. Proof by counterexample: Consider input items = {<“gold”, 100, 1 >, <“platinum”, 110, 3> <“silver”, 80, 2 >} maxweight = 3 Greedy algorithm picks {<“platinum”>} value = 110, but {<“gold”>, “silver”>} has weight <= 3 and value = 180 UVa CS216 Spring 2006 - Lecture 7: Greed is Good 28
Same Weights Knapsack Problem
- Input: a set of n items {, …, }, each having weight w , and maxweight
- Output: a subset of the input items such that the sum of the weights of all items in the output set is ≤ maxweight and there is no subset with weight sum ≤ maxweight with a greater value sum
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 29
Greedy Algorithm Correct
- It keeps adding items until maxweight would be exceeded, so the result contains k items where kw<= maxweight and (k+1)w > maxweight
- Hence, cannot add any item (weight w ) without removing another item
- But, any item with value > value of the lowest value item in result would have already been added by greedy algorithm
UVa CS216 Spring 2006 - Lecture 7: Greed is Good 30
Subset Sum Problem
- Knapsack problem where value = weight
- Input: set of n positive integers, { w 0 , …, wn -1}, maximum weight W
- Output: a subset S of the input set such that the sum of the elements of S ≤ W and there is no subset of the input set whose sum is greater than the sum of S and ≤ W