Download presentation

Presentation is loading. Please wait.

Published byKarla Delane Modified over 2 years ago

1
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 6

2
Recap: Reachability game Suppose you have a bipartite directed graph with nodes in the two partitions colored red and blue, and two players: Red and Blue. Red and Blue play a game where a token gets moved along edges of the graph. At each point, the player whose name matches the color of the current node pushes the token. Initially the token is at s (a red node). The objective of the game is that Red wants the token to avoid a certain set of blue nodes X. Blue wants the token to get to X at some point in the game; Red wants to avoid this. If the token gets to X at any point, the game is over and Blue wins. Aside from this there is no time bound on the game. Can you give an algorithm that, given the graph, s, and X, can tell if Red has a strategy to win this game? 2

3
Solution Iteratively grow a set S from which Blue can force Red to reach X The set we compute is called the attractor of S. We still need a proof! 3 S := X while (S S) { S := S Add to S every red node u such that ALL neighbors of u are in X Add to S every blue node v such that SOME neighbor of v is in X } Check if the initial node lies in S

4
Greedy algorithms 4.1 Interval Scheduling

5
5 Interval Scheduling Interval scheduling. n Job j starts at s j and finishes at f j. n Two jobs compatible if they don't overlap. n Goal: find maximum subset of mutually compatible jobs. Time 01234567891011 f g h e a b c d

6
6 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. n [Earliest start time] Consider jobs in ascending order of start time s j. n [Earliest finish time] Consider jobs in ascending order of finish time f j. n [Shortest interval] Consider jobs in ascending order of interval length f j - s j. n [Fewest conflicts] For each job, count the number of conflicting jobs c j. Schedule in ascending order of conflicts c j.

7
7 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. breaks earliest start time breaks shortest interval breaks fewest conflicts

8
8 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). n Remember job j* that was added last to A. n Job j is compatible with A if s j f j*. Sort jobs by finish times so that f 1 f 2... f n. A for j = 1 to n { if (job j compatible with A) A A {j} } return A jobs selected Interval Scheduling: Greedy Algorithm

9
9 Interval Scheduling: Analysis Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let i 1, i 2,... i k denote set of jobs selected by greedy. n Let j 1, j 2,... j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,..., i r = j r for the largest possible value of r. j1j1 j2j2 jrjr i1i1 i1i1 irir i r+1... Greedy: OPT: j r+1 why not replace job j r+1 with job i r+1 ? job i r+1 finishes before j r+1

10
10 j1j1 j2j2 jrjr i1i1 i1i1 irir i r+1 Interval Scheduling: Analysis Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let i 1, i 2,... i k denote set of jobs selected by greedy. n Let j 1, j 2,... j m denote set of jobs in the optimal solution with i 1 = j 1, i 2 = j 2,..., i r = j r for the largest possible value of r.... Greedy: OPT: solution still feasible and optimal, but contradicts maximality of r. i r+1 job i r+1 finishes before j r+1

11
11 Q1: Selecting Breakpoints Selecting breakpoints. n Road trip from Houston to Palo Alto along fixed route. n Refueling stations at certain points along the way. n Fuel capacity = C. n Goal: makes as few refueling stops as possible. Greedy algorithm. Go as far as you can before refueling. Houston Palo Alto 1 C C 2 C 3 C 4 C 5 C 6 C 7

12
12 Truck driver's algorithm. Question: Is this optimal? Implementation. O(n log n) n Use binary search to select each breakpoint p. Selecting Breakpoints: Greedy Algorithm Sort breakpoints so that: 0 = b 0 < b 1 < b 2 <... < b n = L S {0} x 0 while (x b n ) let p be largest integer such that b p x + C if (b p = x) return "no solution" x b p S S {p} return S breakpoints selected current location

13
13 Selecting Breakpoints: Correctness Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let 0 = g 0 < g 1 <... < g p = L denote set of breakpoints chosen by greedy. n Let 0 = f 0 < f 1 <... < f q = L denote set of breakpoints in an optimal solution with f 0 = g 0, f 1 = g 1,..., f r = g r for largest possible value of r. n Note: g r+1 > f r+1 by greedy choice of algorithm.... Greedy: OPT: g0g0 g1g1 g2g2 f0f0 f1f1 f2f2 fqfq grgr frfr why doesn't optimal solution drive a little further? g r+1 f r+1

14
14 Selecting Breakpoints: Correctness Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let 0 = g 0 < g 1 <... < g p = L denote set of breakpoints chosen by greedy. n Let 0 = f 0 < f 1 <... < f q = L denote set of breakpoints in an optimal solution with f 0 = g 0, f 1 = g 1,..., f r = g r for largest possible value of r. n Note: g r+1 > f r+1 by greedy choice of algorithm. another optimal solution has one more breakpoint in common contradiction... Greedy: OPT: g0g0 g1g1 g2g2 f0f0 f1f1 f2f2 fqfq grgr frfr g r+1

15
4.1 Interval Partitioning

16
16 Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses 4 classrooms to schedule 10 lectures. Time 99:301010:301111:301212:3011:3022:30 h c b a e d g f i j 33:3044:30

17
17 Interval Partitioning Interval partitioning. n Lecture j starts at s j and finishes at f j. n Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses only 3. Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b

18
18 Interval Partitioning: Lower Bound on Optimal Solution Def. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed depth. Ex: Depth of schedule below = 3 schedule below is optimal. Q. Does there always exist a schedule equal to depth of intervals? Time 99:301010:301111:301212:3011:3022:30 h c a e f g i j 33:3044:30 d b a, b, c all contain 9:30

19
19 Interval Partitioning: Greedy Algorithm Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Implementation. O(n log n). n For each classroom k, maintain the finish time of the last job added. n Keep the classrooms in a priority queue. Sort intervals by starting time so that s 1 s 2... s n. 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

20
20 Interval Partitioning: Greedy Analysis Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Pf. n Let d = number of classrooms that the greedy algorithm allocates. n Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. n Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s j. n Thus, we have d lectures overlapping at time s j +. n Key observation all schedules use d classrooms.

21
Q2: A variation on Interval Scheduling You have a processor that can operate 24-7. People submit requests to run daily jobs on the processor. Each such job comes with a start time and an end time; if the job is accepted it must run continuously for the period between the start and end times, EVERY DAY. (Note that some jobs can start before midnight and end after midnight.) Given a list of n such jobs, your goal is to accept as many jobs as possible (regardless of length), subject to the constraint that the processor can run at most one job at any given point of time. Give an algorithm to do this. For example, here you have four jobs (6pm, 6am), (9pm, 4am), (3am, 2pm), (1pm, 7pm). The optimal solution is to pick the second and fourth jobs. 21

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google