0% found this document useful (0 votes)
35 views

Lecture 6

The document discusses greedy algorithms for minimizing lateness and caching problems. It describes how earliest deadline first provides an optimal greedy solution for minimizing lateness. It also describes how farthest-in-future provides an optimal offline caching strategy that minimizes cache misses.

Uploaded by

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

Lecture 6

The document discusses greedy algorithms for minimizing lateness and caching problems. It describes how earliest deadline first provides an optimal greedy solution for minimizing lateness. It also describes how farthest-in-future provides an optimal offline caching strategy that minimizes cache misses.

Uploaded by

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

CSE 421

Greedy Algorithms / Minimizing Lateness

Yin Tat Lee

1
Scheduling to Minimizing Lateness
• Similar to interval scheduling.
• Instead of start and finish times, request 𝒊 has
➢ Time Requirement 𝒕𝒊 which must be scheduled in a
contiguous block
➢ Deadline 𝒅𝒊 by which time the request would like to
be finished
• Requests are scheduled into 1 2 3 4 5 6

time intervals [𝒔𝒊 , 𝒇𝒊 ] s.t. 𝒕𝒊 = 𝒇𝒊 − 𝒔𝒊 . 𝑡𝑗 3 2 1 4 3 2

• Lateness for request 𝒊 is 𝑑𝑗 6 8 9 9 14 15


• If 𝒅𝒊 < 𝒇𝒊 then request 𝒊 is late by 𝑳𝒊 = 𝒇𝒊 − 𝒅𝑖 otherwise its
lateness 𝑳𝒊 = 𝟎
• Goal: Find a schedule that minimize the
Maximum lateness 𝑳 = 𝒎𝒂𝒙 𝑳𝒊
𝒊
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
Minimizing Lateness:
Greedy Algorithms
Greedy template. Consider jobs in some order.
• [Shortest processing time first]
Consider jobs in ascending order of processing time 𝒕𝒋 .
1 2
tj 1 10
d 100 10 counterexample
• [Smallest slack] j

Consider jobs in ascending order of slack 𝒅𝒋 − 𝒕𝒋 .


1 2

tj 1 10
dj 2 10
counterexample
• [Earliest deadline first]
Consider jobs in ascending order of deadline 𝒅𝒋 .
3
Greedy Algorithm:
Earliest Deadline First
Sort deadlines in increasing order (𝒅𝟏 ≤ 𝒅𝟐 ≤ ⋯ ≤ 𝒅𝒏 )
𝒇𝟎
for 𝒊 ← 𝟏 to 𝒏 to
𝒔𝒊  𝒇
𝒇𝒊  𝒔 𝒊 + 𝒕 𝒊
𝒇  𝒇𝒊
end for
1 2 3 4 5 6
𝑡𝑗 3 2 1 4 3 2
𝑑𝑗 6 8 9 9 14 15

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

4
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.
5
Proof for Greedy Algorithm:
Exchange Argument

• We will show that if there is another schedule 𝑶 (think


optimal schedule) then we can gradually change 𝑶 so that
• at each step the maximum lateness in 𝑶 never gets
worse.
• it eventually becomes the same cost as 𝑨 (by greedy).

6
Minimizing Lateness: Inversions
Definition
• An adjacent inversion in schedule 𝑺 is a pair of jobs 𝒊 and 𝒋
such that
• 𝒅𝒊 < 𝒅𝒋
• Job 𝒊 is scheduled immediately after Job 𝒋

inversion

before swap j i

Observation
• Greedy schedule has no adjacent inversions.
7
Minimizing Lateness: Inversions
Definition
• An adjacent inversion in schedule 𝑺 is a pair of jobs 𝒊
and 𝒋 such that
• 𝒅 𝒊 < 𝒅𝒋
• Job 𝒊 is scheduled immediately after Job 𝒋
inversion
𝒇𝒊
before swap j i

after swap i j
𝒇′𝒋
Claim
• Swapping two adjacent, inverted jobs reduces the
number of inversions by one and does not increase the
max lateness. 8
Minimizing Lateness: Inversions

Lemma: Swapping two adjacent, inverted jobs


does not increase the maximum lateness.
inversion
𝒇𝒊
before swap j i

