







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








1
3
Interval scheduling. ļ® Job j starts at sj and finishes at fj. ļ® Two jobs compatible if they don't overlap. ļ® Goal: find maximum subset of mutually compatible jobs. Time 0 1 2 3 4 5 6 7 8 9 10 11 f g h e a b c d 4
Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. ļ® [Earliest start time] Consider jobs in ascending order of sj. ļ® [Earliest finish time] Consider jobs in ascending order of fj. ļ® [Shortest interval] Consider jobs in ascending order of fj - sj. ļ® [Fewest conflicts] For each job j, count the number of conflicting jobs cj. Schedule in ascending order of cj.
5
Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. counterexample for earliest start time counterexample for shortest interval counterexample for fewest conflicts 6 Greedy algorithm. Consider jobs in increasing order of finish time. Take each job provided it's compatible with the ones already taken. Implementation. O(n log n). ļ® Remember job j* that was added last to A. ļ® Job j is compatible with A if sj ā„ fj*. Sort jobs by finish times so that f 1 ⤠f 2 ⤠... ⤠fn. A ā Ļ for j = 1 to n { if (job j compatible with A) A ā A āŖ {j} } return A set of jobs selected
7
Theorem. Greedy algorithm is optimal. Pf. (by contradiction) ļ® Assume greedy is not optimal, and let's see what happens. ļ® Let i 1 , i 2 , ... ik denote set of jobs selected by greedy. ļ® Let j 1 , j 2 , ... jm denote set of jobs in the optimal solution with i 1 = j 1 , i 2 = j 2 , ..., ir = jr for the largest possible value of r. j 1 j 2 jr i 1 i 2 ir ir+
... Greedy: OPT: jr+ why not replace job jr+ with job ir+1? job ir+1 finishes before jr+ 8 j 1 j 2 jr i 1 i 2 ir ir+
Theorem. Greedy algorithm is optimal. Pf. (by contradiction) ļ® Assume greedy is not optimal, and let's see what happens. ļ® Let i 1 , i 2 , ... ik denote set of jobs selected by greedy. ļ® Let j 1 , j 2 , ... jm denote set of jobs in the optimal solution with i 1 = j 1 , i 2 = j 2 , ..., ir = jr for the largest possible value of r.
... Greedy: OPT: solution still feasible and optimal, but contradicts maximality of r. ir+ job ir+1 finishes before jr+
13
Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation. O(n log n). ļ® For each classroom k, maintain the finish time of the last job added. ļ® Keep the classrooms in a priority queue. Sort intervals by starting time so that s 1 ⤠s 2 ⤠... ⤠sn. d ā 0 for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d ā d + 1 } number of allocated classrooms 14
Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Pf. ļ® Let d = number of classrooms that the greedy algorithm allocates. ļ® Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. ļ® These d jobs each end after sj. ļ® Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than sj. ļ® Thus, we have d lectures overlapping at time sj + ε. ļ® Key observation ā all schedules use ā„ d classrooms. āŖ
16
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: lj = max { 0, fj - dj }. ļ® Goal: schedule all jobs to minimize maximum lateness L = max lj. Ex: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 d 3 = 9 d 2 = 8 d 6 = 15 d 1 = 6 d 5 = 14 d 4 = 9 lateness = 2 lateness = 0 dj 6 tj 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6 max lateness = 6
17
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. 18 Greedy template. Consider jobs in some order. ļ® [Shortest processing time first] Consider jobs in ascending order of processing time tj. ļ® [Smallest slack] Consider jobs in ascending order of slack dj - tj. counterexample counterexample dj tj 100 1 1 10 10 2 dj tj 2 1 1 10 10 2
19 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 max lateness = 1 Sort n jobs by deadline so that d 1 ⤠d 2 ⤠⦠⤠dn t ā 0 for j = 1 to n Assign job j to interval [t, t + tj] sj ā t, fj ā t + tj t ā t + tj output intervals [sj, fj]
Greedy algorithm. Earliest deadline first. 20
Observation. There exists an optimal schedule with no idle time. Observation. The greedy schedule has no idle time. 0 1 2 3 4 5 6 d = 4 d = 6 7 8 9 10 11 d = 12 0 1 2 3 4 5 6 d = 4 d = 6 7 8 9 10 11 d = 12
26
Caching. ļ® Cache with capacity to store k items. ļ® Sequence of m item requests d 1 , d 2 , ā¦, dm. ļ® 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. Goal. Eviction schedule that minimizes number of cache misses. Ex: k = 2, initial cache = ab, requests: a, b, c, b, c, a, a, b. Optimal eviction schedule: 2 cache misses. a b a b c b c b c b a b a b c b c a a a b b a b requests cache red = cache miss 27
Farthest-in-future. Evict item in the cache that is not requested until farthest in the future. Theorem. [Bellady, 1960s] FF is optimal eviction schedule. Pf. Algorithm and theorem are intuitive; proof is subtle. a b g a b c e d a b b a c d e a f a d e f g h ... current cache: c d e f future queries: cache miss eject this one 28
Def. A reduced schedule is a schedule that only inserts an item into the cache in a step in which that item is requested. Intuition. Can transform an unreduced schedule into a reduced one with no more cache misses. a x an unreduced schedule c a d c a d b a c b a x b a c b a b c a b c a c d a b c a a a b a reduced schedule c a b c a d c a d c a d b a c b a c b a c b a c d a b c a a a a b c a a b c
29
Claim. Given any unreduced schedule S, can transform it into a reduced schedule S' with no more cache misses. Pf. (by induction on number of unreduced items) ļ® Suppose S brings d into the cache at time t, without a request. ļ® Let c be the item S evicts when it brings d into the cache. ļ® Case 1: d evicted at time t', before next request for d. ļ® Case 2: d requested at time t' before d is evicted. āŖ t t' d c t t' c S' d S d requested at time t' t t' d c t t' c S' e S d evicted at time t', before next request e doesn't enter cache at requested time Case 1 Case 2 30
Theorem. FF is optimal eviction algorithm. Pf. (by induction on number or requests j) Let S be reduced schedule that satisfies invariant through j requests. We produce S' that satisfies invariant after j+1 requests. ļ® Consider (j+1)st^ request d = dj+1. ļ® Since S and SFF have agreed up until now, they have the same cache contents before request j+1. ļ® Case 1: (d is already in the cache). S' = S satisfies invariant. ļ® Case 2: (d is not in the cache and S and SFF evict the same element). S' = S satisfies invariant. Invariant: There exists an optimal reduced schedule S that makes the same eviction schedule as SFF through the first j+1 requests. 31 j
Pf. (continued) ļ® Case 3: (d is not in the cache; SFF evicts e; S evicts f ā e).
Let j' be the first time after j+1 that S and S' take a different action, and let g be item requested at time j'. ļ® Case 3a: g = e. Can't happen with Farthest-In-Future since there must be a request for f before e. ļ® Case 3b: g = f. Element f can't be in cache of S, so let e' be the element that S evicts.
37
Dijkstra's algorithm. ļ® Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. ļ® Initialize S = { s }, d(s) = 0. ļ® Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = Ļ(v). ( ) min () , e ( u , v ): u S e v = du +l =!
s v u d(u) S le shortest path to some u in explored part, followed by a single edge (u, v) 38
Dijkstra's algorithm. ļ® Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. ļ® Initialize S = { s }, d(s) = 0. ļ® Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = Ļ(v). ( ) min () , e ( u , v ): u S e v = du +l =!
s v u d(u) shortest path to some u in explored part, followed by a single edge (u, v) S le 39
Invariant. For each node u ā S, d(u) is the length of the shortest s-u path. Pf. (by induction on |S|) Base case: |S| = 1 is trivial. Inductive hypothesis: Assume true for |S| = k ā„ 1. ļ® Let v be next node added to S, and let u-v be the chosen edge. ļ® The shortest s-u path plus (u, v) is an s-v path of length Ļ(v). ļ® Consider any s-v path P. We'll see that it's no shorter than Ļ(v). ļ® Let x-y be the first edge in P that leaves S, and let P' be the subpath to x. ļ® P is already too long as soon as it leaves S. l (P) ā„ l (P') + l (x,y) ā„ d(x) + l (x, y) ā„ Ļ(y) ā„ Ļ(v) nonnegative weights inductive hypothesis defn of Ļ(y) (^) Dijkstra chose v instead of y S s y v x P u P' 40
For each unexplored node, explicitly maintain ļ® Next node to explore = node with minimum Ļ(v). ļ® When exploring v, for each incident edge e = (v, w), update Efficient implementation. Maintain a priority queue of unexplored nodes, prioritized by Ļ(v). ā Individual ops are amortized bounds PQ Operation Insert ExtractMin ChangeKey Binary heap log n log n log n Fib heap ā 1 log n 1 Array n n 1 IsEmpty 1 1 1 Priority Queue Total n^2 m log n m + n log n Dijkstra n n m n d-way Heap d log (^) d n d log (^) d n log (^) d n 1 m log (^) m/n n
" ( v ) = min e = ( u , v ) : u # S d ( u ) + l (^) e. ! " ( w ) = min { " ( w ), " ( v ) + l (^) e }.
41
The question of whether computers can think is like the question of whether submarines can swim. Do only what only you can do. In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums.
Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit.
Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins. Ex: 34Ā¢. Cashier's algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Ex: $2.89.
49
Selecting breakpoints. ļ® Road trip from Princeton to Palo Alto along fixed route. ļ® Refueling stations at certain points along the way. ļ® Fuel capacity = C. ļ® Goal: makes as few refueling stops as possible. Greedy algorithm. Go as far as you can before refueling. Princeton (^) Palo Alto 1 C C 2 C 3 C 4 C 5 C 6 C 7 50 Truck driver's algorithm. Implementation. O(n log n) ļ® Use binary search to select each breakpoint p.
Sort breakpoints so that: 0 = b 0 < b 1 < b 2 < ... < bn = L S ā {0} x ā 0 while (x ā bn) let p be largest integer such that bp ⤠x + C if (bp = x) return "no solution" x ā bp S ā S āŖ {p} return S breakpoints selected current location 51
Theorem. Greedy algorithm is optimal. Pf. (by contradiction) ļ® Assume greedy is not optimal, and let's see what happens. ļ® Let 0 = g 0 < g 1 <... < gp = L denote set of breakpoints chosen by greedy. ļ® Let 0 = f 0 < f 1 <... < fq = L denote set of breakpoints in an optimal solution with f 0 = g 0 , f 1 = g 1 ,... , fr = gr for largest possible value of r. ļ® Note: gr+1 > fr+1 by greedy choice of algorithm.
... Greedy: OPT: g 0 g 1 g 2 f 0 f 1 f 2 fq gr fr why doesn't optimal solution drive a little further? gr+ fr+ 52
Theorem. Greedy algorithm is optimal. Pf. (by contradiction) ļ® Assume greedy is not optimal, and let's see what happens. ļ® Let 0 = g 0 < g 1 <... < gp = L denote set of breakpoints chosen by greedy. ļ® Let 0 = f 0 < f 1 <... < fq = L denote set of breakpoints in an optimal solution with f 0 = g 0 , f 1 = g 1 ,... , fr = gr for largest possible value of r. ļ® Note: gr+1 > fr+1 by greedy choice of algorithm. another optimal solution has one more breakpoint in common ā contradiction
... Greedy: OPT: g 0 g 1 g 2 f 0 f 1 f 2 fq gr fr gr+