csep 521 applied algorithms richard anderson winter 2013 lecture 2
TRANSCRIPT
![Page 1: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/1.jpg)
CSEP 521Applied Algorithms
Richard Anderson
Winter 2013
Lecture 2
![Page 2: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/2.jpg)
Announcements
• Reading– Chapter 2.1, 2.2– Chapter 3 – Chapter 4
• Homework Guidelines– Prove that your algorithm works
• A proof is a “convincing argument”
– Give the run time for you algorithm• Justify that the algorithm satisfies the runtime bound
– You may lose points for style
![Page 3: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/3.jpg)
Announcements
• Monday, January 21 is a holiday– No class
• Makeup lecture, Thursday, January 17, 5:00 pm – 6:30 pm– UW and Microsoft– View off line if you cannot attend
• Homework 2 is due January 21– Electronic turn in only
![Page 4: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/4.jpg)
What does it mean for an algorithm to be efficient?
![Page 5: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/5.jpg)
Definitions of efficiency
• Fast in practice
• Qualitatively better worst case performance than a brute force algorithm
![Page 6: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/6.jpg)
Polynomial time efficiency
• An algorithm is efficient if it has a polynomial run time
• Run time as a function of problem size– Run time: count number of instructions
executed on an underlying model of computation
– T(n): maximum run time for all problems of size at most n
![Page 7: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/7.jpg)
Polynomial Time
• Algorithms with polynomial run time have the property that increasing the problem size by a constant factor increases the run time by at most a constant factor (depending on the algorithm)
![Page 8: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/8.jpg)
Why Polynomial Time?
• Generally, polynomial time seems to capture the algorithms which are efficient in practice
• The class of polynomial time algorithms has many good, mathematical properties
![Page 9: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/9.jpg)
Polynomial vs. Exponential Complexity
• Suppose you have an algorithm which takes n! steps on a problem of size n
• If the algorithm takes one second for a problem of size 10, estimate the run time for the following problems sizes:
12 14 16 18 20
![Page 10: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/10.jpg)
Ignoring constant factors
• Express run time as O(f(n))• Emphasize algorithms with slower growth
rates• Fundamental idea in the study of
algorithms• Basis of Tarjan/Hopcroft Turing Award
![Page 11: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/11.jpg)
Why ignore constant factors?
• Constant factors are arbitrary– Depend on the implementation– Depend on the details of the model
• Determining the constant factors is tedious and provides little insight
![Page 12: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/12.jpg)
Why emphasize growth rates?
• The algorithm with the lower growth rate will be faster for all but a finite number of cases
• Performance is most important for larger problem size
• As memory prices continue to fall, bigger problem sizes become feasible
• Improving growth rate often requires new techniques
![Page 13: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/13.jpg)
Formalizing growth rates
• T(n) is O(f(n)) [T : Z+ R+]– If n is sufficiently large, T(n) is bounded by a
constant multiple of f(n)– Exist c, n0, such that for n > n0, T(n) < c f(n)
• T(n) is O(f(n)) will be written as: T(n) = O(f(n))– Be careful with this notation
![Page 14: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/14.jpg)
Prove 3n2 + 5n + 20 is O(n2)
T(n) is O(f(n)) if there exist c, n0, such that for n > n0, T(n) < c f(n)
Let c =
Let n0 =
![Page 15: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/15.jpg)
Order the following functions in increasing order by their growth ratea) n log4nb) 2n2 + 10nc) 2n/100
d) 1000n + log8 ne) n100
f) 3n
g) 1000 log10nh) n1/2
![Page 16: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/16.jpg)
Lower bounds
• T(n) is W(f(n))– T(n) is at least a constant multiple of f(n)– There exists an n0, and e > 0 such that
T(n) > ef(n) for all n > n0
• Warning: definitions of W vary
• T(n) is Q(f(n)) if T(n) is O(f(n)) and T(n) is W(f(n))
![Page 17: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/17.jpg)
Useful Theorems
• If lim (f(n) / g(n)) = c for c > 0 then f(n) = Q(g(n))
• If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) is O(h(n))
• If f(n) is O(h(n)) and g(n) is O(h(n)) then f(n) + g(n) is O(h(n))
![Page 18: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/18.jpg)
Ordering growth rates
• For b > 1 and x > 0– logbn is O(nx)
• For r > 1 and d > 0– nd is O(rn)
![Page 19: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/19.jpg)
Stable Matching
![Page 20: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/20.jpg)
Reported Results
Student n M / n W / n M / n * W / n
Stanislav 10,000 9.96 1020 10159
Andy 4,096 8.77 472 4139
Boris 5,000 10.06 499 5020
Huy 10,000 10.68 969 10349
Hans 10,000 9.59 1046 10031
Vijayanand 1,000 8.60 114 980
Robert 20,000 12.40 1698 21055
Zain 2,825 8.61 331 2850
Uzair 8,192 9.10 883 8035
Anand 10,000 9.58 1045 10011
![Page 21: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/21.jpg)
Why is M/n ~ log n?
![Page 22: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/22.jpg)
Why is W/n ~ n / log n?
![Page 23: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/23.jpg)
Graph Theory
![Page 24: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/24.jpg)
Graph Theory
• G = (V, E)– V – vertices– E – edges
• Undirected graphs– Edges sets of two vertices {u, v}
• Directed graphs– Edges ordered pairs (u, v)
• Many other flavors– Edge / vertices weights– Parallel edges– Self loops
![Page 25: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/25.jpg)
Definitions
• Path: v1, v2, …, vk, with (vi, vi+1) in E– Simple Path– Cycle– Simple Cycle
• Distance• Connectivity
– Undirected– Directed (strong connectivity)
• Trees– Rooted– Unrooted
![Page 26: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/26.jpg)
Graph search
• Find a path from s to t
S = {s}
While there exists (u, v) in E with u in S and v not in S
Pred[v] = u
Add v to S
if (v = t) then path found
![Page 27: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/27.jpg)
Breadth first search
• Explore vertices in layers– s in layer 1– Neighbors of s in layer 2– Neighbors of layer 2 in layer 3 . . .
s
![Page 28: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/28.jpg)
Key observation
• All edges go between vertices on the same layer or adjacent layers
2
8
3
7654
1
![Page 29: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/29.jpg)
Bipartite Graphs
• A graph V is bipartite if V can be partitioned into V1, V2 such that all edges go between V1 and V2
• A graph is bipartite if it can be two colored
![Page 30: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/30.jpg)
Can this graph be two colored?
![Page 31: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/31.jpg)
Algorithm
• Run BFS• Color odd layers red, even layers blue• If no edges between the same layer, the
graph is bipartite• If edge between two vertices of the same
layer, then there is an odd cycle, and the graph is not bipartite
![Page 32: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/32.jpg)
Theorem: A graph is bipartite if and only if it has no odd cycles
![Page 33: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/33.jpg)
Lemma 1
• If a graph contains an odd cycle, it is not bipartite
![Page 34: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/34.jpg)
Lemma 2
• If a BFS tree has an intra-level edge, then the graph has an odd length cycle
Intra-level edge: both end points are in the same level
![Page 35: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/35.jpg)
Lemma 3
• If a graph has no odd length cycles, then it is bipartite
![Page 36: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/36.jpg)
Connected Components
• Undirected Graphs
![Page 37: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/37.jpg)
Computing Connected Components in O(n+m) time
• A search algorithm from a vertex v can find all vertices in v’s component
• While there is an unvisited vertex v, search from v to find a new component
![Page 38: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/38.jpg)
Directed Graphs
• A Strongly Connected Component is a subset of the vertices with paths between every pair of vertices.
![Page 39: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/39.jpg)
Identify the Strongly Connected Components
![Page 40: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/40.jpg)
Strongly connected components can be found in O(n+m) time
• But it’s tricky!• Simpler problem: given a vertex v, compute the
vertices in v’s scc in O(n+m) time
![Page 41: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/41.jpg)
Topological Sort
• Given a set of tasks with precedence constraints, find a linear order of the tasks
142 143
321
341
370 378
326
322 401
421
431
![Page 42: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/42.jpg)
Find a topological order for the following graph
E
F
D
A
C
BK
JG
HI
L
![Page 43: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/43.jpg)
If a graph has a cycle, there is no topological sort
• Consider the first vertex on the cycle in the topological sort
• It must have an incoming edge B
A
D
E
F
C
![Page 44: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/44.jpg)
Lemma: If a graph is acyclic, it has a vertex with in degree 0
• Proof: – Pick a vertex v1, if it has in-degree 0 then
done– If not, let (v2, v1) be an edge, if v2 has in-
degree 0 then done– If not, let (v3, v2) be an edge . . .
– If this process continues for more than n steps, we have a repeated vertex, so we have a cycle
![Page 45: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/45.jpg)
Topological Sort Algorithm
While there exists a vertex v with in-degree 0
Output vertex v
Delete the vertex v and all out going edges
E
F
D
A
C
BK
JG
HI
L
![Page 46: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/46.jpg)
Details for O(n+m) implementation
• Maintain a list of vertices of in-degree 0• Each vertex keeps track of its in-degree• Update in-degrees and list when edges
are removed• m edge removals at O(1) cost each
![Page 47: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/47.jpg)
Random Graph models
![Page 48: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/48.jpg)
Question:What is the cycle structure as N gets large?How many cycles?What is the cycle length?
Random out degree one graph
![Page 49: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/49.jpg)
Greedy Algorithms
![Page 50: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/50.jpg)
Greedy Algorithms
• Solve problems with the simplest possible algorithm
• The hard part: showing that something simple actually works
• Pseudo-definition– An algorithm is Greedy if it builds its solution
by adding elements one at a time using a simple rule
![Page 51: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/51.jpg)
Scheduling Theory
• Tasks– Processing requirements, release times,
deadlines• Processors• Precedence constraints• Objective function
– Jobs scheduled, lateness, total execution time
![Page 52: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/52.jpg)
• Tasks occur at fixed times• Single processor• Maximize number of tasks completed
• Tasks {1, 2, . . . N}• Start and finish times, s(i), f(i)
Interval Scheduling
![Page 53: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/53.jpg)
What is the largest solution?
![Page 54: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/54.jpg)
Greedy Algorithm for Scheduling
Let T be the set of tasks, construct a set of independent tasks I, A is the rule determining the greedy algorithm
I = { }
While (T is not empty)
Select a task t from T by a rule A
Add t to I
Remove t and all tasks incompatible with t from T
![Page 55: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/55.jpg)
Simulate the greedy algorithm for each of these heuristics
Schedule earliest starting task
Schedule shortest available task
Schedule task with fewest conflicting tasks
![Page 56: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/56.jpg)
Greedy solution based on earliest finishing time
Example 1
Example 2
Example 3
![Page 57: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/57.jpg)
Theorem: Earliest Finish Algorithm is Optimal
• Key idea: Earliest Finish Algorithm stays ahead
• Let A = {i1, . . ., ik} be the set of tasks found by EFA in increasing order of finish times
• Let B = {j1, . . ., jm} be the set of tasks found by a different algorithm in increasing order of finish times
• Show that for r<= min(k, m), f(ir) <= f(jr)
![Page 58: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/58.jpg)
Stay ahead lemma
• A always stays ahead of B, f(ir) <= f(jr)
• Induction argument– f(i1) <= f(j1)
– If f(ir-1) <= f(jr-1) then f(ir) <= f(jr)
![Page 59: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/59.jpg)
Completing the proof
• Let A = {i1, . . ., ik} be the set of tasks found by EFA in increasing order of finish times
• Let O = {j1, . . ., jm} be the set of tasks found by an optimal algorithm in increasing order of finish times
• If k < m, then the Earliest Finish Algorithm stopped before it ran out of tasks
![Page 60: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/60.jpg)
Scheduling all intervals
• Minimize number of processors to schedule all intervals
![Page 61: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/61.jpg)
How many processors are needed for this example?
![Page 62: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/62.jpg)
Prove that you cannot schedule this set of intervals with two processors
![Page 63: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/63.jpg)
Depth: maximum number of intervals active
![Page 64: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/64.jpg)
Algorithm
• Sort by start times• Suppose maximum depth is d, create d
slots• Schedule items in increasing order, assign
each item to an open slot
• Correctness proof: When we reach an item, we always have an open slot
![Page 65: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/65.jpg)
Scheduling tasks
• Each task has a length ti and a deadline di
• All tasks are available at the start• One task may be worked on at a time• All tasks must be completed
• Goal minimize maximum lateness– Lateness = fi – di if fi >= di
![Page 66: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/66.jpg)
Example
2
3
2
4
DeadlineTime
2 3
23
Lateness 1
Lateness 3
![Page 67: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/67.jpg)
Determine the minimum lateness
2
3
4
5
6
4
5
12
DeadlineTime
![Page 68: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/68.jpg)
Greedy Algorithm
• Earliest deadline first• Order jobs by deadline
• This algorithm is optimal
![Page 69: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/69.jpg)
Analysis
• Suppose the jobs are ordered by deadlines, d1 <= d2 <= . . . <= dn
• A schedule has an inversion if job j is scheduled before i where j > i
• The schedule A computed by the greedy algorithm has no inversions.
• Let O be the optimal schedule, we want to show that A has the same maximum lateness as O
![Page 70: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/70.jpg)
List the inversions
2
3
4
5
4
5
6
12
DeadlineTime
a1
a2
a3
a4
a4 a2 a3a1
![Page 71: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/71.jpg)
Lemma: There is an optimal schedule with no idle time
• It doesn’t hurt to start your homework early!
• Note on proof techniques– This type of can be important for keeping proofs clean– It allows us to make a simplifying assumption for the
remainder of the proof
a4 a2 a3 a1
![Page 72: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/72.jpg)
Lemma
• If there is an inversion i, j, there is a pair of adjacent jobs i’, j’ which form an inversion
![Page 73: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/73.jpg)
Interchange argument
• Suppose there is a pair of jobs i and j, with di <= dj, and j scheduled immediately before i. Interchanging i and j does not increase the maximum lateness.
di djdi dj
j i ji
![Page 74: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/74.jpg)
Proof by Bubble Sort
a4a2 a3 a1
a4a2 a3
a4a2 a3a1
a4a2 a3a1
a1
a4a2 a3a1
Determine maximum lateness
d1 d2 d3 d4
![Page 75: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/75.jpg)
Real Proof
• There is an optimal schedule with no inversions and no idle time.
• Let O be an optimal schedule k inversions, we construct a new optimal schedule with k-1 inversions
• Repeat until we have an optimal schedule with 0 inversions
• This is the solution found by the earliest deadline first algorithm
![Page 76: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/76.jpg)
Result
• Earliest Deadline First algorithm constructs a schedule that minimizes the maximum lateness
![Page 77: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/77.jpg)
Homework Scheduling
• How is the model unrealistic?
![Page 78: CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 2](https://reader034.vdocument.in/reader034/viewer/2022051517/5697bfba1a28abf838ca089c/html5/thumbnails/78.jpg)
Extensions
• What if the objective is to minimize the sum of the lateness?– EDF does not seem to work
• If the tasks have release times and deadlines, and are non-preemptable, the problem is NP-complete
• What about the case with release times and deadlines where tasks are preemptable?