0% found this document useful (0 votes)
2 views18 pages

Lecture 5 A

The document outlines important dates for a midterm exam and provides an overview of greedy algorithms, specifically focusing on the knapsack problem and scheduling to minimize lateness. It discusses two versions of the knapsack problem (0-1 and fractional), and presents examples of how to apply greedy algorithms to solve these problems. Additionally, it covers the concept of minimizing lateness in scheduling jobs, including the greedy approach and theorems proving its optimality.

Uploaded by

chunwan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Lecture 5 A

The document outlines important dates for a midterm exam and provides an overview of greedy algorithms, specifically focusing on the knapsack problem and scheduling to minimize lateness. It discusses two versions of the knapsack problem (0-1 and fractional), and presents examples of how to apply greedy algorithms to solve these problems. Additionally, it covers the concept of minimizing lateness in scheduling jobs, including the greedy approach and theorems proving its optimality.

Uploaded by

chunwan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Important dates:

Midterm exam: 4:00pm to 6:00 pm,


Week 9, Friday, (Nov. 1, 2024)

03/04/2025 DENG Xiaotie 1


Lecture 5: Greedy Algorithm
More Examples

03/04/2025 DENG Xiaotie 2


Knapsack problem:
Input: n items: (w1, v1), …, (wn, vn) and a number W
– the i-th item’s is worth vi dollars with weight wi.
– at most W pounds to be carried.
Output: Some items which are most valuable and
with total weight at most W.
• Two versions:
– 0-1 Knapsack: Items cannot be divided into pieces
– fractional knapsack: Items can be divided into pieces

CS4335 Design and Analysis of


03/04/2025 Page 3
Algorithms/WANG Lusheng
Fractional Knapsack problem
How many units to take?
15-4-1-1-2=7

CS4335 Design and Analysis of


03/04/2025 Page 4
Algorithms/WANG Lusheng
Another Example
a b c d e f
11 kg 3kg 4kg 58kg 8kg 88kg
$3 $6 $35 $8 $28 $66
W=20kg

First, compute unit prices


$0.27 $2.00 $8.75 $0.11 $3.50 $0.75
Sort the items according to prices, and then take
items until the bucket is full.
(c, 4kg), (e, 8kg), (b, 3kg), (f, 5kg)
CS4335 Design and Analysis of
03/04/2025 Page 5
Algorithms/WANG Lusheng
fractional Knapsack Algorithm
• Greedy on the unit price: vi/wi.
– Each time, take the item with maximum vi/wi.
– If exceeds W, take fractions of the item.

CS4335 Design and Analysis of


03/04/2025 Page 6
Algorithms/WANG Lusheng
Proof: (correctness, the hard part)
• Let X be an optimal set of items taken.
• G be the greedy solution.
• Consider the first item that the two solutions differ:
– Greedy: y% of item i
– X: Case 1: z% of item k or Case 2: y’% of item i with y’<y.
• Case 1: the unit price of item i ≥the unit price of item k.
– get rid of (part of) item k and add (the same weight of) item i to X.
– The total value will be increased.
• Case 2: Any item k that are not selected in greedy solution before item
i has the property: the unit price of item i ≥the unit price of item k.
– get rid of (part of) item k from X and add more part (maximum (y-y’)%) of
item i to X.
• If one item k is not enough to get the weight of (y-y’)% item I, we canuse
other item k that are not selected in greedy solution before i.

03/04/2025 The total value will be increased.
CS4335 Design and Analysis of
Page 7
Algorithms/WANG Lusheng
0-1 knapsack problem cannot be solved
optimally by greedy
• Counter example: (moderate part)
– We have three items:
• a:($10, 10kg), b:($10, 10kg), c:($12, 11kg)
• W=20kg
– Greedy approach will take c.
• Total value is $12.
– Optimal solution will take a and b
• Total value is $20.

CS4335 Design and Analysis of


