Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 6.
-
Upload
karla-delane -
Category
Documents
-
view
226 -
download
0
Transcript of Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 6.
Prof. Swarat Chaudhuri
COMP 482: Design and Analysis of Algorithms
Spring 2013
Lecture 6
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
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 := Xwhile (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
Greedy algorithms
4.1 Interval Scheduling
5
Interval Scheduling
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.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
6
Interval Scheduling: Greedy Algorithms
Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
[Earliest start time] Consider jobs in ascending order of start time sj.
[Earliest finish time] Consider jobs in ascending order of finish time fj.
[Shortest interval] Consider jobs in ascending order of interval length fj - sj.
[Fewest conflicts] For each job, count the number of conflicting jobs cj. Schedule in ascending order of conflicts cj.
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
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 f1 f2 ... fn.
A for j = 1 to n { if (job j compatible with A) A A {j}}return A
jobs selected
Interval Scheduling: Greedy Algorithm
9
Interval Scheduling: Analysis
Theorem. Greedy algorithm is optimal.
Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let i1, i2, ... ik denote set of jobs selected by greedy. Let j1, j2, ... jm denote set of jobs in the optimal solution with
i1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r.
j1 j2 jr
i1 i1 ir ir+1
. . .
Greedy:
OPT: jr+1
why not replace job jr+1
with job ir+1?
job ir+1 finishes before jr+1
10
j1 j2 jr
i1 i1 ir ir+1
Interval Scheduling: Analysis
Theorem. Greedy algorithm is optimal.
Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let i1, i2, ... ik denote set of jobs selected by greedy. Let j1, j2, ... jm denote set of jobs in the optimal solution with
i1 = j1, i2 = j2, ..., ir = jr for the largest possible value of r.
. . .
Greedy:
OPT:
solution still feasible and optimal, but contradicts maximality of r.
ir+1
job ir+1 finishes before jr+1
11
Q1: Selecting Breakpoints
Selecting breakpoints. Road trip from Houston 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.
Houston Palo Alto
1
C
C
2
C
3
C
4
C
5
C
6
C
7
12
Truck driver's algorithm. Question: Is this optimal?
Implementation. O(n log n) Use binary search to select each breakpoint p.
Selecting Breakpoints: Greedy Algorithm
Sort breakpoints so that: 0 = b0 < b1 < b2 < ... < 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 selectedcurrent location
13
Selecting Breakpoints: Correctness
Theorem. Greedy algorithm is optimal.
Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by
greedy. Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in an optimal
solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of
r. Note: gr+1 > fr+1 by greedy choice of algorithm.
. . .
Greedy:
OPT:
g0 g1 g2
f0 f1 f2 fq
gr
fr
why doesn't optimal solution drive a little further?
gr+1
fr+1
14
Selecting Breakpoints: Correctness
Theorem. Greedy algorithm is optimal.
Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by
greedy. Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in an optimal
solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of
r. Note: gr+1 > fr+1 by greedy choice of algorithm.
another optimal solution hasone more breakpoint in common contradiction
. . .
Greedy:
OPT:
g0 g1 g2
f0 f1 f2 fq
gr
fr
gr+1
4.1 Interval Partitioning
16
Interval Partitioning
Interval partitioning. Lecture j starts at sj and finishes at fj. 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.
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
b
a
e
d g
f i
j
3 3:30 4 4:30
17
Interval Partitioning
Interval partitioning. Lecture j starts at sj and finishes at fj. 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.
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
a e
f
g i
j
3 3:30 4 4:30
d
b
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?
Time9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30
h
c
a e
f
g i
j
3 3:30 4 4:30
d
b
a, b, c all contain 9:30
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). 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 s1 s2 ... 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
20
Interval Partitioning: Greedy Analysis
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.
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. ▪
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