after swap i j
𝒇′𝒋
Proof: Let 𝑶′ be the schedule after swapping.
• Lateness 𝑳𝒊’ ≤ 𝑳𝒊 since 𝒊 is scheduled earlier in 𝑶’ than in 𝑶
• Requests 𝒊 and 𝒋 together occupy the same total time slot in both
schedules
• All other requests 𝒌 ≠ 𝒊, 𝒋 have 𝑳𝒌’ = 𝑳𝒌
• 𝒇𝒋’ = 𝒇𝒊 so 𝑳′𝒋 = 𝒇′𝒋 − 𝒅𝒋 = 𝒇𝒊 − 𝒅𝒋 < 𝒇𝒊 − 𝒅𝒊 = 𝑳𝒊
• Maximum lateness has not increased!
9
Optimal schedules and inversions

Claim: There is an optimal schedule with no idle time and no


inversions
Proof:
• By previous argument there is an optimal schedule 𝑶 with
no idle time
• If 𝑶 has an inversion then it has a consecutive pair of
requests in its schedule that are inverted and can be
swapped without increasing lateness
• Eventually these swaps will produce an optimal schedule
with no inversions
• Each swap decreases the number of inversions by 1
• There are at most 𝒏(𝒏 − 𝟏)/𝟐 inversions.
(we only care that this is finite.) 14
Idleness and Inversions are
the only issue
Claim: All schedules with no inversions and no
idle time have the same maximum lateness
Proof:
• Schedules can differ only in how they order
requests with equal deadlines
• Consider all requests having some common
deadline 𝒅
• Maximum lateness of these jobs is based only on
the finish time of the last of these jobs but the set of
these requests occupies the same time segment in
both schedules
• Last of these requests finishes at the same time
in any such schedule.
11
Why Exchange Argument?

Greedy cannot handle problems with many local minimum.

Let 𝑆 be any solution and 𝐴 be the solution given by greedy.

Exchange argument gives a sequence

𝑆 → 𝑆1 → 𝑆2 → 𝑆3 → ⋯ → 𝐴

such that
• each solution is “close to” the another solution
• the solution is improving.

It basically proves that there is no local min.


12
CSE 421

Greedy Algorithms / Caching Problem

Yin Tat Lee

13
Optimal Caching/Paging
Memory systems
• Many levels of storage with different access times
• Smaller storage has shorter access time
• To access an item it must be brought to the lowest
level of the memory system

Consider the problem between 2 levels


• Main memory with 𝒏 data items
• Cache can hold 𝒌 < 𝒏 items
• Assume no restrictions about where items can be
• Suppose cache is full initially
➢Holds 𝒌 data items to start with

14
Optimal Offline Caching
Caching
• Cache with capacity to store 𝒌 items.
• Sequence of 𝒎 item requests 𝒅𝟏 , 𝒅𝟐 , ⋯ , 𝒅𝒎 .
• Cache hit: item already in cache when requested.
• Cache miss: item not already in cache when requested:
must bring requested item into cache, and evict some existing
item, if full.

a a b
Goal
b a b
• Eviction schedule that minimizes number
c c b
of evictions.
b c b
c c b
Example: 𝒌 = 𝟐, initial cache = 𝒂, 𝒃, a a b
requests: 𝒂, 𝒃, 𝒄, 𝒃, 𝒄, 𝒂, 𝒂, 𝒃. a a b
b a b
Optimal eviction schedule: 𝟐 cache misses. requests cache
15
Why 2 is optimal?
Optimal Offline Caching:
Farthest-In-Future
Which item we should evict?
Farthest-in-future
• Evict item in the cache that is not requested until farthest
in the future.
current cache: a b c d e f

future queries: g a b c e d a b b a c d e a f a d e f g h ...

cache miss eject this one

Theorem
• [Bellady, 1960s] FIF is an optimal eviction schedule.

Exchange Argument
• We can swap choices to convert other schedules to
Farthest-In-Future without losing quality 16
Warm up (𝑛 = 𝑘 + 1)

Farthest-in-future
• Evict item in the cache that is not requested until farthest
in the future.
current cache: a b c d e f

future queries: g a b c e d a b b a c d e a f a d e f g h ...

cache miss eject this one

When 𝑛 = 𝑘 + 1,
between the cache miss and the farthest-item in the future,
“g a b c e d a b b a c d e a f”
contains all the item.
Hence, any algorithm must miss once.

17
Online Caching
• Online vs. offline algorithms.
Offline: full sequence of requests is known a priori.
Online (reality): requests are not known in advance.
Caching is among most fundamental online problems in CS.

• LIFO. Evict page brought in most recently.


• LRU. Evict page whose most recent access was earliest.
FIF with direction of time reversed!

• Theorem. FIF is optimal offline eviction algorithm.


Provides basis for understanding and analyzing online algorithms.
LRU is k-competitive. [Section 13.8]
LIFO is arbitrarily bad.

18

You might also like