03/04/2025 Page 8
Algorithms/WANG Lusheng
Scheduling to Minimize Lateness
Solving end of Semester blues
Term paper

Exam study

Party!

CS4335 HW
Max
Project lateness
=2

0 0 0 0 2
Party! Exam study CS4335 HW Term paper Project

Monday Tuesday Wednesday Thursday Friday


Scheduling to Minimizing Lateness
• Minimizing lateness problem.
– Single resource processes one job at a time.
– Job j requires tj units of processing time and is due at time dj.
– If j starts at time sj, it finishes at time fj = sj + tj.
– Lateness: j = max { 0, fj - dj }.
– Goal: schedule all jobs to minimize maximum lateness L = max j.
1 2 3 4 5 6
tj 3 2 1 4 3 2
• Ex: dj 6 8 9 9 14 15

lateness = 2 lateness = 0 max lateness = 6

d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 11
Minimizing Lateness: Greedy Algorithms
• Greedy template. Consider jobs in some order.

– [Shortest processing time first] Consider jobs in ascending


order of processing time tj.

– [Earliest deadline first] Consider jobs in ascending order of


deadline dj.

– [Smallest slack] Consider jobs in ascending order of slack dj -


tj.
Page 12
Minimizing Lateness: Greedy Algorithms
• Greedy template. Consider jobs in some order.

– [Shortest processing time first] Consider jobs in ascending order of


processing time tj. 1 2
tj 1 10
dj 100 10 counterexample
l2=0, l1=0 l1=0, l2=1
0 10 11
– [Smallest slack] Consider jobs in ascending order of slack dj - tj.

l1=0, l2=1 1 2
l1=9, l2=0
tj 1 10
0 1 11 dj 2 10
counterexample

Page 13
Minimizing Lateness: Greedy Algorithm
• Greedy algorithm. Earliest deadline first.
Sort n jobs by deadline so that d1  d2  … 
dn

t  0
for j = 1 to n 1 2 3 4 5 6

Assign job j to interval [t, t + tj] tj 3 2 1 4 3 2


dj 6 8 9 9 14 15
sj  t, fj  t + tj
t  t + tj
output intervals [sj, fj] max lateness = 1

d1 = 6 d2 = 8 d3 = 9 d4 = 9 d5 = 14 d6 = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 14
Minimizing Lateness: No Idle Time
• Observation. There exists an optimal schedule
with no idle time.
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11

d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11

• Observation. The greedy schedule has no idle


time.
Page 15
Minimizing Lateness: Inversions
• Def. An inversion in schedule S is a pair of jobs i and j such that:
di < dj but j scheduled before i.
inversion

before swap j i

• Observation. Greedy schedule has no inversions.

• Observation. If a schedule (with no idle time) has an inversion, it has one


with a pair of adjacent inverted jobs (adjacent inversion).

No adjacent inversion no inversion.


Page 16
Minimizing Lateness: Inversions
• Def. An inversion in schedule S is a pair of jobs i and j such that:
di < dj but j scheduled before i. inversion
fi

before swap j i
after swap i j
f'j

• Claim. Swapping two adjacent, inverted jobs reduces the number of inversions by
one and does not increase the max lateness.

• Pf. Let  be the lateness before the swap, and let  ' be it afterwards.
– 'k = k for all k  i, j j  f j  d j (definition)
– 'i  i  fi  d j ( j finishes at time f i )
– If ’ >0, then
i
 fi  di (di  dj )
 i (definition)
Page 17
• Theorem: The greedy is optimal.
Pf: Let G be the solution generated by the greedy
algorithm. Let Opt be an optimal solution.
If there is an adjacent inversion in Opt. We can
swap it. After swapping, the no. of inversion is
reduced and the total lateness is not increased.
After a number of swapping, Opt =G.

CS4335 Design and Analysis of


03/04/2025 Page 18
Algorithms/WANG Lusheng

You might also like