Lecture 6
Lecture 6
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
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
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
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
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
𝑆 → 𝑆1 → 𝑆2 → 𝑆3 → ⋯ → 𝐴
such that
• each solution is “close to” the another solution
• the solution is improving.
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
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
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
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.
18