![Page 1: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/1.jpg)
CS 401
Greedy Algorithms / Interval Partition / Minimizing Lateness
Xiaorui Sun
1
![Page 2: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/2.jpg)
2
Homework 1
If the solution involves reducing the problem into a question we solved in class, then the solution should look like:
Reduction Outline (how to use algo learned in class)!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
Proof of Runtime/Termination!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
Proof of Correctness!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
![Page 3: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/3.jpg)
3
Homework 1
If the solution involves a new algorithm (usually modified from an algo in class), then the solution should look like:
Algorithm Outline!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
Proof of Runtime/Termination!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
Proof of Correctness!"#$%&'()*+,-./0☺23456789:;<=>?@ABCDEF☹HIJKLMNOPQRST
![Page 4: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/4.jpg)
44
Problem 2
!" = $ !% , Ω !% or Θ(!%)?!" = $ !+ , Ω !+ or Θ(!+)?!" = $ !, , Ω !, or Θ(!,)?
Answer the following 10 questions:
!" = $ !- , Ω !- or Θ(!-)?!% = $ !+ , Ω !+ or Θ(!+)?
!% = $ !, , Ω !, or Θ(!,)?!% = $ !- , Ω !- or Θ(!-)?!+ = $ !, , Ω !, or Θ(!,)?!+ = $ !- , Ω !- or Θ(!-)?!, = $ !- , Ω !- or Θ(!-)?
![Page 5: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/5.jpg)
5
Greedy Algorithms
• Hard to define exactly but can give general properties• Solution is built in small steps• Decisions on how to build the solution are made to
maximize some criterion without looking to the future• Want the ‘best’ current partial solution as if the
current step were the last step
![Page 6: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/6.jpg)
Interval Scheduling: Earliest Finish Time
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 !∗ that was added last to A.• Job ! is compatible with A if # ! ≥ %(!∗).
Correctness proof: greedy stays ahead6
Sort jobs by finish times so that f(1) £ f(2) £ ... £ f(n).( ← ∅for j = 1 to n {
if (job j compatible with ()( ← ( ∪ {-}
}return (
![Page 7: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/7.jpg)
Interval PartitioningTechnique: Structural
![Page 8: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/8.jpg)
Interval Partitioning
Lecture ! starts at "(!) and finishes at %(!).Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room.
8Time9 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
Room 1
Room 2
Room 3
Room 4
![Page 9: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/9.jpg)
Interval Partitioning
9Time9 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
Room 1
Room 2
Room 3
Room 4
C
BA
ED G
F
J
H
I
Note: graph coloring is very hard in general, but graphs corresponding to
interval intersections are simpler.
![Page 10: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/10.jpg)
A Better Schedule
This one uses only 3 classrooms
10
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
![Page 11: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/11.jpg)
A Greedy Algorithm
Greedy algorithm: Consider lectures in increasing order of finish time: assign lecture to any compatible classroom.
Correctness: This is wrong!11
Sort intervals by finish time so that f1 £ f2 £ ... £ fn.d ¬ 0
for j = 1 to n {if (let j is compatible with some classroom k, ! ≤ # ≤ $)
schedule lecture j in classroom kelse
allocate a new classroom d + 1schedule lecture j in classroom d + 1d ¬ d + 1
}
![Page 12: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/12.jpg)
Example
12
Time0 1 2 3 4 5 6
d
a
b
c
Time0 1 2 3 4 5 6
d
a
b
c
Greedy by finish time gives: OPT:
Time0 1 2 3 4 5 6
da
b c
![Page 13: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/13.jpg)
A Greedy Algorithm
Greedy algorithm: Consider lectures in increasing order of start time: assign lecture to any compatible classroom.
Implementation: Exercise!13
Sort intervals by starting time so that s1 £ s2 £ ... £ sn.d ¬ 0
for j = 1 to n {if (lect j is compatible with some classroom k, ! ≤ # ≤ $)
schedule lecture j in classroom kelse
allocate a new classroom d + 1schedule lecture j in classroom d + 1d ¬ d + 1
}
![Page 14: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/14.jpg)
A Structural Lower-Bound on OPT
Def. The depth of a set of 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?
14Time9 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
![Page 15: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/15.jpg)
Correctness
Observation: Greedy algorithm never schedules two incompatible lectures in the same classroom.
Theorem: Greedy algorithm is optimal.Proof (exploit structural property). Let ! = number of classrooms that the greedy algorithm allocates.Classroom ! is opened because we needed to schedule a job, say #, that is incompatible with all ! − 1 previously used classrooms.Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than &(#).Thus, we have ! lectures overlapping at time & # + *, i.e. depth ≥ !�OPT Observation� Þ all schedules use ³ depth classrooms, so ! = depth and greedy is optimal
15
![Page 16: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/16.jpg)
Minimizing Lateness
![Page 17: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/17.jpg)
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 time intervals [%!, '!] s.t. "! = '! − %!.
• Lateness for request ! is• If #! < '! then request ! is late by ,! = '! − #- otherwise its
lateness ,! = .• Goal: Find a schedule that minimize the
Maximum lateness , = /01! ,!
23 6
43 3
1
8
2
2
9
1
3
9
4
4
14
3
5
15
2
6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d5 = 14d2 = 8 d6 = 15 d1 = 6 d4 = 9d3 = 9
lateness = 0lateness = 2 max lateness = 6lateness = 0lateness = 0lateness = 0
![Page 18: lecture-11 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-11.pdf · Lecture !starts at "(!)and finishes at %(!). Goal: find minimum number of classrooms](https://reader033.vdocuments.us/reader033/viewer/2022042400/5f0f4d097e708231d4437b2e/html5/thumbnails/18.jpg)
18
Minimizing Lateness: Greedy Algorithms
Greedy template. Consider jobs in some order. • [Shortest processing time first]
Consider jobs in ascending order of processing time !".
• [Smallest slack]Consider jobs in ascending order of slack #" − !".
• [Earliest deadline first]Consider jobs in ascending order of deadline #".
counterexampledj
tj
100
1
1
10
10
2
counterexampledj
tj2
1
1
10
10
2