1 cs lunchblerner/cs312/slides/slides08.pdf · new topic! 4-1 new topic! greedy algorithms 4-2 new...
TRANSCRIPT
CS Lunch
Learn about the CS Department and CS major
Wednesday, 12:15 to 1:00
1
Midterm
No homework this week
Midterm on Feb. 25
Covers chapters 1-3
Will post some sample questions
2
3
Slides08 - IntervalScheduling.key - February 18, 2019
New topic!4-1
New topic!
Greedy Algorithms
4-2
New topic!
Greedy AlgorithmsGeneral approach:
1. Sort the data2. Consider each data item in order
and make a local decision.
4-3
Slides08 - IntervalScheduling.key - February 18, 2019
Interval SchedulingJob 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
5
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 1: Earliest start time. Consider jobs in ascending order of start time sj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
6-1
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 1: Earliest start time. Consider jobs in ascending order of start time sj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
a, g
6-2
Slides08 - IntervalScheduling.key - February 18, 2019
Earliest Start Time: Bad Example
Earliest start time picks single yellow jobBetter solution is the 4 short blue jobsOther ideas?
7
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 2: Shortest interval. Consider jobs in ascending order of interval length fj - sj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
8-1
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 2: Shortest interval. Consider jobs in ascending order of interval length fj - sj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
b, c, h
8-2
Slides08 - IntervalScheduling.key - February 18, 2019
Shortest Interval: Bad Example
Shortest interval picks single yellow jobBetter solution is the 2 blue jobsOther ideas?
9
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 3: Fewest conflicts. For each job, count the number of conflicting jobs cj. Schedule in ascending order of conflicts cj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
10
Fewest Conflicts: Bad Example
Fewest conflicts picks the 3 yellow jobsBetter solution is the 4 jobs on the top rowOther ideas?
11
Slides08 - IntervalScheduling.key - February 18, 2019
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 4: Earliest finish time. Consider jobs in ascending order of finish time fj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
12-1
Interval Scheduling: Greedy SolutionGreedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken.
Idea 4: Earliest finish time. Consider jobs in ascending order of finish time fj.
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
b, e, h
12-2
Earliest Finish Time - Optimal Solution
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
13
Slides08 - IntervalScheduling.key - February 18, 2019
Hallmark of a Greedy Algorithm
Sort data according to some criteria
Consider each piece of data in sorted order and make a local decision
Result is globally optimal (if this problem is amenable to a greedy solution!)
Complexity is generally no better than O(n log n) due to the sort
Important to prove that the solution is optimal
14
Interval PartitioningLecture 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.
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
15
Interval Partitioning Lower Bound
The depth of a set of intervals is the maximum number that contain any given time.Key observation. Number of classrooms needed ≥ depth.
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
16
Slides08 - IntervalScheduling.key - February 18, 2019
Interval Partitioning Lower BoundExample: Depth of schedule below = 3Question: 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
b
a
e
d g
f i
j
3 3:30 4 4:30
Time9 9:30 10 10:30 11 11:30 1212:30 1 1:30 2 2:30
h
c
a e
f
g i
j
3 3:30 4 4:30
d
b
17
Interval Partitioning: Greedy Solution
Sort intervals by starting time so that s1 ≤ s2 ≤ ... ≤ sn.d ← 0 // Number of classrooms
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 }
Complexity?
18
Slides08 - IntervalScheduling.key - February 18, 2019