Greedy Algorithms
● Usedto solve optimization problems
● Always makes the choice that looks best at the moment
● When a greedy algorithm leads to an optimal solution, it
is because a locally optimal choice leads to a globally
optimal solution.
● Usually simple and fast
● Implementation/running time analysis is typically
straightforward
■ Often implementation involves use of a sorting algorithm or a data structure
to facilitate identification of next greedy choice
● Proof of optimality is typically the hard part
3.
Elements of GreedyAlgorithms
● Question: how do we know that a greedy
algorithm always finds the optimum for a
given problem?
● Answer: No general way. However,
problems that can be greedily solved
typically have:
■ Optimal substructure
■ Greedy-choice property
4.
Elements of GreedyAlgorithms
● A problem exhibits optimal substructure if an
optimal solution contains within it optimal solutions
to subproblems
■ Build an optimal solution from optimal solutions to
subproblems
● A problem has greedy-choice property if a global
optimal solution can be arrived by making a locally
optimal greedy choice at each step
■ Always make the choice that look best at the moment
Task Scheduling
● Thetask scheduling problem: Schedule unit-
time tasks with deadlines and penalties s.t. the
total penalty for missed deadlines is
minimized.
■ S = {1, 2, …, n} of n unit-time tasks.
■ Deadlines d1, d2, …, dn for tasks, 1 di n.
■ Penalties w1, w2, …, wn : wi is incurred if task i misses
deadline.
7.
Consider a givenSchedule
● A task is late if it finishes after its deadline.
Otherwise, its early.
● A schedule can always be put in early first
form
● A schedule can always be put in canonical
form which is in early first form & tasks are
arranged in monotonically increasing
deadlines.
8.
● Set Aof tasks is independent if a schedule
with no late tasks.
● Nt(A): number of tasks in A with deadlines t or
earlier, t = 1, 2, …, n.
● Three equivalent statements for any set of tasks
A
1. A is independent.
2. Nt(A) t, t = 1, 2, …, n.
3. If the task in A are scheduled in order of nondecreasing
deadlines, then no task is late.
9.
Greedy Algorithm: TaskScheduling
● The optimal greedy scheduling algorithm:
1. Sort penalties in non-increasing order.
2. Find tasks of independent sets: no late task in the sets.
3. Schedule tasks in a maximum independent set in order
of nondecreasing deadlines.
4. Schedule other tasks (missing deadlines) at the end
arbitrarily.
Problem
There are 5jobs whose profits (p1..p5)=(20, 15,
10, 5, 1) and deadlines (d1..d5) = (2, 2, 1, 3,
3). Find the optimal solution that maximizes
profit on scheduling these jobs.
The Knapsack Problem
●The famous knapsack problem:
■ A thief breaks into a museum. Fabulous paintings,
sculptures, and jewels are everywhere. The thief has
a good eye for the value of these objects, and knows
that each will fetch hundreds or thousands of dollars
on the clandestine art collector’s market. But, the
thief has only brought a single knapsack to the scene
of the robbery, and can take away only what he can
carry. What items should the thief take to maximize
the haul?
14.
The Knapsack Problem
●More formally, the 0-1 knapsack problem:
■ The thief must choose among n items, where the ith item
worth vi dollars and weighs wi pounds
■ Carrying at most W pounds, maximize value
○ Note: assume vi, wi, and W are all integers
○ “0-1” each item must be taken or left in entirety
● A variation, the fractional knapsack problem:
■ Thief can take fractions of items
■ Think of items in 0-1 problem as gold ingots, in fractional
problem as buckets of gold dust
15.
The Knapsack Problem
AndOptimal Substructure
● Both variations exhibit optimal substructure
● To show this for the 0-1 problem, consider the
most valuable load weighing at most W pounds
■ If we remove item j from the load, what do we know
about the remaining load?
■ A: remainder must be the most valuable load
weighing at most W - wj that thief could take from
museum, excluding item j
16.
The Knapsack Problem
AndOptimal Substructure
● Fractional Knapsack
■ Consider optimal load of weight W
■ If we remove weight w of item j , the remaining
load is the optimal load weighing W-w that the
thief can take from the original n-1 original
items plus wj - w pounds of item j
17.
Fractional KS canbe Solved Using Greedy
● What is the greedy selection criterion?
■ Pick the heaviest item first?
■ Pick the lightest item first?
● Greedy strategy: take in order of dollars/pound
(benefit/weight)
● The optimal solution to the 0-1 problem cannot
be found with the same greedy strategy
18.
Greedy Algorithm
Item 1$60
Item 2 l
b
$100
Item 3 l
b
$120
Knapsack
W = 50 lb
l
b
l
b
l
b l
b
Optimal
Solution
19.
Greedy Does NotWork for 0-1 KS
Item 1 l
b
$60
Item 2 l
b
$100
Item 3 l
b
$120
l
b
l
b
l
b
l
b
l
b
l
b
$220 $160 $180
20.
The Knapsack Problem:
GreedyVs. Dynamic
● The fractional problem can be solved greedily
● The 0-1 problem cannot be solved with a
greedy approach
■ It can be solved with dynamic programming
21.
Greedy Algorithm forFractional
Knapsack Problem
Greedy-fractional-knapsack (w, v, W)
FOR i =1 to n
do x[i] =0
weight = 0
while weight < W
do i = best remaining item
IF weight + w[i] ≤ W
then x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
return x
22.
Example 1
● Findthe optimal solution to the following
knapsack instance where W = 60
Item A B C D
Profit 280 100 120 120
Weight 40 10 20 24
23.
Step 1: Computeprofit/weight ratio
•A: 280/40=7280/40 = 7280/40=7
•B: 100/10=10100/10 = 10100/10=10
•C: 120/20=6120/20 = 6120/20=6
•D: 120/24=5120/24 = 5120/24=5
Order by ratio (descending):
B (10) > A (7) > C (6) > D (5)
24.
Step 2: Fillknapsack greedily
Capacity = 60
1.Take B fully (weight 10, profit 100).
Remaining capacity = 50.
2.Take A fully (weight 40, profit 280).
Remaining capacity = 10.
3.Next is C (weight 20, profit 120). But only 10 capacity
left → take 10/20 = 0.5 fraction of C.
Profit = 120×0.5=60120 times 0.5 = 60120×0.5=60.
Now capacity = 0 (full).
25.
Optimal solution (fractionalknapsack):
•Take B fully,
•Take A fully,
•Take half of C,
•Total profit = 440.
Step 3: Total profit
100+280+60=440
26.
Example 2
● Findthe optimal solution to the following
knapsack instance
■ n=7
■ m=15
■ (p1, p2 …. p7) = (10, 5, 15, 7, 6, 18, 3)
■ (w1,w2….. w7) = (2, 3, 5, 7, 1, 4, 1)