Algorithms Lecture 6 Interval partitioning Interval partitioning problem
Algorithms Lecture 6
Interval partitioning
Interval partitioning problem • Previously we had one resource and wanted to schedule as many requests as possible • Now we want to satisfy all requests using the fewest possible resources
Interval partitioning problem • Depth = maximum number of requests at any instant of time • Number of resources needed is clearly at least equal to the depth
Greedy algorithm for the problem • Sort requests by their start times • For i=1, … – Assign ri the lowest available resource • Clearly no two requests are assigned to the same resource at the same time • Claim: number of resources used ≤ depth – Let d be highest resource assigned, say to request r – At the start time for r, there are d-1 other requests using resources depth ≥ d
Job scheduling
Minimizing lateness • Assume we have only one resource, and want to satisfy all requests (“jobs”) • Instead of fixed start/end times, each job now has a deadline di and a duration duri • Want to assign starting time si ≥ 0 to each job; its finishing time is fi = si + duri – Define the lateness Li of job i by Li = fi – di • Goal: minimize maximum lateness maxi {Li}
Greedy algorithm • Order jobs by increasing deadlines – “Earliest deadline first” • Is this optimal?
Proof of correctness • Without loss of generality, we can consider schedules with no “idle time” – Intuitively true – Can only decrease the maximum lateness by getting rid of any idle time in a schedule • From now on, assume all schedules we consider have no idle time
Proof of correctness • In a schedule r 1, r 2, …, say an inversion at (i, j) occurs if i < j but dj < di – The schedule returned by the greedy algorithm has no inversions • Claim: all schedules with no inversions have the same maximum lateness – In schedules with no inversions all jobs with the same deadline are scheduled consecutively • Among all jobs with deadline d, the one scheduled last has the maximum lateness
Proof of correctness • Claim: There is an optimal schedule with no inversions – Let O be an optimal schedule – If O has an inversion, there is an inversion at some consecutive positions (i, i+1) • Must be the case that Li+1 > Li – Swapping the jobs at those positions to get schedule O’ decreases the number of inversions, and cannot increase the maximum lateness • Lateness of ri+1 decreases in O’ • Lateness of ri in O’ is less than lateness of ri+1 in O • Lateness of other jobs stays the same
Proof of correctness • Claim: all schedules with no inversions have the same maximum lateness • Claim: There is an optimal schedule with no inversions • The greedy algorithm produces a schedule with no inversions the greedy algorithm produces an optimal schedule
Shortest paths
Shortest paths • So far, we have been considering shortest paths in unweighted (and undirected) graphs • Now we consider weighted (directed) graphs – Each (directed) edge e has an associated non-negative weight (length) L(e) • Negative weights are more difficult to handle • Say L(u, v) = if there is no such edge – The length of a path is the sum of the lengths of the edges in that path • Want to find the shortest paths from a given source s to (all) other nodes
Dijkstra’s algorithm • Idea: maintain a set R of nodes for which we know the lengths of the shortest paths – Grow R using a greedy approach • Initially, R = {s} and d(s) = 0 • While u R, v R with edge (u, v): – Find such an edge that minimizes d(u) + L(u, v) – Add v to R and set d(v) = d(u) + L(u, v)
Dijkstra’s algorithm • As described, the algorithm computes the lengths of the shortest paths to all nodes – Easy to adapt the algorithm to also keep track of the shortest paths to all nodes
Proof of correctness • Claim: at any point, for all v R, the value d(v) is the length of the shortest path to v • Proof by induction – Clearly true at the beginning (when R = {s}) – Assume true at the beginning of an iteration – If v is added, there is a path of length d(v) – Any other path to v must take an edge that exits R, and that path must have length at least d(v) (since v was the vertex added in this iteration)
Running time? • At most |V| iterations • What is the cost of an iteration? – Naïve: check O(|E|) edges exiting R – Can do better using a priority queue • A priority queue maintains a set of (item, key) pairs while supporting (in O(log n) time): – Insertion/deletion – Updating the key of an item – Extracting the item with the minimum key
Running time • Idea: each vertex v R has the “key” d’(v) = minu R{d(u) + L(u, v)} – Store the vertices in a priority queue • In an iteration: 1. Put v with minimum d’(v) in R and set d(v) = d’(v) 2. For each edge (v, w) with w R, update d’(w) • • Step 1 is run O(|V|) times (once per iteration) Step 2 is run O(|E|) times (at most once/edge) Each can be implemented in O(log |V|) time Overall running time O(|E| log |V|)
- Slides: 19