ALGORITHMS AND DATA STRUCTURES II
Ivana Černá Jaro 2020
Fakulta informatiky MU
George Pólya: How to Solve It?
What is the difference between method and device? A method is a device which you used twice.
Andrew Hamilton, rektor Oxford University, Respekt 7/2015
Je nutné mít na paměti, že my studenty nepřipravujeme na konkrétní povolání, ale trénujeme jejich mysl, aby byli co nejlépe připraveni na změny, jež nás nevyhnutelně čekají, a které budou nejspíš dosti
dramatické.
i
CONTENTS
1. Computational Complexity
1.1 Complexity of Problems and Algorithms
1.2 Amortized Complexity
2. Data Stuctures
2.1 Heaps
2.2 Disjoint-sets Data structures
3. Algorithm Design Techniques
3.1 Divide and Conquer
3.2 Dynamic Programming
3.3 Greedy Algorithms
4. Network Flow
5. String Matching
2
ADMINISTRATIVE STUFF
Interactive syllabi
https://is.muni.cz/auth/el/1433/jaro2020/IV003/index.qwarp
follow discussion groups in IS
3
LITERATURE
recommended textbooks
• J. Kleinberg, E. Tardos: Algorithm Design. Addison-Wesley, 2006.
• T. Cormen, Ch. Leiserson, R. Rivest, C. Stein: Introduction to Algorithms. 3rd Edition. MIT Press, 2009.
• S. Dasgupta, Ch. Papadimitriou, U. \/az\ran\:Algorithms. McGraw Hill, 2007.
slides and demo
http://www.es.princeton.edu/~wayne/kleinberg-tardos/
4
Part I
Algorithmic Complexity
5
OVERVIEW
Complexity of Problems and Algorithms
Algorithm Complexity Analysis Recursive algorithms Iterative algorithms
Amortized Complexity Amortized analysis
6
Complexity of Problems and Algorithms
A strikingly modern thought
" As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise—By what course of calculation can these results be arrived at by the machine in the shortest time? " — Charles Babbage (1864)
Models of computation: Turing machines
Deterministic Turing machine. Simple and idealistic model
NOI
^R>#:#-^lT ~^Jh) halt
0:1---^
YES
# # # 1110 1 1 0 0 1 1 1 # # #
Running time. Number of steps. Memory. Number of tape cells utilized.
Caveat. No random access of memory.
• Single-tape TM requires > n2 steps to detect ^z-bit palindromes.
• Easy to detect palindromes in < cn steps on a real computer.
4
Models of computation: word RAM
Word RAM. ^ assume w^log2,
• Each memory location and input/output cell stores a w-bit integer.
• Primitive operations: arithmetic/logic operations, read/write memory, array indexing, following a pointer, conditional branch, ...
constant-time C-style operations
(w = 64)
input
program
output
a[i]
memory
Running time. Number of primitive operations. Memory. Number of memory cells utilized.
Caveat. At times, need more refined model (e.g., multiplying ^z-bit integers).
Brute force
Brute force. For many nontrivial problems, there is a natural brute-force search algorithm that checks every possible solution.
• Typically takes 2n steps (or worse) for inputs of size n.
• Unacceptable in practice.
Ex. Stable matching problem: test all n\ perfect matchings for stability.
Polynomial running time
Desirable scaling property. When the input size doubles, the algorithm should slow down by at most some constant factor C.
Def. An algorithm is poly-time if the above scaling property holds.
There exist constants c > 0 and d > 0 such that, for every input of size n, the running time of the algorithm is bounded above by c nd primitive computational steps. <- choose c = 2d
Polynomial running time
We say that an algorithm is efficient if it has a polynomial running time.
Theory. Definition is (relatively) insensitive to model of computation.
Practice. It really works!
• The poly-time algorithms that people develop have both small constants and small exponents.
• Breaking through the exponential barrier of brute force typically exposes some crucial structure of the problem.
n
120
Exceptions. Some poly-time algorithms in the wild have galactic constants and/or huge exponents.
Q. Which would you prefer: 20^120 or nl + QmXnn ?
Map graphs in polynomial time
Mikkel Thorup* Department of Computer Science, University of Copenhagen Universitetsparken 1, DK-2100 Copenhagen East, Denmark mthorup@diku.dk
Abstract
Chen,Grigni,andPapadimitriou(WADS'97andSTOC98) have introduced a modified notion of planarity, where two faces are considered adjacent if they share at least one point. The corresponding abstract graphs are catted map graphs. Chen et.al. raised the question of whether map graphs can be recognized in polynomial time. They showed that the decision problem is in NP and presented a polynomial time algorithm for the special case where we allow at most 4 faces to intersect in any point — if only 3 are allowed to intersect in a point, we get the usual planar graphs.
Chen et.al. conjectured that map graphs can be recognized in polynomial time, and in this paper, their conjecture is settled affirmatively.
Worst-case analysis
Worst case. Running time guarantee for any input of size n.
• Generally captures efficiency in practice.
• Draconian view, but hard to find effective alternative.
Exceptions. Some exponential-time algorithms are used widely in practice because the worst-case instances don't arise.
Other types of analyses
Probabilistic. Expected running time of a randomized algorithm. Ex. The expected number of compares to quicksort n elements is
2n In n.
Amortized. Worst-case running time for any sequence of n operations. Ex. Starting from an empty stack, any sequence of n push and pop operations takes 0{n) primitive computational steps using a resizing array.
Also. Average-case analysis, smoothed analysis, competitive analysis, ...
10
PROBLEM COMPLEXITY
algorithm complexity versus problem complexity
• lower bound of the problem complexity
• proof techniques
• upper bound of the problem complexity
• complexity of the particular algorithm solving the problem
• problem complexity
• given by a lower and upper bound
• tight bounds
7
LOWER BOUND PROOF TECHNIQUES
• information-theoretic arguments
• decision tree
• problem reduction
• adversary arguments
INFORMATION-THEORETIC ARGUMENTS
based on counting the number of items in the problems' input that must be processed and the number of output items that need to be produced
list all permutations of r?-elements sequence the number of permutation is n\; lower bound is fi(r?!); problem complexity is 0(r?!)
evaluate a polynomial of degree n at a given point x lower bound is Q(n); problem complexity is 0(r?)
product of two n-by-n matrices
lower bound is ^(r?2) (size of the result); upper bound is (9(r?log27); problem complexity is 111
Traveling Salesperson
lower bound is ^(r?2) (number of graph edges); upper bound is exponential; problem complexity is 111
9
DECISION TREES
• many algorithms work by comparing items of their inputs
• we study performance of such algorithms with a device called the decision tree
• decision tree represents computations on inputs of length n
• each internal node represents a key comparison
• node's degree is proportional to the number of possible answers and node's subtrees contain information about subsequent comparisons
• each leaf represents a possible outcome; the number of leaves must be at least as large as the number of possible outcomes
• the algorithms's work on a particular input of size n can be traced by a path from the root to a leaf
10
DECISION TREES AND LOWER BOUNDS
• a tree with a given number of leaves has to be tall enough
• a tree with degree k and / leaves has depth at least [log^ /]
• fi(log/(r?)) is the lower bound for problem complexity
(/(r?) is the number of possible outputs for inputs of length n)
DECISION TREES FOR SEARCHING A SORTED ARRAY
Input sorted sequence of numbers (xi,... , xn), number x Output index / such that x; = x or NONE
• key comparisons
• n + 1 possible outcomes
• lower bound fi(logr?)
• lower bound is tight
12
DECISION TREES FOR SORTING ALGORITHMS
Input sequence of pairwise different numbers (xi,... ,xn) Output permutation l~l such that xn(i) < xn(2) < • • • < *n(n)
• key comparisons: x; < xj
• n\ possible outcomes (number of permutations)
• lower bound fi(logr?!)
• logr?! e Q(n\ogn) (viz Stirling formulae)
• lower bound is tight
PROBLEM REDUCTION
if we know a lower bound for problem Q and problem Q reduces to problem P then lower bound for Q is as well a lower bound for P
Q element uniqueness problem in (xi,... , xn)? P Euclidean minimum spanning tree problem
lower bound for the element uniqueness problem is fi(r?logr?)
reduction graph with vertices (xi, 0),... (xn, 0)
checking whether the minimum spanning tree contains a zero-length
edge answers the question about uniqueness of the given numbers
claim lower bound for the Euclidean minimum spanning tree problem is ft(r? log n)
ADVERSARY ARGUMENTS
The idea is that an all-powerful malicious adversary pretends to choose an input for the algorithm. When the algorithm asks a question about the input, the adversary answers in whatever way will make the algorithm do the most work. If the algorithm does not ask enough queries before terminating, then there will be several different inputs, each consistent with the adversary's answers, that should result in different outputs. In this case, whatever the algorithm outputs, the adversary can 'reveal' an input that is consistent with its answers, but contradists the algorithm's output, an then claim that that was the input that he was using all along.
15
ADVERSARY FOR THE MAXIMUM PROBLEM
The adversary originally pretends that x\ = / for all i, and answers all comparison queries accordingly Whenever the adversary reveals that x\ < Xj, he marks x\ as an item that the algorithm knows (or should know) is not the maximum element. At most one element x\ is marked after each comparison. Note that xn is never marked. If the algorithm does less than n — 1 comparisons before it terminates, the adversary must have at least one other unmarked element x^ 7^ xn. In this case, the adversary can change the value of x^ from k to n + 1 making x^ the largest element, without being inconsistent with any of the comparisons that the algorithm has performed. However, xn is the maximum element in the original input, and x^ is the largest element in the modified input, so the algorithm cannot possibly give the correct answer for both cases.
Any comparison-based algorithm solving the maximum element problem must perform at least n — 1 comparisons.
The decision tree model gives lower bound log2 n. 16
ADVERSARY FOR THE MAXIM. AND MINIM. PROBLEM
Similar arguments as for the maximum problem. Whenever the adversary reveals that x\ < xj, he marks x\ as an item that the algorithm knows is not the maximum element, and he marks xj as an item that the algorithm knows is not the minimum element. Whenever two already marked elements are compared, at most one new mark can be added. If the algorithm does less than [n/2\ + n — 2 comparisons before it terminates, the adversary must have at least two elements that can be both the maximum or both minimum, so the algorithm cannot possibly give the correct answer .
Any comparison-based algorithm solving the maximum and minimum element problem must perform at least \n/2\ + n — 2 comparisons.
17
Algorithm Complexity Analysis
Algorithm Complexity Analysis
Recursive algorithms
5. Divide and Conquer
► mergesort
► counting inversions
► randomized quicksort
► median and selection
► closest pair of points
Section 9.3
Median and selection problems
Selection. Given n elements from a totally ordered universe, find kth smallest.
• Minimum: k=\\ maximum: k = n.
• Median: k=[(n+ 1)/2J.
• 0(n) compares for min or max.
• 0(n log n) compares by sorting.
• 0(n log k) compares with a binary heap. <— max heap with & smallest
Applications. Order statistics; find the "top k"\ bottleneck paths, ...
Q. Can we do it with 0(ri) compares? A. Yes! Selection is easier than sorting.
43
Randomized quickselect
• Pick a random pivot element p e A.
• 3-way partition the array into L, M, and R
o
Recur in one subarray—the one containing the kth smallest element.
Quick-Select(A, k)
Pick pivot pGA uniformly at random.
(L, M, R) <- PARTITION-3-WAY(A,/?). <-0(w)
If (k < I LI) Return Quick-Select(L, &). <-txo
Else if (k > ILI + I Ml) Return Quick-Select^, k-\L\- \M\) <-Tin -1 - i)
Else if (k = I LI) Return/?.
Randomized quickselect analysis
Intuition. Split candy bar uniformly => expected size of larger piece is 3A.
T(n) < T(3n/ 4) +n T(ri) < 4n
\
not rigorous: can t assume E[T(i)] < T(E[i])
Def. T(n, k) = expected # compares to select kih smallest in array of length < n.
Def. T{n) = maxyt T(n, k).
Proposition. T(n) < An.
Pf. [ by strong induction on n]
• Assume true for 1,2,1.
• T(n) satisfies the following recurrence:
can assume we always recur of larger of two subarrays since Tin) is monotone non-decreasing
/
T(n) < n + l/n[ 2T(n/2) + ... + 2T(n - 3) + 2T(n - 2) + 2T(n - 1) ]
< n + l/n[ 8(n/^)+...+8(n-3) + 8(n-2) + 8(n-l)]
< n + 1 / n (3n2) = 4 n. ■
tiny cheat: sum should start at T([n/2\)
inductive hypothesis
45
Selection in worst-case linear time
Goal. Find pivot element p that divides list of n elements into two pieces so that each piece is guaranteed to have < 7/10 n elements.
Q. How to find approximate median in linear time?
A. Recursively compute median of sample of < 2/10 n elements.
T(n) =
0(1)
7 (7/10 ft) + T (2/10 n) + 0(n)
if n= 1
otherwise
two subproblems of different sizes!
=^ T(n) = 0(//)
we'll need to show this
46
Choosing the pivot element
• Divide n elements into [n 15 J groups of 5 elements each (plus extra).
Choosing the pivot element
• Divide n elements into [n 15 J groups of 5 elements each (plus extra).
• Find median of each group (except extra).
medians
Choosing the pivot element
Divide n elements into [n 15 J groups of 5 elements each (plus extra). Find median of each group (except extra). Find median of [n 15 J medians recursively. Use median-of-medians as pivot element.
medians
median of medians
/
© © © © © o o ©
© © © © © © © © © ©
o © © o 0 © © ©
© © © © © © © © © © ©
© © © • © © © © © ©
n = 54
49
Median-of-medians selection algorithm
Mom-Select(A, k)
w <- I Al. If (n < 50)
return kth smallest of element of A via mergesort.
Group A into [n I 5 J groups of 5 elements each (ignore leftovers). B <— median of each group of 5.
p <- m0m-select(5, [n I 10J) <- median of medians
(l, M, R) <- Partition-3-way(A,/?).
If (k < I LI) Return Mom-Select(L, k).
Else if (k > ILI + I Ml) Return Mom-Select(#, ^ -1LI -1 Ml)
Else Return p.
50
Analysis of median-of-medians selection algorithm
• At least half of 5-element medians < p.
medians
Analysis of median-of-medians selection algorithm
• At least half of 5-element medians < p.
• At least |> / 5J / 2j = [n 110J medians < p.
medians
p.
medians
Analysis of median-of-medians selection algorithm
• At least half of 5-element medians > p.
• At least [[n 15 J / 2 J = [n 110J medians > p.
medians >p
Analysis of median-of-medians selection algorithm
• At least half of 5-element medians > p.
• At least [[n 15 J / 2 J = [n 110J medians > p.
• At least 3 [nl 10J elements > p.
medians >p
si
medians/? ^ (^2^) (5?) (7^ ^24^ (^)
\© ©©©©©©©@0© ©©0©©©0©O©©
©©©©©©©©©©©
©©©©©©©©©0
Median-of-medians selection algorithm recurrence
Median-of-medians selection algorithm recurrence.
• Select called recursively with [n 15 J elements to compute MOM p.
• At least 3 [nl 10J elements < p.
• At least 3 [nl 10J elements > p.
• Select called recursively with at most n -3 [nl 10J elements.
Def. C(n) = max # compares on any array of n elements.
C(n) < CO/5J) + C(n-3Ln/10j) +
median of recursive computing median of 5
medians select (< 6 compares per group)
partitioning (< n compares)
Intuition.
• C(n) is going to be at least linear in n => C(n) is super-additive.
• Ignoring floors, this implies that C(n) < C(nl5 + n-3nll0)+lll5n
= C(9n/10)+ 11/5« ^> C(«) < 22«.
Median-of-medians selection algorithm recurrence
Median-of-medians selection algorithm recurrence.
• Select called recursively with [n 15 J elements to compute MOM p.
• At least 3 [nl 10J elements < p.
• At least 3 [nl 10J elements > p.
• Select called recursively with at most n -3 [nl 10J elements.
Def. C(n) = max # compares on any array of n elements.
C(n) < CO/5J) + C(n-3Ln/10j) +
median of recursive computing median of 5
medians select (< 6 compares per group)
partitioning (< n compares)
Now, let's solve given recurrence.
• Assume n is both a power of 5 and a power of 10 ?
• Prove that C(n) is monotone non-decreasing.
58
Divide-cmd-conquer: quiz 4
Consider the following recurrence
C(n)
0 if n < 1
C(Ln/5j) + C(n-3Ln/10j) + ^n if n > 1
Is C(w) monotone non-decreasing?
A. Yes, obviously.
B. Yes, but proof is tedious.
C. Yes, but proof is hard.
D. No.
59
Median-of-medians selection algorithm recurrence
Analysis of selection algorithm recurrence.
• T{n) = max # compares on any array of < n elements.
• Tin) is monotone non-decreasing, but C(«) is not!
if n< 50
T(n) < \
{ max{ T(n- 1), T([n/5\) + T(n - 3[n/10j) + ^n) } if n > 50
Claim. Tin) < 44n.
Pf. [ by strong induction ]
• Base case: T(n) < 6n for n < 50 (mergesort).
• Inductive hypothesis: assume true for 1,2,1.
• Induction step: for n > 50, we have either T(n) < T(n-1) < 44n or
T(n) < 7XL»/5J) + T(n-3[nl 10\)+ 11/5 n
inductive hypothesis
> < 44 (L« / 5J) + 44 (n - 3 [n I lOj) + 11/5 n
< 44(n/5) + 44^-44(^/4)+ 11/5« ^
for w>50, 3 |V 10J > w/4
= 44«. ■
60
Divide-cmd-conquer: quiz 5
Suppose that we divide n elements into [n/r\ groups of r elements each, and use the median-of-medians of these [n/r\ groups as the pivot. For which r is the worst-case running time of select 0{n) ?
A. r = 3
B. r = 7
C. Both A and B.
D. Neither A nor B.
61
Linear-time selection retrospective
Proposition. [Blum-Floyd-Pratt-Rivest-Tarjan 1973] There exists a compare-based selection algorithm whose worst-case running time is 0(n).
Time Bounds for Selection*
Manuel Blum, Robert W. Floyd, Vaughan Pratt, Ronald L. Rivest, and Robert E. Tarjan
Department of Computer Science, Stanford University, Stanford, California 94305 Received November 14, 1972
The number of comparisons required to select the i-th smallest of n numbers is shown to be at most a linear function of n by analysis of a new selection algorithm—PICK. Specifically, no more than 5.4305 n comparisons are ever required. This bound is improved for extreme values of and a new lower bound on the requisite number of comparisons is also proved.
Theory.
• Optimized version of BFPRT: < 5.4305 n compares.
• Upper bound: [Dor-Zwick 1995] < 2.95 n compares.
• Lower bound: [Dor-Zwick 1 999] > (2 + 2-80) n compares
Practice. Constants too large to be useful.
62
Algorithm Complexity Analysis
Iterative algorithms
PEARSON
1. Stable Matching
► stable matching problem
► Gale-Shapley algorithm
► hospital optimality
► context
JON KLEINBERG • EVA TARDOS
Lecture slides by Kevin Wayne Copyright © 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 2/5/18 3:54 PM
1. Stable Matching
► stable matching problem
► Gale-Shapley algorithm
► hospital optimality
► context
Section 1.1
Matching med-school students to hospitals
Goal. Given a set of preferences among hospitals and med-school students, design a self-reinforcing admissions process.
Unstable pair. Hospital h and student s form an unstable pair if both:
• h prefers s to one of its admitted students.
• s prefers h to assigned hospital.
Stable assignment. Assignment with no unstable pairs.
• Natural and desirable condition.
• Individual self-interest prevents any hospital-student side deal.
Stable matching problem: input
Input. A set of n hospitals //and a set of n students S.
• Each hospital hGHranks students.
one student per hospital (for now)
• Each student s e S ranks hospitals.
favorite 1 least favorite 1 favorite 1 least favorite 1
1st 2nd 1st 2nd 3rd
Atlanta Xavier Yolanda Zeus Xavier Boston Atlanta Chicago
Boston Yolanda Xavier Zeus Yolanda Atlanta Boston Chicago
Chicago Xavier Yolanda Zeus Zeus Atlanta Boston Chicago
hospitals' preference lists students' preference lists
4
Perfect matching
Def. A matching M is a set of ordered pairs h-s with «e//and sGS s.t.
• Each hospital hGHappears in at most one pair of M.
• Each student sGS appears in at most one pair of M.
Def. A matching Mis perfect if IMI = \H\ = \S\ = n.
1st 2nd 3rd
Atlanta Xavier Yolanda Zeus
Boston Yolanda Xavier Zeus
Chicago Xavier Yolanda Zeus
a perfect matchin
1st 2nd 3rd
Xavier Boston Atlanta Chicago
Yolanda Atlanta Boston Chicago
Zeus Atlanta Boston Chicago
M = { A-Z, B-Y, C-X }
Unstable pair
Def. Given a perfect matching M, hospital h and student s form an unstable pair if both:
• h prefers s to matched student.
• s prefers h to matched hospital.
Key point. An unstable pair h-s could each improve by joint action.
1st 2nd 3rd 1st 2nd 3rd
Atlanta Xavier Yolanda Zeus Xavier Boston Atlanta Chicago
Boston Yolanda Xavier Zeus Yolanda Atlanta Boston Chicago
Chicago Xavier Yolanda Zeus Zeus Atlanta Boston Chicago
A-Y is an unstable pair for matching M = { A-Z, B-Y, C-X }
6
Stable matching: quiz 1
Which pair is unstable in the matching { A-X, B-Z, C-Y } ?
A. A-Y.
B. B-X.
C. B-Z.
D. None of the above.
1st 2nd 3rd 1st 2nd 3rd
Atlanta Xavier Yolanda Zeus Xavier Boston Atlanta Chicago
Boston Yolanda Xavier Zeus Yolanda Atlanta Boston Chicago
Chicago Xavier Yolanda Zeus Zeus Atlanta Boston Chicago
Stable matching problem
Def. A stable matching is a perfect matching with no unstable pairs.
Stable matching problem. Given the preference lists of n hospitals and n students, find a stable matching (if one exists).
1st 2nd 1st 2nd 3rd
Atlanta Xavier Yolanda Zeus Xavier Boston Atlanta Chicago
Boston Yolanda Xavier Zeus Yolanda Atlanta Boston Chicago
Chicago Xavier Yolanda Zeus Zeus Atlanta Boston Chicago
a stable matching M = { A-X, B-Y, C-Z }
Stable roommate problem
Q. Do stable matchings always exist? A. Not obvious a priori.
Stable roommate problem.
• 2n people; each person ranks others from 1 to 2n- 1
• Assign roommate pairs so that no unstable pairs.
1st 2nd 3rd
A B c D
B C A D
C A B D
D A B C
no perfect matching is stable
A-B, C-D A-C, B-D A-D, B-C
B-C unstable A-B unstable A-C unstable
Observation. Stable matchings need not exist.
10
1. Stable Matching
► stable matching problem
► Gale-Shapley algorithm
► hospital optimality
► context
Section 1.1
Gale-Shapley deferred acceptance algorithm
An intuitive method that guarantees to find a stable matching.
Gale-Shapley {preference lists for hospitals and students) initialize Mto empty matching.
while (some hospital h is unmatched and hasn't proposed to every student) s <- first student on h's list to whom h has not yet proposed. if (s is unmatched)
Add h—s to matching M. else if (s prefers h to current partner h')
Replace h'-s with h-s in matching M.
else
s rejects h. return stable matching M.
12
Proof of correctness: termination
Observation 1. Hospitals propose to students in decreasing order of preference.
Observation 2. Once a student is matched, the student never becomes unmatched; only "trades up."
Claim. Algorithm terminates after at most n2 iterations of while loop.
Pf. Each time through the while loop, a hospital proposes to a new student.
Thus, there are at most n2 possible proposals. ■
1st 2nd 3rd 4th 5th
A V W X Y
b W X Y V Z
C X Y V W Z
d Y V W z
e V W X Y z
n(n-l)
1st 2nd 3rd 4th 5th
V C D E A
w D E A B
X D E A B C
Y A B C D
z B C D E
1 proposals
Proof of correctness: perfect matching
Claim. Gale-Shapley outputs a matching. Pf.
• Hospital proposes only if unmatched. => matched to < 1 student
• Student keeps only best hospital. => matched to < 1 hospital
Claim. In Gale-Shapley matching, all hospitals get matched. Pf. [by contradiction]
• Suppose, for sake of contradiction, that some hospital hGH\s unmatched upon termination of Gale-Shapley algorithm.
• Then some student, say s e S, is unmatched upon termination.
• By Observation 2, s was never proposed to.
• But, h proposes to every student, since h ends up unmatched. *
Claim. In Gale-Shapley matching, all students get matched. Pf. [by counting]
• By previous claim, all n hospitals get matched.
• Thus, all n students get matched. ■
Proof of correctness: stability
Claim. In Gale-Shapley matching M*, there are no unstable pairs. Pf. Consider any pair h-s that is not in M*.
• Case 1: h never proposed to s.
hospitals propose in
=> h prefers its Gale-Shapley partner s' to s. <— decreasing order
of preference
=> h-s is not unstable.
• Case 2: h proposed to s. => s rejected h (either right away or later) => s prefers Gale-Shapley partner K to h. =^> h-s is not unstable.
students only trade up
• In either case, the pair h-s is not unstable. ■
Gale-Shapley matching M*
h-s' h'-s
1 5
Summary
Stable matching problem. Given n hospitals and n students, and their preference lists, find a stable matching if one exists.
Theorem. [Gale-Shapley 1962] The Gale-Shapley algorithm guarantees to find a stable matching for any problem instance.
COLLEGE ADMISSIONS AND THE STABILITY OF MARRIAGE
D. GALE* and L. S. SHAPLEY, Brown University and the RAND Corporation
1. Introduction. The problem with which we shall be concerned relates to the following typical situation: A college is considering a set of n applicants of which it can admit a quota of only q. Having evaluated their qualifications, the admissions office must decide which ones to admit. The procedure of offering admission only to the q best-qualified applicants will not generally be satisfactory, for it cannot be assumed that all who are offered admission will accept. Accordingly, in order for a college to receive q acceptances, it will generally have to offer to admit more than q applicants. The problem of determining how many and which ones to admit requires some rather involved guesswork. It may not be known (a) whether a given applicant has also applied elsewhere; if this is known it may not be known (b) how he ranks the colleges to which he has applied; even if this is known it will not be known (c) which of the other colleges will offer to admit him. A result of all this uncertainty is that colleges can expect only that the entering class will come reasonably close in numbers to the desired quota, and be reasonably close to the attainable optimum in quality.
16
Stable matching: quiz 2
Do all executions of Gale-Shapley lead to the same stable matching?
A. No, because the algorithm is nondeterministic.
B. No, because an instance can have several stable matchings.
C. Yes, because each instance has a unique stable matching.
D. Yes, even though an instance can have several stable matchings and the algorithm is nondeterministic.
1. Stable Matching
► stable matching problem
► Gale-Shapley algorithm
► hospital optimality
► context
Section 1.1
Understanding the solution
For a given problem instance, there may be several stable matchings.
1st 2nd 3rd
X B A c
Y A B c
Z A B c
an instance with two stable matchings: S = { A-X, B-Y, C-Z } and S' = { A-Y, B-X, C-Z }
19
Understanding the solution
Def. Student s is a valid partner for hospital h if there exists any stable matching in which /*and s are matched.
Ex.
• Both X and Y are valid partners for A.
• Both X and Y are valid partners for B.
• Z is the only valid partner for C.
1st 2nd 1st 2nd 3rd
A X Y Z X B A c
B Y X Z Y A B c
C X Y Z Z A B c
an instance with two stable matchings: S = { A-X, B-Y, C-Z } and S' = { A-Y, B-X, C-Z }
20
Stable matching: quiz 3
Who is the best valid partner for W in the following instance?
. 6 stable matchings /»■ -
{ A-W, B-X, C-Y, D-Z }
B. { A-X, B-W, C-Y, D-Z }
{ A-X, B-Y, C-W, D-Z }
c.
{ A-Z, B-W, C-Y, D-X }
D. { A-Z, B-Y, C-W, D-X }
{ A-Y, B-Z, C-W, D-X }
1st 2nd 3rd 4th
A Y z X w
B Z Y W X
C w Y X z
X Z W Y
1st 2nd 3rd 4th
w D A B C
X C B A D
Y C B A D
z D A B C
Understanding the solution
Def. Student s is a valid partner for hospital h if there exists any stable matching in which /*and s are matched.
Hospital-optimal assignment. Each hospital receives best valid partner.
• Is it a perfect matching?
• Is it stable?
Claim. All executions of Gale-Shapley yield hospital-optimal assignment. Corollary. Hospital-optimal assignment is a stable matching!
23
Hospital optimality
Claim. Gale-Shapley matching M* is hospital-optimal. Pf. [by contradiction]
• Suppose a hospital is matched with student other than best valid partner.
• Hospitals propose in decreasing order of preference.
=> some hospital is rejected by a valid partner during Gale-Shapley
• Let h be first such hospital, and let s be the first valid partner that rejects h.
• Let M be a stable matching where h and s are matched.
• When s rejects h in Gale-Shapley, s forms (or re-affirms) commitment to a hospital, say K.
s prefers h' to h.
stable matching M
• Let s' be partner of K in M.
• h! had not been rejected by any valid partner (including s') at the point when h is rejected by s. <—
• Thus, K had not yet proposed to s' when h! proposed to s.
because this is the first rejection by a valid partner
h' prefers s to s',
• Thus, h'-s is unstable in M, a contradiction.
24
Student pessimality
Q. Does hospital-optimality come at the expense of the students? A. Yes.
Student-pessimal assignment. Each student receives worst valid partner.
Claim. Gale-Shapley finds student-pessimal stable matching M*. Pf. [by contradiction]
• Suppose h-s matched in M* but h is not the worst valid partner for s.
• There exists stable matching Min which s is paired with a hospital, say ti, whom s prefers less than h.
s prefers h to ti,
Let s' be the partner of h in M.
By hospital-optimality, s is the best valid partner for h.
h prefers s to s'.
• Thus, h-s is an unstable pair in M, a contradiction
stable matching M
25
Stable matching: quiz 4
Suppose each agent knows the preference lists of every other agent before the hospital propose-and-reject algorithm is executed. Which is true?
A. No hospital can improve by falsifying its preference list.
B. No student can improve by falsifying their preference list.
C. Both A and B.
D. Neither A nor B.
26
1. Stable Matching
► stable matching problem
► Gale-Shapley algorithm
► hospital optimality
► context
Section 1.1
Extensions
Extension 1. Some agents declare others as unacceptable. Extension 2. Some hospitals have more than one position Extension 3. Unequal number of positions and students.
med-school student unwilling to work in Cleveland
> 43K med-school students; only 31 K positions
Def. Matching M is unstable if there is a hospital h and student s such that:
• h and s are acceptable to each other; and
• Either s is unmatched, or s prefers h to assigned hospital; and
• Either h does not have all its places filled, or h prefers s to at least one of its assigned students.
Theorem. There exists a stable matching.
Pf. Straightforward generalization of Gale-Shapley algorithm.
29
Historical context
National resident matching program (NRMP).
• Centralized clearinghouse to match med-school students to hospitals.
• Began in 1 952 to fix unraveling of offer dates.
• Originally used the "Boston Pool" algorithm.
• Algorithm overhauled in 1998.
- med-school student optimal
- deals with various side constraints
z || I . | . . | \ stable matching no longer
(e.g., allow couples to match together) «— guaranteedyt0 existy
hospitals began making offers earlier and earlier, up to 2 years in advance
The Redesign of the Matching Market for American Physicians: Some Engineering Aspects of Economic Design
By Alvin E. Roth and Elliott Peranson*
We report on the design of the new clearinghouse adopted by the National Resident Matching Program, which annually fills approximately 20,000 jobs for new physicians. Because the market has complementarities between applicants and between positions, the theory of simple matching markets does not apply directly. However, computational experiments show the theory provides good approximations. Furthermore, the set of stable matchings, and the opportunities for strategic manipulation, are surprisingly small. A new kind of "core convergence" result explains this; that each applicant interviews only a small fraction of available positions is important. We also describe engineering aspects of the design process. (JEL C78, B41, J44)
?MATCH
NATIONAL RESIDENT MATCHING PROGRAM'
2012 Nobel Prize in Economics
Lloyd Shapley. Stable matching theory and Gale-Shapley algorithm.
COLLEGE ADMISSIONS AND THE STABILITY OF MARRIAGE
D. GALE* and L. S. SHAPLEY, Brown University and the RAND Corporation
1. Introduction. The problem with which we shall be concerned relates to Original applicationS\
the following typical situation: A college is considering a set of n applicants of ^ CO 1160 6 admissions and
which it can admit a quota of only q. Having evaluated their qualifications, the
admissions office must decide which ones to admit. The procedure of offering OppOSite-SeX marriage
admission only to the q best-qualified applicants will not generally be satisfactory, for it cannot be assumed that all who are offered admission will accept.
Alvin Roth. Applied Gale-Shapley to matching med-school students with hospitals, students with schools, and organ donors with patients.
New York City high school match
8th grader. Ranks top-5 high schools.
High school. Ranks students (and limit).
Goal. Match 90K students to 500 high school programs.
How Game Theory Helped Improve New York City's High School Application Process
By TRACY TULLIS DEC. 5,2014
Tuesday was the deadline for eighth graders in New York City to submit applications to secure a spot at one of 426 public high schools. After months of school tours and tests, auditions and interviews, 75,000 students have entrusted their choices to a computer program that will arrange their school assignments for the coming year. The weeks of research and deliberation will be reduced to a fraction of a second of mathematical calculation: In just a couple of hours, all the sorting for the Class of 2019 will be finished.
32
Questbridge national college match
Low-income student. Ranks colleges.
College. Ranks students willing to admit (and limit).
Goal. Match students to colleges.
A modern application
Content delivery networks. Distribute much of world's content on web.
User. Preferences based on latency and packet loss. ^Akamai Web server. Preferences based on costs of bandwidth and co-location. Goal. Assign billions of users to servers, every 1 0 seconds.
Algorithmic Nuggets in Content Delivery
Bruce M. Maggs Ramesh K. Sitaraman
Duke and Akamai UMass, Amherst and Akamai
bmm@cs.duke.edu ramesh@cs.umass.edu
This article is an editorial note submitted to CCR. It has NOT been peer reviewed. The authors take full responsibility for this article's technical content. Comments can be posted through CCR Online.
ABSTRACT
This paper "peeks under the covers" at the subsystems that provide the basic functionality of a leading content delivery network. Based on our experiences in building one of the largest distributed systems in the world, we illustrate how sophisticated algorithmic research has been adapted to balance the load between and within server clusters, manage the caches on servers, select paths through an overlay routing network, and elect leaders in various contexts. In each instance, we first explain the theory underlying the algorithms, then introduce practical considerations not captured by the theoretical models, and finally describe what is implemented in practice. Through these examples, we highlight the role of algorithmic research in the design of complex networked systems. The paper also illustrates the close synergy that exists between research and industry where research ideas cross over into products and product requirements drive future research.
«—*. Content
Authoritative Name Server DNS (Global and Local Load
Balancing)
34
Amortized Complexity
Amortized Complexity
Amortized analysis
Data structures I, II, III, and IV
/. Amortized Analysis
II. Binary and Binomial Heaps
III. Fibonacci Heaps
IV. Union-Find
Lecture slides by Kevin Wayne
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 2/2/18 5:50 AM
Data structures
Static problems. Given an input, produce an output.
Ex. Sorting, FFT, edit distance, shortest paths, MST, max-flow, ...
Dynamic problems. Given a sequence of operations (given one at a time), produce a sequence of outputs.
Ex. Stack, queue, priority queue, symbol table, union-find, ....
Algorithm. Step-by-step procedure to solve a problem.
Data structure. Way to store and organize data.
Ex. Array, linked list, binary heap, binary search tree, hash table, ...
Appetizer
Goal. Design a data structure to support all operations in 0(1) time.
• Init(w): create and return an initialized array (all zero) of length n.
• Read(A, i)\ return element i in array.
• Write(A, i, value): set element i in array to value.
true in C or C++, but not Java
Assumptions. ^
• Can malloc an uninitialized array of length n in 0(1) time.
• Given an array, can read or write element i in 0(1) time.
Remark. An array does Init in S(n) time and Read and Write in 0(1) time.
3
Appetizer
Data structure. Three arrays A[l.. n], B[l.. n], and Cfl.. n], and an integer k.
• A[i] stores the current value for Read (if initialized).
• k = number of initialized entries.
• C\j] = index of jth initialized element for j= 1,
• |f c\J] = i, then B[i] =j fory = 1, ...,k.
Theorem. A[i] is initialized iff both I < B[í] < k and C[B[i\\ = i. Pf. Ahead.
1 2 3 4 5 6 7 8
A[] H22 55 99 ? 33 ? ■
BM 13 4 1 ? 2 ? ■
CM 4 6 2 3 ? ? ? ■
k = 4
A[4]=99, A[6] = 33, A[2] = 22, and A[3] = 55 initialized in that order
Appetizer
Init (A, n) k ^0.
A«- malloc(ft).
5 <- Malloc(^).
C^malloc(n).
Read (A, i)
If (Is-Initialized (AD']))
Return A [/]. Else
Return 0.
Write (A, i, value)
If (Is-Initialized (AD'])) A[i] <- value.
Else & <- & + 1. AD] «- value. B[i] «- k. C[k] «- i.
IS-lNITIALIZED (A, /)
IF (1<#D] < *) and (C[fi[i]] = i)
Return true. Else
Return /ate.
Appetizer
Theorem. A[i] is initialized iff both 1 k, then A[i] clearly uninitialized.
• If 1 < B[i] < k by coincidence, then we still can't have C[B[i]] = i because none of the entries Cfl.. k] can equal i. ■
1 2 3 4 5 6 7 8
A[] H22 55 99 ? 33 ? ■
B[] 1 3 4 1 ? 2 ? ■
CM 4 6 2 3 ? ? ? ■
k = 4
A[4]=99, A[6] = 33, A[2] = 22, and A[3] = 55 initialized in that order
Amortized Analysis
► binary counter
► multi-pop stock
► dynamic toble
Lecture slides by Kevin Wayne
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 2/2/18 5:50 AM
Amortized analysis
Worst-case analysis. Determine worst-case running time of a data structure operation as function of the input size n. *\
can be too pessimistic if the only way to encounter an expensive operation is when there were lots of previous cheap operations
Amortized analysis. Determine worst-case running time of a sequence of n data structure operations.
Ex. Starting from an empty stack implemented with a dynamic table, any sequence of n push and pop operations takes 0{n) time in the worst case.
9
Amortized analysis: applications
• Splay trees.
• Dynamic table.
• Fibonacci heaps.
• Garbage collection.
• Move-to-front list updating.
• Push-relabel algorithm for max flow.
• Path compression for disjoint-set union.
• Structural modifications to red-black trees.
• Security, databases, distributed computing, ...
SIAM J. Alg. Disc. METH. © 1985 Society for Industrial and Applied Mathematics
Vol. 6, No. 2, April 1985 016
AMORTIZED COMPUTATIONAL COMPLEXITY*
ROBERT ENDRE TARJANt
Abstract. A powerful technique in the complexity analysis of data structures is amortization, or averaging over time. Amortized running time is a realistic but robust complexity measure for which we can obtain surprisingly tight upper and lower bounds on a variety of algorithms. By following the principle of designing algorithms whose amortized complexity is low, we obtain "self-adjusting" data structures that are simple, flexible and efficient. This paper surveys recent work by several researchers on amortized complexity.
ASM(MOS) subject classifications. 68C25, 68E05
10
THOMAS H CORMEN CHARLES E. LEISE RSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITH MS
THIRD EDITION
Amortized Analysis
► binary counter
► multi-pop stack
► dynamic table
Chapter 17
Binary counter
Goal. Increment a k-b\t binary counter (mod 2k). Representation. aj = jth least significant bit of counter.
value ^^^^^^^^
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 ■
2 0 0 0 0 0 0 1 0
3 0 0 0 0 0 0 1 ■
4 0 0 0 0 0 1 0 0
5 0 0 0 0 0 1 0 ■
6 0 0 0 0 0 1 1 0
7 0 0 0 0 0 1 1 ■
8 0 0 0 0 1 0 0 0
9 0 0 0 0 1 0 0 ■
10 0 0 0 0 1 0 1 0
11 0 0 0 0 1 0 1 1
12 0 0 0 0 1 1 0 0
13 0 0 0 0 1 1 0 ■
14 0 0 0 0 1 1 1 0
15 0 0 0 0 1 1 1 ■
16 0 0 0 1 0 0 0 0
Cost model. Number of bits flipped.
Binary counter
Goal. Increment a k-b\t binary counter (mod 2k). Representation. aj = jth least significant bit of counter.
Counter value ^ ^ ^ ^ ^ ^ ^ ^
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 ■
2 0 0 0 0 0 0 1 0
3 0 0 0 0 0 0 1 ■
4 0 0 0 0 0 1 0 0
5 0 0 0 0 0 1 0 1
6 0 0 0 0 0 1 1 0
7 0 0 0 0 0 1 1 ■
8 0 0 0 0 1 0 0 0
9 0 0 0 0 1 0 0 ■
10 0 0 0 0 1 0 1 0
11 0 0 0 0 1 0 1 1
12 0 0 0 0 1 1 0 0
13 0 0 0 0 1 1 0 ■
14 0 0 0 0 1 1 1 0
15 0 0 0 0 1 1 1 ■
16 0 0 0 1 0 0 0 0
Theorem. Starting from the zero counter, a sequence of n Increment
Operations flips 0(nk) bitS. <- overly pessimistic upper bound
Pf. At most k bits flipped per increment. ■
Aggregate method (brute force)
Aggregate method. Analyze cost of a sequence of operations.
Counter value <^<<^<<^<^<^<^<\N a, we store credits in data structure A to pay for future ops; when Ci < a, we consume credits in data structure A.
• Initial data structure D0 starts with 0 credits.
Credit invariant. The total number of credits in the data structure > 0.
^2* -0 «
our job is to choose suitable amortized costs so that this invariant holds
Accounting method (banker's method)
Assign (potentially) different charges to each operation.
• Di = data structure after ith operation. can be more or less
than actual cost
• a = actual cost of Ith operation. /
• ci = amortized cost oUth operation = amount we charge operation i.
• When ct > d, we store credits in data structure A to pay for future ops; when ct < d, we consume credits in data structure A.
• Initial data structure D0 starts with 0 credits.
Credit invariant. The total number of credits in the data structure > 0.
i=l i=l
Theorem. Starting from the initial data structure Do, the total actual cost of any sequence of n operations is at most the sum of the amortized costs.
n n
Pf. The amortized cost of the sequence of n operations is: J2d* ^ J2C*- ■
i = l A i = l
T_ credit
invariant
Intuition. Measure running time in terms of credits (time = money).
Binary counter: accounting method
Credits. One credit pays for a bit flip.
Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting.
• Flip bit j from 0 to 1: charge 2 credits (use one and save one in bit j).
increment
7 6 5 4 3 2 10
Binary counter: accounting method
Credits. One credit pays for a bit flip.
Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting.
• Flip bit j from 0 to 1: charge 2 credits (use one and save one in bit j).
• Flip bit y from 1 to 0: pay for it with the 1 credit saved in bit j.
increment
7 6 5 4 3 2 10
19
Binary counter: accounting method
Credits. One credit pays for a bit flip.
Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting.
• Flip bit j from 0 to 1: charge 2 credits (use one and save one in bit j).
• Flip bit y from 1 to 0: pay for it with the 1 credit saved in bit j.
7 6 5 4 3 2 10
20
Binary counter: accounting method
Credits. One credit pays for a bit flip.
Invariant. Each 1 bit has one credit; each 0 bit has zero credits. Accounting.
• Flip bit j from 0 to 1: charge 2 credits (use one and save one in bit j).
• Flip bit y from 1 to 0: pay for it with the 1 credit saved in bit j.
Theorem. Starting from the zero counter, a sequence of n Increment operations flips 0(ri) bits.
the rightmost 0 bit
pf. |- (unless counter overflows)
• Each Increment operation flips at most one 0 bit to a 1 bit, so the amortized cost per Increment < 2.
• Invariant => number of credits in data structure > 0.
• Total actual cost of n operations < sum of amortized costs < 2«. ■
t
accounting method theorem
21
Potential method (physicist's method)
Potential function. O(A) maps each data structure A to a real number s.t.: . O(D0) = 0.
• O(A) ^ 0 for each data structure A.
Actual and amortized costs.
• a = actual cost of ith operation.
• 6i = ct + O(A) - O(A-i) = amortized cost of/* operation.
Potential method (physicist's method)
Potential function. O(A) maps each data structure A to a real number s.t.: . <|>(Do) = 0.
• 0(A) ^ 0 for each data structure A.
Actual and amortized costs.
• a = actual cost of ith operation.
• q = d + O(A) - O(A-i) = amortized cost ofoperation.
Theorem. Starting from the initial data structure Do, the total actual cost of any sequence of n operations is at most the sum of the amortized costs. Pf. The amortized cost of the sequence of operations is:
n
n
*(A-i))
i=l
i=l
n
$(A>)
i=l
n
23
i=l
Binary counter: potential method
Potential function. Let 3>(D) = number of 1 bits in the binary counter D.
. O(Do) = 0.
• O(D0 > 0 for each A.
increment
Binary counter: potential method
Potential function. Let 3>(D) = number of 1 bits in the binary counter D.
. O(Do) = 0.
• O(D0 > 0 for each A.
increment
25
Binary counter: potential method
Potential function. Let O(D) = number of 1 bits in the binary counter D. . o(D0) = 0. • O(A) > 0 for each A.
0
0
0
0
0
0
Binary counter: potential method
Potential function. Let O(D) = number of 1 bits in the binary counter D.
• <|>(Do) = 0.
• O(A) > 0 for each A.
Theorem. Starting from the zero counter, a sequence of n Increment
operations flips 0(n) bits.
Pf.
• Suppose that the ith Increment operation flips u bits from 1 to 0.
• The actual COSt a < U + \. <- operation flips at most one bit from 0 to 1
(no bits flipped to 1 when counter overflows)
• The amortized cost ct = a - <£(A-i)
< a + 1 — U <- potential decreases by 1 for U bits flipped from 1 to 0
and increases by 1 for bit flipped from 0 to 1
< 2.
• Total actual cost of n operations < sum of amortized costs < In. ■
t
potential method theorem
27
Famous potential functions
Fibonacci heaps. $(H) = 2trees(H) + 2marks(H)
Splay trees. $(T) = ^ [log2 size(x)_
Move-to-front. (L) = 2 inversions(L,L*)
Preflow-push. $(/) = ^ height(v)
v : excess(v) > 0
Red-black trees. $(T) =
'o if X
1 < if £
0 if £
,2 if X
THOMAS H CORMEN CHARLES E. LEISE RSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITHMS
THIRD EDITION
Amortized Analysis
► binary counter
► multi-pop stack
► dynamic table
Section 17.4
Multipop stack
Goal. Support operations on a set of elements:
• Push(S,jc): add element x to stack S.
• Pop(5): remove and return the most-recently added element.
• Multi-PopCS, k): remove the most-recently added & elements.
Multi-Pop (S, k)
For i = 1 to k Pop (S).
Exceptions. We assume Pop throws an exception if stack is empty.
30
Multipop stack
Goal. Support operations on a set of elements:
• Push(S,jc): add element x to stack S.
• Pop(5): remove and return the most-recently added element.
• Multi-PopCS, k): remove the most-recently added & elements.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0(n2) time. Pf.
overly pessimistic upper bound
• Use a singly linked list.
• Pop and Push take 0(1) time each.
• Multi-Pop takes 0(n) time. ■
Multipop stack: aggregate method
Goal. Support operations on a set of elements:
• Push(5,x): add element x to stack S.
• Pop(5): remove and return the most-recently added element.
• Multi-PopCS, k): remove the most-recently added & elements.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0(n) time.
Pf.
• An element is popped at most once for each time that it is pushed.
• There are < n Push operations.
• Thus, there are < n Pop operations (including those made within Multi-Pop). ■
32
Multipop stack: accounting method
Credits. 1 credit pays for either a Push or Pop. Invariant. Every element on the stack has 1 credit.
Accounting.
• Push(5,x): charge 2 credits.
- use 1 credit to pay for pushing x now
- store 1 credit to pay for popping xat some point in the future
• Pop(5): charge 0 credits.
Theorem. Starting from an empty stack, any intermixed sequence of n
Push, Pop, and Multi-Pop operations takes 0(n) time.
Pf.
• Invariant => number of credits in data structure > 0.
• Amortized cost per operation < 2.
• Total actual cost of n operations < sum of amortized costs < In. ■
t
accounting method theorem
33
Multipop stack: potential method
Potential function. Let O(D) = number of elements currently on the stack.
• <£>(D0) = 0.
• O(A) > 0 for each A.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0{n) time.
Pf. [Case 1: push]
• Suppose that the ith operation is a Push.
• The actual cost a =1.
• The amortized cost ct = a - O(A-i) = 1 + 1=2.
34
Multipop stack: potential method
Potential function. Let O(D) = number of elements currently on the stack.
• <|>(Do) = 0.
• O(A) > 0 for each A.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0{n) time.
Pf. [Case 2: pop]
• Suppose that the ith operation is a Pop.
• The actual cost a = 1.
• The amortized cost ct = a - 0(D,_i) = 1-1=0.
Multipop stack: potential method
Potential function. Let O(D) = number of elements currently on the stack.
• 0 for each A.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0{n) time.
Pf. [Case 3: multi-pop]
• Suppose that the ith operation is a Multi-Pop of k objects.
• The actual cost a = k.
• The amortized cost ct = a +0(A) - O(A-i) = k - k = 0. ■
Multipop stack: potential method
Potential function. Let O(D) = number of elements currently on the stack. . <|>(Do) = 0.
• O(A) > 0 for each A.
Theorem. Starting from an empty stack, any intermixed sequence of n Push, Pop, and Multi-Pop operations takes 0(n) time.
Pf. [putting everything together]
• Amortized cost 6i < 2. <— 2 for push;0 for p°p and mum-pop
• Sum of amortized costs q of the n operations < In.
• Total actual cost < sum of amortized cost < In. ■
t
potential method theorem
37
Part I
Data stuctures
18
OVERVIEW
Heaps
Disjoint-sets Data Structures
19
Heaps
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
THOMAS H CORMEN CHARLES E. LEISE RSON RONALD L RIVEST CLIFFORD STEIN
Lecture slides by Kevin Wayne
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 7/25/17 11:07 AM
Priority queues performance cost summary
operation linked list binary heap binomial heap Fibonacci heap t
Make-Heap 0(1) 0(1) 0(1) 0(1)
Is-Empty 0(1) 0(1) 0(1) 0(1)
Insert 0(1) <9(log n) 0(\og n) 0(1)
Extract-M in 0(n) 0(log I!) 0(log n) 0(log n)
Decrease-Key 0(1) 0(log n) 0(\og n) 0(1)
Delete 0(1) 0(log n) (9(log n) 0(log n)
Meld 0(1) 0(n) (9(log n) 0(1)
Find-Min 0(n) 0(1) 0(log n) 0(1)
t amortized
Ahead. 0(1) Insert and Decrease-Key, O(log^) Extract-Min.
Fibonacci heaps
Theorem. [Fredman-Tarjan 1986] Starting from an empty Fibonacci heap, any sequence of m Insert, Extract-Min, and Decrease-Key operations involving n Insert operations takes 0(m + n\ogn) time.
History.
• Ingenious data structure and application of amortized analysis.
• Original motivation: improve Dijkstra's shortest path algorithm from 0(m log n) to 0(m + n log n).
• Also improved best-known bounds for all-pairs shortest paths, assignment problem, minimum spanning trees.
Fibonacci heap: structure
• Set of heap-ordered trees.
\
each child no smaller than its parent
heap-ordered tree
heap H
39 44
Fibonacci heap: structure
• Set of heap-ordered trees.
• Set of marked nodes.
\
used to keep trees bushy (stay tuned)
min
heap H
Fibonacci heap: structure
Heap representation.
• Store a pointer to the minimum node.
• Maintain tree roots in a circular, doubly-linked list.
min
1
heap H
Fibonacci heap: representation
Node representation. Each node stores:
• A pointer to its parent.
• A pointer to any of its children.
• A pointer to its left and right siblings.
• Its rank = number of children.
• Whether it is marked.
min
1
heap H
Fibonacci heap: representation
Operations we can do in constant time:
• Determine rank of a node.
• Find the minimum element.
• Merge two root lists together.
• Add or remove a node from the root list.
• Remove a subtree and merge into root list.
• Link the root of a one tree to root of another tree.
min
1
heap H
Fibonacci heap: notation
notation meaning
n number of nodes
rank(x) number of children of node x
rank(H) max rank of any node in heap H
trees(H) number of trees in heap H
marks(H) number of marked nodes in heap H
n = 14 rank(H) = 3 trees(H) = 5 marks(H) = 3
min
Fibonacci heap: potential function
Potential function.
0(7/) = trees(iZ) + 2 • marks(#)
THOMAS H CORMEN CHARLES E. LEISERSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITH MS
THIRD EDITION
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
Section 19.2
Fibonacci heap: insert
• Create a new singleton tree.
• Add to root list; update min pointer (if necessary).
Fibonacci heap: insert
• Create a new singleton tree.
• Add to root list; update min pointer (if necessary).
insert 21
Fibonacci heap: insert analysis
Actual cost, a = 0(1).
Change in potential. AO = <£(//*)-(//m) = +1. +
one more tree; no change in marks
Amortized cost, ct = a + AO =0(1).
0(7/) = trees(#) + 2 • marks(#)
24
23
7 ■■
21
mm
1
3
heap H
35
41
i
44
1 7
THOMAS H CORMEN CHARLES E. LEISERSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITH MS
THIRD EDITION
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
Section 19.2
Linking operation
Useful primitive. Combine two trees Ti and T2 of rank k.
• Make larger root be a child of smaller root.
• Resulting tree T' has rank k+ 1.
tree Ti tree T2 tree T'
19
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
i
rank
0 12 3 • t t •
I
min 7
w current
24
30
17
I
52
44
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
rank
link 23 to 17
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
rank
link 17 to 7
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
rank
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
rank
29
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
Fibonacci heap: extract the minimum
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
stop (no two trees have same rank)
min
52
41
Fibonacci heap: extract the minimum analysis
Actual cost, a = 0(rank(H)) + 0(trees(H)).
• 0(rank(H)) to meld min's children into root list. <— < rank(H) children
• 0(mnk(H)) + 0(trees(H)) tO Update ITlin. <- < rank(H) + trees(H)- 1 root nodes
• 0(rank(H)) + 0(trees(H)) to consolidate trees. <— number of roots decreases by 1 after
each linking operation
Change in potential. AO < rank(H') + 1 - trees(H).
• No new nodes become marked.
• trees(H') < rank(H') + 1. <- no two trees have same rank after consolidation
Amortized cost. O(log^).
• q = a + AO = 0(rank(H)) + 0(rank(H')).
• The rank of a Fibonacci heap with n elements is O(log^).
\
Fibonacci lemma (stay tuned)
0(7/) = trees(iZ) + 2 • marks(iZ)
36
Fibonacci heap vs. binomial heaps
Observation. If only Insert and Extract-Min operations, then all trees are binomial trees.
\
we link only trees of equal rank
B0 B]
' I
B;
Binomial heap property. This implies rank(H) < \og2n.
Fibonacci heap property. Our Decrease-Key implementation will not preserve this property, but we will implement it in such a way that rank(H)< log^n.
THOMAS H CORMEN CHARLES E. LEISERSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITH MS
THIRD EDITION
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
Section 19.3
Fibonacci heap: decrease key
Intuition for deceasing the key of node x.
• If heap-order is not violated, decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
decrease-key of x from 30 to 7
45 32
24
50
Fibonacci heap: decrease key
Intuition for deceasing the key of node x.
• If heap-order is not violated, decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
decrease-key of x from 23 to 5
24
50
40
Fibonacci heap: decrease key
Intuition for deceasing the key of node x.
• If heap-order is not violated, decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
decrease-key of 22 to 4 decrease-key of 48 to 3 decrease-key of 31 to 2 decrease-key of 17 to 1
50
41
Fibonacci heap: decrease key
Intuition for deceasing the key of node x.
• If heap-order is not violated, decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
• Problem: number of nodes not exponential in rank.
Fibonacci heap: decrease key
Intuition for deceasing the key of node x.
• If heap-order is not violated, decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
• Solution: as soon as a node has its second child cut, cut it off also and meld into root list (and unmark it).
min
Fibonacci heap: decrease key
Case 1. [heap order not violated]
• Decrease key of x.
• Change heap min pointer (if necessary).
decrease-key of x from 46 to 29
min
Fibonacci heap: decrease key
Case 1. [heap order not violated]
• Decrease key of x.
• Change heap min pointer (if necessary).
decrease-key of x from 46 to 29
min
Fibonacci heap: decrease key
Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 29 to 15
min
Fibonacci heap: decrease key
Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 29 to 15
min
Fibonacci heap: decrease key
Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 29 to 15
min
Fibonacci heap: decrease key
Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 29 to 15
min
Fibonacci heap: decrease key
Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 29 to 15
min
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
min
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
min
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5 x min
4fe -38
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
x min
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
x min p
15 ■ 5 - 4\ -38
55
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
x min p
15 ■ 5 - 4\ -38
II 1 \ 71
^P ^P p' ^P ^P ^P ^P ^P ^P
/
second child cut
56
Fibonacci heap: decrease key
Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted atx, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
decrease-key of x from 35 to 5
57
Fibonacci heap: decrease key analysis
Actual cost, a = 0(c), where c is the number of cuts.
• 0(1) time for changing the key.
• 0(1) time for each of c cuts, plus melding into root list.
Change in potential. AO = 0(1) - c.
• trees(H') = trees(H) + c.
• J n-T'\ <- h t u\ i 9 eac'1 cut (excePt first) unmarks a node mar/CS(H ) < marKS^tt) - C + Z. < last cut may or may not mark a node
• AO < c + 2 • (-c + 2) = 4 - c.
Amortized cost. 6 = c* + AO = 0(1).
58
<&(H) = trees(H) + 2 • marks(H)
THOMAS H CORMEN CHARLES E. LEISERSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITH MS
THIRD EDITION
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
Section 19.4
Analysis summary
Insert. 0(1).
Delete-min. 0(rank(H)) amortized. Decrease-key. (9(1) amortized.
Fibonacci lemma. Let H be a Fibonacci heap with n elements. Then, rank(H) = 0(\ogn).
\
number of nodes is exponential in rank
60
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let yu ...,yk denote its current children in the order in which they were linked to x. Then:
• When yt was linked into x, x had at least i - 1 children yu
• Since only trees of equal rank are linked, at that time rankiy^ = rank(x) > i - 1.
• Since then, yt has lost at most one child (or yt would have been cut).
• Thus, right now rankiy^ > i - 2. ■
if i = 1 if i > 2
Pf.
61
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let yu ...,yk denote its current children in the order in which they were linked to x. Then:
rank(yi) >
0 if i = 1
- 2 if i > 2
Def. Let Tk be smallest possible tree of rank k satisfying property.
I A A
F2 = 1 F3 = 2 F4 = 3 F5 = 5
F6 = 8
F7 = 13
62
Bounding the rank
Lemma 1. Fix a point in time. Let x be a node of rank k, and let yu ...,yk denote its current children in the order in which they were linked to x. Then:
rank(yi) >
0 if i = 1
- 2 if i > 2
Def. Let Tk be smallest possible tree of rank k satisfying property.
F6 = 8
F7 = 13
F8 = F6 + F7 = 8+ 13 = 21
63
Bounding the rank
Lemma 2. Let sk be minimum number of elements in any Fibonacci heap of rank k. Then sk > Fk+2, where Fk is the kth Fibonacci number.
Pf. [by strong induction on k]
• Base cases: so = 1 and 51 = 2.
• Inductive hypothesis: assume sl > Fi+2 fori = 0, ...,k- 1.
• As in Lemma 1, let let yu ...,yk denote its current children in the order in which they were linked to x.
Sk > 1 + 1 + (So + Si + ... + Sk-2) (Lemma 1)
> (1 +Fi) + F2 + F3 + ... + Fk (inductive hypothesis) — Fk+2. ■ (Fibonacci fact 1)
64
Bounding the rank
Fibonacci lemma. Let H be a Fibonacci heap with n elements.
Then, rank{H) < log^n, where cj) is the golden ratio = (1 + 75)/2^ 1.618.
Pf.
• Let H is a Fibonacci heap with n elements and rank k.
• Then n > Fk+2 > fyk.
t t
Lemma 2 Fibonacci Fact 2
• Taking logs, we obtain rank(H) = k < log^n. ■
65
Fibonacci fact 1
Def. The Fibonacci sequence is: 0,1,1,2,3,5,8,13,21,...
{0 if k = 0
1 if k = 1
iVi + iV2 iffc>2
Fibonacci fact 1. For all integers k>0, Fk+2 = 1 + F0 + Fi + ... +Fk. Pf. [by induction on k]
• Base case: F2= 1 +F0 = 2.
• Inductive hypothesis: assume Fk+l= 1 +F0 + F1 + ... +F*_i.
Fyt+2 = Fife + Fyfe+i (definition)
= Fyfe + (1 + F0 + Fi + ... + Fyfe-i) (inductive hypothesis)
= 1 + Fo + Fi + ... + Fyfe-i + Fyfe. ■ (algebra)
66
Fibonacci fact 2
Def. The Fibonacci sequence is: 0,1,1,2,3,5,8,13,21,...
{0 if k = 0
1 if k = 1
iVi + iV2 iffc>2
Fibonacci fact 2. F^+2 > (j)*, where (j) = (1+V5)/2^ 1.618. Pf. [by induction on k]
• Base cases: F2 = 1 > 1, F3 = 2 > (j).
• Inductive hypotheses: assume/^ > ((>* andF^+1> §k+1
Fk+2 = Fk + Fjfc+i (definition)
> + (|)^-2 (inductive hypothesis)
= (j)^-2(l + (j)) (algebra)
- ^-2 ^2 (+l)
= (j)*. ■ (algebra)
67
THOMAS H CORMEN CHARLES E. LEISERSON RONALD L RIVEST CLIFFORD STEIN
INTRODUCTION TO
ALGORITHMS
THIRD EDITION
Fibonacci Heaps
► preliminaries
► insert
► extract the minimum
► decrease key
► bounding the rank
► meld and delete
Section 19.2, 19.3
Fibonacci heap: meld
Meld. Combine two Fibonacci heaps (destroying old heaps). Recall. Root lists are circular, doubly-linked lists.
Fibonacci heap: meld
Meld. Combine two Fibonacci heaps (destroying old heaps). Recall. Root lists are circular, doubly-linked lists.
min
Fibonacci heap: meld analysis
Actual cost, a = 0(1). Change in potential. AO = 0. Amortized cost. ct = a + AO = 0(1).
O(H) = trees(iZ) + 2 • marks(H)
mm
Fibonacci heap: delete
Delete. Given a handle to an element x, delete it from heap H.
• decrease-key(7/,x, -oo).
• Extract-M in (h).
Amortized cost, ci = 0(rank(H)).
• 0(1) amortized for Decrease-Key.
• 0(rank(H)) amortized for Extract-Min.
0(#) = trees(iZ) + 2 • marks(#)
Priority queues performance cost summary
operation linked list binary heap binomial heap Fibonacci heap t
Make-Heap 0(1) 0(1) 0(1) 0(1)
Is-Empty 0(1) 0(1) 0(1) 0(1)
Insert 0(1) 0(log n) <9(log n) 0(1)
Extract-Min 0(n) 0(log n) 0(log n) 0(log n)
Decrease-Key 0(1) 0(log n) 0(log n) 0(1)
Delete 0(1) 0(log n) 0(log n) 0(log «)
Meld 0(1) 0(n) 0(log n) 0(1)
Find-Min 0(n) 0(1) 0(log H) 0(1)
t amortized
Accomplished. 0(1) Insert and Decrease-Key, 0(\ogn) Extract-Min.
Heaps of heaps
b-heaps. Fat heaps. 2-3 heaps. Leaf heaps. Thin heaps. Skew heaps. Splay heaps. Weak heaps. Leftist heaps. Quake heaps. Pairing heaps. Violation heaps. Run-relaxed heaps. Rank-pairing heaps. Skew-pairing heaps. Rank-relaxed heaps. Lazy Fibonacci heaps.
Disjoint-sets Data Structures
PEARSON
JON KLEINBERG • EVA TARDOS
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Lecture slides by Kevin Wayne Copyright © 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 11/22/17 6:04 AM
Disjoint-sets data type
Goal. Support three operations on a collection of disjoint sets.
• Make-Set(x): create a new set containing only element x.
• Find(x): return a canonical element in the set containing x.
• Union(x,)0: replace the sets containing x and y with their union.
Performance parameters.
• m = number of calls to Make-Set, Find, and Union.
• n = number of elements = number of calls to Make-Set.
Dynamic connectivity. Given an initially empty graph G, support three operations.
• Add-NodeO): add node u.
• Add-EdgeO, v): add an edge between nodes u and v.
• Is-ConnectedO, v): is there a path between u and v?
<- disjoint sets =
connected components
<- 1 Make-Set operation
<- 1 Union operation
<- 2 Find operations
Disjoint-sets data type: applications
Original motivation. Compiling Equivalence, Dimension, and Common statements in Fortran.
An Improved Equivalence Algorithm
Bernard A. Galler and Michael J. Fisher University of Michigan, Ann Arbor, Michigan
An algorithm for assigning storage on the basis of EQUIVALENCE, DIMENSION and COMMON declarations is presented. The algorithm is based on a tree structure, and has reduced computation time by 40 percent over a previously published algorithm by identifying all equivalence classes with one scan of the EQUIVALENCE declarations. The method is applicable in any problem in which it is necessary to identify equivalence classes, given the element pairs defining the equivalence relation.
Note. This 1 964 paper also introduced key data structure for problem.
3
Disjoint-sets data type: applications
Applications.
• Percolation.
• Kruskal's algorithm.
• Connected components.
• Computing LCAs in trees.
• Computing dominators in digraphs.
• Equivalence of finite state automata.
• Checking flow graphs for reducibility.
• Hoshen-Kopelman algorithm in physics.
• Hinley-Milner polymorphic type inference.
• Morphological attribute openings and closings.
• Matlab's Bw-Label function for image processing.
• Compiling Equivalence, Dimension and Common statements in Fortran.
4
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Disjoint-sets data structure
Parent-link representation. Represent each set as a tree of elements.
• Each element has an explicit parent pointer in the tree.
• The root serves as the canonical element (and points to itself)-
• Find(x): find the root of the tree containing x.
• Union(x,)0: merge trees containing x and y.
Union(3, 5)
Disjoint-sets data structure
Parent-link representation. Represent each set as a tree of elements.
• Each element has an explicit parent pointer in the tree.
• The root serves as the canonical element (and points to itself)-
• Find(x): find the root of the tree containing x.
• Union(x,)0: merge trees containing x and y.
Union(3, 5)
Disjoint-sets data structure
Array representation. Represent each set as a tree of elements.
• Allocate an array parent\\ Of length fl. <- must know number of elements n a priori
• parent[i] = j means parent of element i is element j.
0 l 2 3 4 5 6 7 8 9
parent[] 7 5 7 ® 8 7 5 7 8 8
parent of 3 is 8
root
Note. For brevity, we suppress arrows and self loops in figures.
NaTve linking
Naive linking. Link root of first tree to root of second tree. Union(5, 3)
9 43 0 5 2
9
NaTve linking
Naive linking. Link root of first tree to root of second tree.
Union(5, 3)
NaTve linking
Naive linking. Link root of first tree to root of second tree.
Make-Set(x) Union(x, y)
parent[x] ^- x. r <— FlND(x).
s <- FlND(y). parent\f\ <— s.
FlND(x)
While (x # parent[x\) x <— parent\x\.
Return x.
NaTve linking: analysis
Theorem. Using naive linking, a Union or Find operation can take S(n) time in the worst case, where n is the number of elements.
max number of links on any path from root to leaf node
Pf. /
• In the worst case, Find takes time proportional to the height of the tree.
• Height of the tree is n - 1 after the sequence of union operations:
UNI0N(1,2), UNI0N(2,3), UNION(w- 1,«).
height = 2
height = 3
height = n-1
0
T
0
©
12
Union-Find
► naive linking
► link-by-size
► link-by-ronk
► path compression
► link-by-rank with path compression
► context
Link-by-size
Link-by-size. Maintain a tree size (number of nodes) for each root node. Link root of smaller tree to root of larger tree (breaking ties arbitrarily).
Union(5, 3)
Link-by-size
Link-by-size. Maintain a tree size (number of nodes) for each root node. Link root of smaller tree to root of larger tree (breaking ties arbitrarily).
Link-by-size
Link-by-size. Maintain a tree size (number of nodes) for each root node. Link root of smaller tree to root of larger tree (breaking ties arbitrarily).
Make-Set(x)
parent[x] <— x. size[x] <— 1.
FlND(x)
While (x # parent[x\) x <— parent\x\.
Return x.
Union(x, y)
r <- FlND(x). s <- FtND(y).
If (r = s) Return. Else if (size[r] > size[s\)
parentis] <— r.
size\f\ <— size\f\ + size{s\. Else
parent\f\ <— s.
size[_s\ «— size\f\ + size{s\.
link-by-size
16
Link-by-size: analysis
Property. Using link-by-size, for every root node r : size[r] > 2heisht^r\ Pf. [ by induction on number of links ]
• Base case: singleton tree has size 1 and height 0.
• Inductive hypothesis: assume true after first i links.
• Tree rooted at r changes only when a smaller (or equal) size tree rooted at s is linked into r.
• Case 1 . [ height(r) > height(s) ] size'[r] > size\f[
> 2heiSht(r) <- inductive hypothesis
= ^ height'(r)
size = 8 (height = 2)
Link-by-size: analysis
Property. Using link-by-size, for every root node r : size[r] > 2heisht^r\ Pf. [ by induction on number of links ]
• Base case: singleton tree has size 1 and height 0.
• Inductive hypothesis: assume true after first i links.
• Tree rooted at r changes only when a smaller (or equal) size tree rooted at s is linked into r.
• Case 2. [height(r) < height(s) ] size'[r] = size[r] + size[s]
> 2 Size[s] <- link-by-size
> 2 • 2heiSht(s) <- inductive hypothesis
size = 6
(height = 1) = 2heisht(s)+l
o
18
Link-by-size: analysis
Theorem. Using link-by-size, any Union or Find operation takes 0(\ogn) time in the worst case, where n is the number of elements. Pf.
• The running time of each operation is bounded by the tree height.
• By the previous property, the height is < \\gn\. ■
t
lg n - log2 n
Note. The Union operation takes 0(1) time except for its two calls to Find.
A tight upper bound
Theorem. Using link-by-size, a tree with n nodes can have height = \gn. Pf.
• Arrange 2k- 1 calls to Union to form a binomial tree of order k.
• An orders binomial tree has 2k nodes and height k. ■
20
Section 5.1.4
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Link-by-rank
Link-by-rank. Maintain an integer rank for each node, initially 0. Link root of smaller rank to root of larger rank; if tie, increase rank of new root by 1.
Union(7, 3)
Note. For now, rank = height.
Link-by-rank
Link-by-rank. Maintain an integer rank for each node, initially 0. Link root of smaller rank to root of larger rank; if tie, increase rank of new root by 1.
Link-by-rank
Link-by-rank. Maintain an integer rank for each node, initially 0. Link root of smaller rank to root of larger rank; if tie, increase rank of new root by 1.
Make-Set(x)
Union(x, y)
parent[x] <— x. rank[x] <— 0.
FlND(x)
While (x # parent[x]) x <— parent\x\.
Return x.
r <- FlND(x). s <- FlND(j).
If (r = s) Return.
else if (rank[r] > rank[s])
parentis] <— r. else if (rank\f\ < rank[s\)
parent\f\ <— s.
Else
parent\f\ <— s.
rank[s] <— rank[s] + 1.
link-by-rank
24
Link-by-rank: properties
Property 1. If x is not a root node, then rank[x] < rank\parent[x\\.
Pf. A node of rank k is created only by linking two roots of rank k-l. ■
Property 2. If x is not a root node, then rank[x] will never change again. Pf. Rank changes only for roots; a nonroot never becomes a root. ■
Property 3. If parent[x] changes, then rank[parent[x]] strictly increases. Pf. The parent can change only for a root, so before linking parent[x] =x. After x is linked-by-rank to new root r we have rank[r] > rank[x]. ■
rank = 3
ran
Link-by-rank: properties
Property 4. Any root node of rank k has > 2k nodes in its tree. Pf. [ by induction on k]
• Base case: true for k = 0.
• Inductive hypothesis: assume true for k-l.
• A node of rank k is created only by linking two roots of rank k- 1.
• By inductive hypothesis, each subtree has >2k~l nodes => resulting tree has >2k nodes. ■
Property 5. The highest rank of a node is < |_lg^J-Pf. Immediate from Property 1 and Property 4. ■
rank = 2 (8 nodes)
O
26
Link-by-rank: properties
Property 6. For any integer k > 0, there are < n 12k nodes with rank k. Pf.
• Any root node of rank k has > 2k descendants. [Property 4]
• Any nonroot node of rank k has > 2k descendants because:
- it had this property just before it became a nonroot [Property 4]
- its rank doesn't change once it became a nonroot [Property 2]
- its set of descendants doesn't change once it became a nonroot
• Different nodes of rank k can't have common descendants. [Property 1 ] ■
rank = 4
rank = 0 (11 nodes) Q
27
Link-by-rank: analysis
Theorem. Using link-by-rank, any Union or Find operation takes 0(\ogn) time in the worst case, where n is the number of elements. Pf.
• The running time of Union and Find is bounded by the tree height.
• By Property 5, the height is < [\gn\. ■
Section 5.1.4
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
before path compression
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
33
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
34
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
35
Path compression
Path compression. When finding the root r of the tree containing x, change the parent pointer of all nodes along the path to point directly to r.
FlND(x)
If (x ^ parent\x\)
this Find implementation parent[x] «- FlND(parent[x]). <- changes the tree structure (!)
Return parent[x\.
Note. Path compression does not change the rank of a node; so height(x) < rank[x] but they are not necessarily equal.
36
Path compression
Fact. Path compression with naive linking can require Q(n)t\rr\e to perform a single Union or Find operation, where n is the number of elements.
Pf. The height of the tree is n - 1 after the sequence of union operations:
LlNI0N(l,2), LlNI0N(2,3), UNION(w- 1,«). ■
\
naive linking: link root of first tree to root of second tree
Theorem. [Tarjan-van Leeuwen 1984] Starting from an empty data structure, path compression with naive linking performs any intermixed sequence of m>n Make-Set, Union, and Find operations on a set of n elements in 0(m\ogn) time.
Pf. Nontrivial (but omitted).
37
Section 5.1.4
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Link-by-rank with path compression: properties
Property. The tree roots, node ranks, and elements within a tree are the same with or without path compression.
Pf. Path compression does not create new roots, change ranks, or move elements from one tree to another. ■
before path compression
after path compression
Link-by-rank with path compression: properties
Property. The tree roots, node ranks, and elements within a tree are the same with or without path compression.
Corollary. Property 2, 4-6 hold for link-by-rank with path compression.
Property 1. If x is not a root node, then rank[x] < rank{parent[x]].
Property 2. If x is not a root node, then rank[x] will never change again.
Property 3. If parent[x] changes, then rank[parent[x]] strictly increases.
Property 4. Any root node of rank k has > 2k nodes in its tree.
Property 5. The highest rank of a node is < |_lg^J-
Property 6. For any integer k > 0, there are < n 12k nodes with rank k.
Bottom line. Property 1-6 hold for link-by-rank with path compression, (but we need to recheck Property 1 and Property 3)
40
Link-by-rank with path compression: properties
Property 3. If parent[x] changes, then rank\parent[x\\ strictly increases. Pf. Path compression can make x point to only an ancestor of parent\x\.
Property 1. If x is not a root node, then rank[x] < rank[parent[x]]. Pf. Path compression doesn't change any ranks, but it can change parents. If parent[x] doesn't change during a path compression, the inequality continues to hold; if parent[x] changes, then rank\parent\x\\ strictly increases.
before path compression
after path compression
Iterated logarithm function
Def. The iterated logarithm function is defined by:
i * lg n
0 if n < 1
1 + lg* (lg n) otherwise
n lg* n
1 0
2 1
[3,4] 2
[5, 16] 3
[17, 65536] 4
[65537, 265536] 5
iterated lg function
Note. We have lg* n < 5 unless n exceeds the # atoms in the universe.
42
Analysis
Divide nonzero ranks into the following groups:
• { 1 }
• {2}
' {3,4}
• { 5,6,16 }
• { 17,18,...,216}
• { 65537,65538,..., 265536}
Property 7. Every nonzero rank falls within one of the first lg* n groups. Pf. The rank is between 0 and [\gn\. [Property 5]
Creative accounting
Credits. A node receives credits as soon as it ceases to be a root.
If its rank is in the interval { k + l,k+2,2k}, we give it 2k credits.
i-;-"
group k
Proposition. Number of credits disbursed to all nodes is < n \g*n. Pf.
• By Property 6, the number of nodes with rank > k + 1 is at most
n n n - + - + • • • < —
• Thus, nodes in group k need at most n credits in total.
• There are < lg*^ groups. [Property 7] ■
44
Running time of FIND
Running time of Find. Bounded by number of parent pointers followed.
• Recall: the rank strictly increases as you go up a tree. [Property 1]
• Case 0: parent[x] is a root => only happens for one link per Find.
• Case 1: rank[parent[x]] is in a higher group than rank[x].
• Case 2: rank[parent[x]] is in the same group as rank[x].
Case 1. At most \g*n nodes on path can be in a higher group. [Property 7]
Case 2. These nodes are charged 1 credit to follow parent pointer.
• Each time x pays 1 credit, rank{parent[x]] strictly increases. [Property 1]
• Therefore, if rank[x] is in the group { k+1, ...,2k}, the rank of its parent will be in a higher group before x pays 2k credits.
• Once rank[parent[x]] is in a higher group than rank[x], it remains so because:
- rank[x] does not change once it ceases to be a root. [Property 2]
- rank[parent[x]] does not decrease. [Property 3]
- thus, x has enough credits to pay until it becomes a Case 1 node. ■
Link-by-rank with path compression
Theorem. Starting from an empty data structure, link-by-rank with path compression performs any intermixed sequence of m >// Make-Set, Union, and Find operations on a set of n elements in 0(m\og*n) time.
46
Union-Find
► naive linking
► link-by-size
► link-by-rank
► path compression
► link-by-rank with path compression
► context
Link-by-size with path compression
Theorem. [Fischer 1 972] Starting from an empty data structure, link-by-size with path compression performs any intermixed sequence of m>n Make-Set, Union, and Find operations on a set of n elements in O(mloglogft) time.
MASSACHUSETTS INSTITUTE OF TECHNOLOGY A. (. LA&QRATQRY
Artificial Intelligence
flemo Ho. £56 April W7Z
EFFICIENCY OF EQUIVALENCE ALGORITHMS Kit,-.ael J. Fisi'ier
1, INTRODUCTION
The equivalence problem is to determine the Finest partition on a set that is consistent vith a sequence of assertions of the form "x i j". A strategy for doing this on a computer processes the assertions serially, maintaining always in storage a representation of the partition defined by the assertions so far encountered. To process the command "x = y", the equivalence classes of x and y are determined. If they are the same, nothing further is dans; otherwise the two classes are merged together.
48
Link-by-size with path compression
Theorem. [Hopcroft-Ullman 1 973] Starting from an empty data structure, link-by-size with path compression performs any intermixed sequence of m>n Make-Set, Union, and Find operations on a set of n elements in 0(m\og*ri) time.
siam j. Comput.
Vol. 2, No. 4, December 1973
SET MERGING ALGORITHMS*
J. E. HOPCROFTf and J. D. ULLMANJ
Abstract. This paper considers the problem of merging sets formed from a total of n items in such a way that at any time, the name of a set containing a given item can be ascertained. Two algorithms using different data structures are discussed. The execution times of both algorithms are bounded by a constant times nG(n), where G(n) is a function whose asymptotic growth rate is less than that of any finite number of logarithms of n.
Key words, algorithm, algorithmic analysis, computational complexity, data structure, equivalence algorithm, merging, property grammar, set, spanning tree
49
Link-by-size with path compression
Theorem. [Tarjan 1 975] Starting from an empty data structure,
link-by-size with path compression performs any intermixed sequence
of m>n Make-Set, Union, and Find operations on a set of n elements
in 0(ma(m,n)) time, where a(m,n) is a functional inverse of the Ackermann
function.
Efficiency of a Good But Not Linear Set Union Algorithm
ROBERT ENDRE TARJAN
University of California, Berkeley, California
abstract. Two types of instructions for manipulating a family of disjoint sets -which partition a universe of n elements are considered FIND (x) computes the name of the (unique) set containing element x UNION (A, B, C) combines sets A and B into a new set named C, A known algorithm for implementing sequences of these instructions is examined It is shown that, if t(m, n) is the maximum time required by a sequence of m > n FINDs and n — 1 intermixed UNIONb, then kima(m, n) < l{m, n) < Jc2ma(rw, n) for some positive constants fci and k2, where a{m, n) is related to a functional inverse of Ackermann's function and is very slow-growing.
50
Ackermann function
Ackermann function. [Ackermann 1 928] A computable function that is not primitive recursive.
n + 1 if rn — 0
A(m, n) — \ A(m — 1,1) if m > 0 and n — 0
A(m — 1, A(m, n — 1)) if m > 0 and n > 0
Zum Hilbertschen Anfbau der reellen Zahlen.
Von
Wilhelm Ackermann in Güttingen.
Um den Beweis für die von Cantor aufgestellte Vermutung zu erbringen, daß sich die Menge der reellen Zahlen, d. h. der zahlentheoretischen Funktionen, mit Hilfe der Zahlen der zweiten Zahlklasse auszählen läßt, benutzt Hilbert einen speziellen Aufbau der zahlentheoretischen Funktionen. Wesentlich bei diesem Aufbau ist der Begriff des Typs einer Funktion. Eine Funktion vom Typ 1 ist eine solche, deren Argumente und Werte ganze Zahlen sind, also eine gewöhnliche zahlentheoretische Funktion. Die Funktionen vom Typ 2 sind die Funktionenfunktionen. Eine derartige Funktion ordnet jeder zahlentheoretischen Funktion eine Zahl zu. Eine Funktion vom Typ 3 ordnet wieder den Funktionenfunktionen Zahlen zu, usw. Die Definition der Typen läßt sich auch ins Transfinite fortsetzen, für den Gegenstand dieser Arbeit ist das aber nicht von Belang1).
Note. There are many inequivalent definitions.
51
Ackermann function
Ackermann function. [Ackermann 1 928] A computable function that is not primitive recursive.
n + 1 if m — 0
A(m, n) — { A(m — 1,1) if m > 0 and n — 0
A(m — 1, A(m, n — 1)) if m > 0 and n > 0
Inverse Ackermann function.
a(m,n) = mm{i > 1 : A(i, \ m/n\) > log2 n}
" I am not smart enough to understand this easily. " — Raymond Seidel
52
Inverse Ackermann function
Definition.
Ex.
\n/2\ iffc = l
&k(n) — { 0 if n = 1 and k > 2
1 + afc(afc_i(n)) otherwise
ai(n) = \n I 2].
a2(n) = \\gn] = # of times we divide n by 2, until we reach 1.
a3(n) = lg*^ =# of times we apply the lg function to n, until we reach 1.
a4(n) = # of times we apply the iterated lg function to n, until we reach 1.
2 t 65536 = 2
65536 times
1 2 3 4 5 6 7 8 9 10 1 1 12 13 14 15 16 216 265536 2 t65536
ai(n) 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 215 265535 huge
0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 16 65536 2 T 65535
CL3(n) 0 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 5 65536
a4ji) 0 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4
53
Inverse Ackermann function
Definition. , ^ .ffc = 1
ttfc(n) = \ 0 if n = 1 and > 2
1 + afc(afc_i(n)) otherwise
Property. For every n>5, the sequence ai(«),a2(«),a3(«),... converges to 3.
Ex. [n = 9876!] ai(/i) > 1035163, a2(w) = 116812, a3(«) = 6, a4(w) = 4, a5(n) = 3.
One-parameter inverse Ackermann. a(ri) = min { k: a*(«) < 3 }. Ex. a(9876!) = 5.
Two-parameter inverse Ackermann. a(m, n) = min { k: a*(«) < 3 + m I n }.
54
A tight lower bound
Theorem. [Fredman-Saks 1 989] In the worst case, any cell-probe(log^) algorithm requires Q(ma(m,n)) time to perform an intermixed sequence of m Make-Set, Union, and Find operations on a set of n elements.
Cell-probe model. [Yao 1 981 ] Count only number of words of memory accessed; all other operations are free.
The Cell Probe Complexity of Dynamic Data Structures
Michael L. Fredman
Michael E. Saks
Bellcore and U.C. San Diego
U.C. San Diego, Bellcore and Rutgers University
Dynamic data structure problems involve the representation of data in memory in such a way as to permit certain types of modifications of the data (updates) and certain types of questions about the data (queries). This paradigm encompasses many fundamental problems in computer science.
1. Summary of Results
register size from logn to polylog(«) only reduces the time complexity by a constant factor. On the other hand, decreasing the register size from logn to 1 increases time complexity by a logrt factor for one of the problems we consider and only a loglogn factor for some other problems.
The purpose of this paper is to prove new lower and upper bounds on the time per operation to implement solutions to some familiar dynamic data structure problems including list representation, subset ranking, partial sums, and the set union problem . The main features of our lower bounds are:
The first two specific data structure problems for which we obtain bounds are:
n't Thev hold in the rp.ll nrfihp model of mmnnlalinn ÍA. Yan
List Representation. This problem concerns the represention of an ordered list of at most n (not necessarily distinct) elements from the universe U = {1, 2 n }. The operations to be supported are report(&), which returns the k'h element of the list, insfrt/t u) which inserts element u into the list between the
55
Path compaction variants
Path splitting. Make every node on path point to its grandparent.
before path splitting
56
Path compaction variants
Path halving. Make every other node on path point to its grandparent.
before path halving
Linking variants
Link-by-size. Number of nodes in tree. Link-by-rank. Rank of tree.
Link-by-random. Label each element with a random real number between 0.0 and 1.0. Link root with smaller label into root with larger label.
Disjoint-sets data structures
Theorem. [Tarjan-van Leeuwen 1984] Starting from an empty data structure, link-by- { size, rank } combined with { path compression, path splitting, path halving } performs any intermixed sequence of m >// Make-Set, Union, and Find operations on a set of n elements in 0(m a(m,n)) time.
Worst-Case Analysis of Set Union Algorithms
ROBERT E. TARJAN
AT&T Bell Laboratories, Murray Hill, New Jersey AND
JAN VAN LEEUWEN
University of Utrecht, Utrecht, The Netherlands
Abstract. This paper analyzes the asymptotic worst-case running time of a number of variants of the well-known method of path compression for maintaining a collection of disjoint sets under union. We show that two one-pass methods proposed by van Leeuwen and van der Weide are asymptotically optimal, whereas several other methods, including one proposed by Rem and advocated by Dijkstra, are slower than the best methods.
59
Part III
Algorithm Design Techniques
20
Algorithmic paradigms
Greed. Build up a solution incrementally, myopically optimizing some local criterion.
Divide-and-conquer. Break up a problem into independent subproblems; solve each subproblem; combine solutions to subproblems to form solution to original problem.
Dynamic programming. Break up a problem into a series of overlapping subproblems\combine solutions to smaller subproblems to form solution to large subproblem.
fancy name for caching intermediate results in a table for later reuse
2
Peak Finding
Closest Pair of Points Dynamic Programming
Interval Scheduling
Parenthesization Problem
Knapsack Problem
Sequence Alignement
Bellman-Ford Algorithm
Greedy Algorithms
Coin Changing
Interval Scheduling
Interval Partitioning
Scheduling to Minimize Lateness
Optimal Caching
Dijkstra's algorithm
Minimum Spanning Trees
Divide and Conquer
DIVIDE AND CONQUER
Nothing is particularly hard if you divide it into small jobs.
Henry Ford
22
Divide-and-conquer paradigm
Divide-and-conquer.
• Divide up problem into several subproblems (of the same kind).
• Solve (conquer) each subproblem recursively.
• Combine solutions to subproblems into overall solution.
Most common usage.
• Divide problem of size n into two subproblems of size nil. <-0{n) time
• Solve (conquer) two subproblems recursively.
• Combine two solutions into overall solution. <-0(«)time
Consequence.
• Brute force: @(n2).
• Divide-and-conquer: 0(n\ogn).
attributed to Julius Caesar
Divide and Conquer
Maximal and minimal elements
NAIVE ALGORITHM
complexity: number of comparisons
Algorithm: Iterative MaxMin
Input: sequence S[l... n]
Output: maximal and minimal element
1 max <- S[l]; min <- S[l]
2 for I: ^r- 2 to n do
3
4
if S[i] > max then max <— S[i] if S[i] < min then min ^- S[i]
5 return max, min
2(r? — 1) comparisons
23
DIVIDE AND CONQUER ALGORITHM
divide the sequence into two equally sizek subsequences
solve find maximal and minimal elements of both subsequences
combine greater of the maximal elements is the maximal element of the whole sequence(t/?e same for the minimal element)
Algorithm: MaxMin
Input: sequence S[l... n], indices x,y
Output: maximal and minimal element of S[x... y]
1 if y = x then return (S[x], 5[x])
2 if y = x + 1 then return (max(S[x], S[y]), min(S[x], S[y]))
3 if y > x + 1 then
4 (/i,/2) <- MaxMin(S,x, L(x + y)/2j)
5 |_ (ri,rs) ^ MaxMin(S, |_(x+ y)/2j + l,y)
6 return (max(/i, ri), min(/2, r2)) 24
correctness induction w.r.t. the length of the sequence
complexity
T(n) =
1 for n = 2
TY>/2J)+ T(\n/2]) + 2 for n > 2
by induction w.r.t. n we can check that 7~(n) < |n — 2 n = 2 7(2) = landl<§-2-2
n > 2 assumption: the inequality is true for all /, 2 < / < n let us prove the inequality for n
T(n)=T([n/2\)+T([n/2]) + 2
5 n 5
< - n 2 - 2 + - n 2
3 1 3 1
5 3
Divide and Conquer
Peak Finding
6.006
Introduction to Algorithms
ALGORITHMS
Lecture 2: Peak Finding
Prof. Erik Demaine
ID Peak Finding
Given an array A [0.. n — 1]:
A:-
00
-0(3
0 1 2 3 4 5 6
A[i] is a peak if it is not smaller than its neighbor (s):
A[i-1] i4[i + l] where we imagine
A[-l] =A[n] = -co
Goal: Find any peak
"Brüte Force" Algorithm
Test all elements for peakyness
for i in range (n):
ifA[i-l] A[i + l]: return i
A:
0 1 2 3 4 5 6
Algorithm IV2
max 04)
— Global maximum is a local maximum
A:
m = 0
for i in range(l, n):
if ;![£] >i4[m]:lQ(ö
m = i
return m
0 1 2 3 4 5 6
Cleverer Idea
A e*a(
A
1 \ \ o
Look at any element ^4[i] and its neighbors A[i — l]&A[i + 1]
— If peak: return i
— Otherwise: locally rising on some side .
• Must be a peak in that direction \^
• So can throw away rest of array, ns;,2? wtyf leaving A [: i] or A [i + 1: ]
A:
Sx
J
0 1 2 3 4 5 6
Where to Sample?
• Want to minimize the worst-case remaining elements in array
- Balance A[: i] of length i
with A[i + 1:] of length n — i — 1
- i = n — i — 1
- i = (n — l)/2: middle element
- Reduce n to (n — l)/2
A:
0 1 2 3 4 5 6
Algorithm
peakld(i4, i,f):
m = L(i+;)/2J
if A[m - 1] < A[m] > A[m + 1]:
return m elif A[m - 1] > j4[m]:
return peakld(y4, i,m — 1) elif A[m] < A[m + 1]:
return peakld(y4,m +
i4
0 1 2 3 4 5 6
Divide & Conquer
• General design technique:
1. Divide input into part(s]
2. Conquer each part recursively
3. Combine result(s] to solve original problem
• ID peak:
1. One half
2. Recurse
3. Return
Divide & Conquer Analysis
• Recurrence for time T(ri) taken by problem size n
1. Divide input into part(s]:
W-li n2i ■■■» ^fe
2. Conquer each part recursively
3. Combine result(s]
to solve original problem
T(n) =
divide cost +
rCrii) + T(n2) + - +T(nk)
+ combine cost
ID Peak Finding Analysis
• Divide problem into 1 problem of size ~ ^
• Divide cost: 0(1)
• Combine cost: 0(1)
• Recurrence:
Hn)= tQ + 0(1)
Solving Recurrence
S*
7(n
T(n
T(n
T(n T(n
+ c
= T -) + c + c
+ c + c + c
= rGfe) + clgn
= 7(1) + clgn = 0(lgn)
2D Peak Finding
• Given n x n matrix of numbers
• Want an entry not smaller than its (up to] 4 neighbors:
mSM mim
In
km Km]
Divide & Conquer #0
Looking at center element doesn't split the problem into pieces...
Divide & Conquer #Vi
• Consider max element in each column
• ID algorithm would solve max array in 0 (lg n) time
• But 0(n2) time to compute max array
u u
Divide & Conquer #1
• Look at center column
• Find global max within
• If peak: return it
• Else:
- Larger left/right neighbor
- Larger max in that column
- Recurse in left/right half
• Base case: 1 column
- Return global max within
3 5
7 2 5
8 9 8
6 3
9 0 6
8 9 8
w 1 2
9 9 9 3 5 9 8
Analysis #1
• 0 (ri) time to find max in column
• O(lgn) iterations (like binary search]
• 0 (n lg n) time total
WW 5
7 2 5
9 8 9 8
WW 3
9 0 6 Km]
8 9 8
2
• Can we do better?
Divide & Conquer #2
• Look at boundary, center row, and center column (window]
• Find global max within
• If it's a peak: return it
• Else:
- Find larger neighbor
- Can't be in window
- Recurse in quadrant, including green boundary
f9 0 6W4
0 8 9 8 0 5
00000000
Correctness
• Lemma: If you enter a quadrant, it contains a peak of the overall array [climb up]
• Invariant: Maximum element of window never decreases as we descend in recursion
• Theorem: Peak in visited quadrant is also peak in overall array
Analysis #2
• Reduce nxn matrix to
~ - x - submatrix in
2 2
n) time [|window|]
0 T(n
T(n
T(n
T(n
+ cn
n
+ c- + cn
2
= T (-) + c- + c- + c n
\8j 4 2
= 7(1) + c(l + 2 + 4 + - + ^ + | + n)
Divide and Conquer
Closest Pair of Points
5. Divide and Conquer
► mergesort
► counting inversions
► randomized quicksort
► median and selection
► closest pair of points
Section 5.4
Closest pair of points
Closest pair problem. Given n points in the plane, find a pair of points with the smallest Euclidean distance between them.
Fundamental geometric primitive.
• Graphics, computer vision, geographic information systems, molecular modeling, air traffic control.
• Special case of nearest neighbor, Euclidean MST, Voronoi.
fast closest pair inspired fast algorithms for these problems
Closest pair of points
Closest pair problem. Given n points in the plane, find a pair of points with the smallest Euclidean distance between them.
Brute force. Check all pairs with G(n2) distance calculations.
1 D version. Easy 0(n log n) algorithm if points are on a line.
Non-degeneracy assumption. No two points have the same x-coordinate.
Closest pair of points: first attempt
Sorting solution.
• Sort by x-coordinate and consider nearby points.
• Sort by y-coordinate and consider nearby points.
Closest pair of points: first attempt
Sorting solution.
• Sort by x-coordinate and consider nearby points.
• Sort by y-coordinate and consider nearby points.
Closest pair of points: second attempt
Divide. Subdivide region into 4 quadrants.
Closest pair of points: second attempt
Divide. Subdivide region into 4 quadrants.
Obstacle. Impossible to ensure n/4 points in each piece.
Closest pair of points: divide-and-conquer algorithm
Divide: draw vertical line L so that nil points on each side. Conquer: find closest pair in each side recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions. N>
seems like 0(«2)
o o • L o ° •
o ° • o o o • «• / 21 8 ^ •
1^ ° ° o 0 0 o
o o • o
• •
70
How to find closest pair with one point in each side?
Find closest pair with one point in each side, assuming that distance < 6. • Observation: suffices to consider only those points within 6 of line L.
How to find closest pair with one point in each side?
Find closest pair with one point in each side, assuming that distance < 6.
• Observation: suffices to consider only those points within 6 of line L.
• Sort points in 26-strip by their y-coordinate.
• Check distances of only those points within 7 positions in sorted list!
\ „
whv?
• L •
• o
• o © ° o ° © •
12 o © 8 = min(12,21)
• ° o
• o © o •
i-1 6
How to find closest pair with one point in each side?
Def. Let st be the point in the 26-strip, with the ith smallest y-coordinate.
Claim. If\j-i\ > 7, then the distance between 5, and 5,- is at least 6.
Pf.
Consider the 26-by-6 rectangle R in strip whose min y-coordinate is y-coordinate of st Distance between st and any point Sj above R is > 6.
diameter is
Subdivide R into 8 squares. j/ V2 < s
At most 1 point per square. At most 7 other points can be in R.
\
constant can be improved with more refined geometric packing argument
R r
Closest pair of points: divide-and-conquer algorithm
closest-pair(pi,/?2, ...,pn)
Compute vertical line L such that half the points <- 0(n)
are on each side of the line.
61 «- closest-pair(points in left half). <- T(n I 2)
62 «- closest-pair(points in right half). <- Tin I 2)
6 <- min { 61 , 62 }.
Delete all points further than 6 from line L. *- 0(n)
Sort remaining points by _y-coordinate. +- 0(n log n)
Scan points in y-order and compare distance between
each point and next 7 neighbors. If any of these <- 0(ri)
distances is less than 6, update 6.
Return 6.
74
Divide-and-conquer: quiz 6
T(n) =
9(1) ifn = l
T([n/2\) + T([n/2]) + B(nlogn) if n > 1
A. 7T[/i) = 0(«).
B. r(n) = 0(« log n).
C. T(n) = G(n\og2n).
D. T(n) = <3>(n2).
I>
What is the solution to the following recurrence?
75
Refined version of closest-pair algorithm
Q. How to improve to 0(n log n) ?
A. Don't sort points in strip from scratch each time.
• Each recursive call returns two lists: all points sorted by x-coordinate, and all points sorted by y-coordinate.
• Sort by merging two pre-sorted lists.
Theorem. [Shamos 1 975] The divide-and-conquer algorithm for finding a closest pair of points in the plane can be implemented in 0(n log n) time.
Pf.
T{\n/2\) + T(\n/2\) + 9(n) if n > 1
if n
1
Illustrated Encyclopedia of
BILLIARDS
Divide-cmd-conquer: quiz 7
What is the complexity of the 2D closest pair problem?
A. 0(«).
B. 0.
• Two jobs are compatible if they don't overlap.
• Goal: find max-weight subset of mutually compatible jobs.
Earliest-finish-time first algorithm
Earliest finish-time first.
• Consider jobs in ascending order of finish time.
• Add job to subset if it is compatible with previously chosen jobs.
Recall. Greedy algorithm is correct if all weights are 1.
Observation. Greedy algorithm fails spectacularly for weighted version.
Weighted interval scheduling
Convention. Jobs are in ascending order of finish time: fx< f2 < ... time
0 1 2 3 4 5 6 7 8 91011
Dynamic programming: binary choice
Def. OPT(j) = max weight of any subset of mutually compatible jobs for subproblem consisting only of jobs 1,2, ...J.
Goal. OPT(n) = max weight of any subset of mutually compatible jobs.
Case 1. OPT(j) does not select job j.
• Must be an optimal solution to problem consisting of remaining jobs 1,2,j- 1.
optimal substructure property
Case 2. OPT(j) Selects job j. ^ (proof via exchange argument)
• Collect profit wj.
• Can't use incompatible jobs {p(J) + l,p(j) + 2> j- 1 }■
• Must include optimal solution to problem consisting of remaining compatible jobs 1,2,..., p(j).
Bellman equation.
max { OPT(j - 1), Wj + OPT(p(j)) } if j > 0
Weighted interval scheduling: brute force
Brute-Force(n,si, ...,sn,fi, ...,fn,wi, ...,wn)
Sort jobs by finish time and renumber so that f\ < fi < ... < fn. Compute p[l],p[2], ...,p[n] via binary search.
Return Compute-Opt(^).
Compute-Opt(7)
If (/ = 0)
return 0. Else
Return max{CoiviPUTE-OPT(y-l), wj + Compute-Opt(/?[7]) }.
Dynamic programming: quiz 1
What is running time of Compute-Opt(w) in the worst case?
A. S(n log n)
B. S(n2)
C. 0(1.618")
D. 0(2W)
compute-opt(y)
IF (/ = 0)
return 0.
Else
Return max{CoMPUTE-OPT(y-l), vv/ + Compute-Opt(/?|j]) }.
Weighted interval scheduling: brute force
Observation. Recursive algorithm is spectacularly slow because of overlapping subproblems => exponential-time algorithm.
Ex. Number of recursive calls for family of "layered" instances grows like Fibonacci sequence.
recursion tree
13
Weighted interval scheduling: memoization
Top-down dynamic programming (memoization).
• Cache result of subproblem j in M[/j.
• Use M[j] to avoid solving subproblem j more than once.
TOP-DOWN(«,5l, ...,5n,/l, ...,/n,Wl, ...,Wn)
Sort jobs by finish time and renumber so that f\ < fz < ... < fn. Compute p[l],p[2], ...,p\n\ via binary search.
M[0] <- 0. <- 9|obal arrav
Return M-Compute-Opt(^).
M-COMPUTE-OPT(y)
IF (M[/J is uninitialized)
M[j] <- max { M-COMPUTE-OPT (J- 1), Wj + M-COMPUTE-OPT(/7[/]) }. RETURN M[/j.
14
Weighted interval scheduling: running time
Claim. Memoized version of algorithm takes 0(n\ogn) time. Pf.
• Sort by finish time: 0(n\ogn) via mergesort.
• Compute p[j] for each j : 0(n log n) via binary search.
• M-Compute-Opt(/): each invocation takes 0(1) time and either
- (1) returns an initialized value M[y]
- (2) initializes M[j] and makes two recursive calls
• Progress measure = # initialized entries among Mfl..«].
- initially 0 = 0; throughout O < ^z.
- (2) increases O by 1 => < 2^ recursive calls.
• Overall running time of M-Compute-Opt(^) is 0(n). ■
1 5
Those who cannot remember the past are condemned to repeat it.
- Dynamic Programming
Weighted interval scheduling: finding a solution
Q. DP algorithm computes optimal value. How to find optimal solution? A. Make a second pass by calling Find-Solution^).
Find-Solution(/)
If (/ = 0)
Return 0.
else if (wj +M[p{j]] > M[y-1])
Return {y}U Find-Solution(p[/]). Else
Return Find-Solution(j-1).
M\j] = max{M{j-l],Wj+M[p[j]] }. Analysis. # of recursive calls 0(n).
Weighted interval scheduling: bottom-up dynamic programming
Bottom-up dynamic programming. Unwind recursion.
bottom-up(«, 5l, ..., Sn,fl, • • • ,/n, Wl, ..., Wn)
Sort jobs by finish time and renumber so that f\ < fz < ... < fi
Compute p[l],p[2], ...,p[n].
M[0] 0. previously computed values
For j= 1 to n
- max{M[y-l], wj + M[p{j]] }.
Running time. The bottom-up version takes O(^log^) time.
i
Dynamic Programming
Parenthesization Problem
PARENTHESIZATION PROBLEM
• given sequence of matrices (/4i,..., An) of dimension
PO X Pi.Pi X P2, • • . , Pn-1 X Pn
• compute associative product A\ • A2 - ... - An using sequence of normal matrix multiplies in the order that minimizes cost
• cost to multiply / x j with j x k is ijk
27
Parenthesization Example
NUMBER OF PARENTHESIZATIONS
denote the number of alternative parenthesizations if a sequence of n matrices by P(n)
P(n) =
1 pro n — 1
P(k) ■ P(n -k) pro n > 1
the solution to the recurrence is Q(2n)
brute force algorithm is exponentia
28
STRUCTURE OF AN OPTIMAL PARENTHESIZATION
to compute the product A\ • A'+i • • • • • Aj we have first for an index k compute products A; - ... - A^ and A^+i - ... - Aj
Q: which index k ?
A: we have to examine all possibilities
Q: how to compute products A; - ... - Ak and A^+i • ... • Ajl A: in an optimal way =4> subproblems of the original problem
29
COST OF AN OPTIMAL SOLUTION
• given matrices (Aj.,... ,An) of dimension
PO x Pi,Pi x P2, . . . , Pn-1 x pn
let us define a function m : {1,..., n} x {1,..., n} —> N where m(ij) is the minimal cost to multiply A\ • A'+i • ... • Aj
• we can define m(ij') recursively as follows
y min/
0 and a constant d
n-l n-1
T(n) = 7(/c) + T(n - /c)) + cfn = 2 ^ 7(/c) + cfn
k=l k=l
. T(n) = e(3n)
31
COMPUTING THE COST FUNCTION BOTTOM UP
• make use of dependencies
• the order is given by the number of matrices
• m(l, 1), m(2, 2),..., m(r?, n) m(l, 2), m(2, 3)..., m(n — 1, n) rrc(l, 3), m(2,4)..., m(n — 2, n)
m(l, A7 — 1), m(2, r?)
A77(15A?)
COMPUTING THE COST FUNCTION BOTTOM UP
Algorithm: Matrix Multiplication
Input: dimensions po, pi, P2, • • •, pn of matrices Output: value m(l,r?)
1 for / = 1 to n do /) <- 0
2 for r = 2 to r? do
3
4
5
6
7
8
for / = 1 to n — r + 1 do
j <- i + r-1 M{iJ) 0 and weighs wi >0.
• Knapsack has weight capacity of w.
Assumption. All input values are integral.
Ex. { 1,2,5 } has value $35 (and weight 1 0). Ex. { 3,4} has value $40 (and weight 1 1).
1 Vi Wi
1 $1 1kg
2 $6 2 kg
3 $18 5 kg
4 $22 6 kg
5 $28 7 kg
Creative Commons Attribution-Share Alike 2.5 by Dake
knapsack instance (weight limit W = 11)
31
Dynamic programming: adding a new variable
Def. OPT(i,w) = max-profit subset of items 1, with weight limit w. Goal. OPT(n,W).
^ possibly because wr > w
Case 1. OPT(i,w) does not select item i.
• OPT(i,w) selects best of { 1,2,1 } using weight limit w.
Case 2. OPT(i,w) Selects item i. ^ optimal substructure property
S (proof via exchange argument)
• Collect value v*. *
• New weight limit = w-uv.
• OPT(i,w) selects best of { 1,2,1 } using this new weight limit.
Bellman equation.
' 0 if i = 0
OPT{i,w) = I OPT(i-l,w) \iwi>w
max{ OPT(i — 1, w), Vi + OPT(i — l,w — Wi) } otherwise
Knapsack problem: bottom-up dynamic programming
Knapsack^, W,wi, ...,wn,vi, ...,vn)
For w = 0toW
M[0,w] «-0.
For i = 1 to n For w = 0 to w If (wt>w) M[i,w] Else M[/,w]
previously computed values
M[i-l,w]. max { M[i-l, w], vt +M[i-l, w- wt] }.
Return M[n, W]
0
if i = 0
OPT(i,w) = < OPT(i-l,w)
if Wj > w
[ max{ OPT(i — l,w), Vi + OPT(i — l,w — wi) } otherwise
33
Knapsack problem: bottom-up dynamic programming demo
l Vi Wi
1 $1 1kg
2 $6 2 kg
3 $18 5 kg
4 $22 6 kg
5 $28 7 kg
subset of items 1, .... i
0
if i = 0
OPT(i,w) = < OPT(i-l,w) if Wi>w
max {OPT(i — 1, w), Ví + OPT(i — 1, w — Wi} otherwise
weight limit w
0 1 2 3 4 5 6 7 8 9 10
{ } 0 t 0 t 0 + 0 0 0 0 0 0 0 0 0 0 0
{1} 1 1 1 1 1 1 1 1 1 1 1
{1,2} 6 6 7 7 7 7 7 7 7 7 7
{ 1, 2, 3 } 0 1 7 - 18 ^ 24 25 25 25 25
{ 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 ~~2T~- -40 1
{ 1, 2, 3, 4, 5 } 0 1 6 7 7 18 22 28 29 34 35
OPT(i, w) = max-profit subset of items 1, i with weight limit w.
34
Knapsack problem: running time
Theorem. The DP algorithm solves the knapsack problem with n items and maximum weight W in S(n W) time and S(n W) space. Pf ^ ,
1 1 ■ weights are integers
• Takes 0(1) time per table entry. between 1 and w
• There are G(n W) table entries.
• After computing optimal values, can trace back to find solution: OPT(i, w) takes item i iff M [i, w] > M [i - 1, w]. ■
35
Dynamic programming: quiz 4 Does there exist a poly-time algorithm for the knapsack problem?
A. Yes, because the DP algorithm takes G(n W) time.
B. No, because G(n W) is not a polynomial function of the input size.
C. No, because the problem is NP-hard.
D. Unknown.
36
Dynamic Programming
Sequence Alignement
6. Dynamic Programming II
► sequence alignment
► Hirschberg's algorithm
► Bellman-Ford-Moore algorithm
► distance-vector protocols
► negative cycles
Section 6.6
String similarity
Q. How similar are two strings? Ex. ocurrance and occurrence.
0 c u r r a n c e — 0 c — u r r a nee
0 c r r r nee
c u r e n c e 0 c c u e
6 mismatches, 1 gap 1 mismatch, 1 gap
0 mismatches, 3 gaps
Edit distance
Edit distance. [Levenshtein 1966, Needleman-Wunsch 1970]
• Gap penalty 6; mismatch penalty apq.
• Cost = sum of gap and mismatch penalties.
T
G A C
A C G
C T G A C
A C G
COSt = 8 + CXrr + (X
CG WTA
assuming aAA = acc = aGG = axx = 0
Applications. Bioinformatics, spell correction, machine translation, speech recognition, information extraction, ...
Spokesperson confirms senior government adviser was found
Spokesperson said the senior adviser was found
BLOSUM matrix for proteins
A R N D C Q E G H 1 L K M F P S T W Y V
A 7 -3 -3 -3 -1 -2 -2 0 -3 -3 -3 -1 -2 -4 -1 2 0 -5 -4 -1
R -3 9 -1 -3 -6 1 -1 -4 0 -5 -4 3 -3 -5 -3 -2 -2 -5 -4 -4
N -3 -1 9 2 -5 0 -1 -1 1 -6 -6 0 -4 -6 -4 1 0 -7 -4 -5
D -3 -3 2 10 -7 -1 2 -3 -2 -7 -7 -2 -6 -6 -3 -1 -2 -8 -6 -6
C -1 -6 -5 -7 13 -5 -7 -6 -7 -2 -3 -6 -3 -4 -6 -2 -2 -5 -5 -2
Q -2 1 0 -1 -5 9 3 -4 1 -5 -4 2 -1 -5 -3 -1 -1 -4 -3 -4
E -2 -1 -1 2 -7 3 8 -4 0 -6 -6 1 -4 -6 -2 -1 -2 -6 -5 -4
G 0 -4 -1 -3 -6 -4 -4 9 -4 -7 -7 -3 -5 -6 -5 -1 -3 -6 -6 -6
H -3 0 1 -2 -7 1 0 -4 12 -6 -5 -1 -4 -2 -4 -2 -3 -4 3 -5
1 -3 -5 -6 -7 -2 -5 -6 -7 -6 7 2 -5 2 -1 -5 -4 -2 -5 -3 4
L -3 -4 -6 -7 -3 -4 -6 -7 -5 2 6 -4 3 0 -5 -4 -3 -4 ■2 1
K -1 3 0 -2 -6 2 1 -3 -1 -5 -4 8 -3 -5 -2 -1 -1 -6 -4 -4
M -2 -3 -4 -6 -3 -1 -4 -5 -4 2 3 -3 9 0 -4 -3 -1 -3 -3 1
F -4 -5 -6 -6 -4 -5 -6 -6 -2 -1 0 -5 0 10 -6 -4 -4 0 4 -2
P -1 -3 -4 -3 -6 -3 -2 -5 -4 -5 -5 -2 -4 6 12 -2 -3 -7 -6 -4
S 2 -2 1 -1 -2 -1 -1 -1 -2 -4 -4 -1 -3 -4 -2 7 2 -6 -3 -3
T 0 -2 0 -2 -2 -1 -2 -3 -3 -2 -3 -1 -1 -4 -3 2 8 -5 -3 0
W -5 -5 -7 -8 -5 -4 -6 -6 -4 -5 -4 -6 -3 0 -7 -6 -5 16 3 -5
Y -4 -4 -4 -6 -5 -3 -5 -6 3 -3 -2 -4 -3 4 -6 -3 -3 3 11 -3
V -1 -4 -5 -6 -2 -4 -4 -6 -5 4 1 -4 1 -2 -4 -3 0 -5 -3 7
5
Dynamic programming: quiz 1
A. 1
B. 2
C. 3
D. 4
E. 5
What is edit distance between these two strings?
PALETTE PALATE
Assume gap penalty = 2 and mismatch penalty = 1.
6
Sequence alignment
Goal. Given two strings xxx2 ...xm and yxy2 ...yn, find a min-cost alignment.
Def. An alignment M is a set of ordered pairs xt-yj such that each character appears in at most one pair and no crossings.
Xi-yj and Xi'-yy cross if i< i', but j>j'
Def. The cost of an alignment M is:
COSt(M) = 2 <**iyj + 2 6 + 2 6
(xt,y j) £ M i: xt unmatched j': yj unmatched
v-v-1 v-v-1
mismatch gap
Xi X2 X3 X4 X5
X6
C T A c c G
T A c G
yi
an alignment of CTACCG and TACATG
M={ x2-yl,x3-y2,x4-y3,x^y4,x6-y6}
Sequence alignment: problem structure
Def. OPTiiJ) = min cost of aligning prefix strings xxx2 ...xt and yly2 ...yjm Goal. OPT(m,n).
Case 1. OPTQJ) matches xt-yj.
Pay mismatch for xi-yi + min cost of aligning xlx2 ...xt_x and y1y2 ...yhl.
Case 2a. OPT(iJ) leaves xt unmatched. Pay gap for x( + min cost of aligning xx x2
Case 2b. OPTQJ) leaves yj unmatched. Pay gap for yj + min cost of aligning xx x2
\ /
xt and y1 y2 ... yhl.
optimal substructure property (proof via exchange argument)
Bellman equation.
id
OPT(i,j) = {
mm <
dxiVj + OPT(i - 1, j
5 + OPT(i-l,j) 5 + OPT(iJ - 1)
1)
if i = 0 if j = 0
otherwise
Sequence alignment: analysis
Theorem. The DP algorithm computes the edit distance (and an optimal alignment) of two strings of lengths m and n in G(mn) time and space. Pf.
• Algorithm computes edit distance.
• Can trace back to extract optimal alignment itself. ■
Theorem. [Backurs-lndyk 201 5] If can compute edit distance of two strings of length n in 0(n2~e) time for some constant e > 0, then can solve SAT with n variables and m clauses in poly(m) time for some constant 6 > 0.
\
which would disprove SETH
Edit Distance Cannot Be Computed (strong exponential time hypothesis)
in Strongly Subquadratic Time [unless SETH is false)*
Arturs Backurs^ Piotr Indyk*
MIT MIT
1 1
Sequence alignment: traceback
SIMILARITY
— 2 4 6 8 10 12 14 16 18 20
1 2 4 1 ^ \ 3 ^1 2 t 4 6 4 6 8 7 9 1 1
D 4 6 3 3 4 k 8 6 8 9 9 1 1
E 6 8 5 5 6 8 10 1 1 1 1
N 8 10 7 7 8 8 10 12 13
T 10 12 9 9 9 10 10 *10 10 12 9 9 1 1
1 12 14 8 10 8 10 12 1 1 \ 8 10 1 1
T 14 16 10 10 10 10 12 14 1 1 1 1
Y 16 18 12 12 12 12 12 14 13
Sequence alignment: analysis
Theorem. The DP algorithm computes the edit distance (and an optimal alignment) of two strings of lengths m and n in G(mn) time and space. Pf.
• Algorithm computes edit distance.
• Can trace back to extract optimal alignment itself. ■
Theorem. [Backurs-lndyk 201 5] If can compute edit distance of two strings of length n in 0(n2~e) time for some constant e > 0, then can solve SAT with n variables and m clauses in poly(m) time for some constant 6 > 0.
\
which would disprove SETH
Edit Distance Cannot Be Computed (strong exponential time hypothesis)
in Strongly Subquadratic Time [unless SETH is false)*
Arturs Backurs^ Piotr Indyk*
MIT MIT
1 1
Dynamic programming: quiz 3
It is easy to modify the DP algorithm for edit distance to...
A. Compute edit distance in OQnri) time and 0(m + n) space.
B. Compute an optimal alignment in 0(mri) time and 0(m + n) space. C Both A and B.
D. Neither A nor B.
if 2 = 0
iS
if j = 0
OPT(i,j)
<
' aXiV. + OPT(i
mint 5 + OPT(i - 1, j) ^ S + OPT(iJ - 1)
otherwise
12
Dynamic Programming
Shortest Paths — Bellman-Ford Algorithm
Shortest paths with negative weights
Shortest-path problem. Given a digraph G = (V,E), with arbitrary edge lengths lvw, find shortest path from source node s to destination node t.
/
assume there exists a path from every node to t
length of shortest path from stot = 9- 3- 6+ 11 = 11
32
Shortest paths with negative weights: failed attempts
Dijkstra. May not produce shortest paths when edge lengths are negative.
Dijkstra selects the vertices in the order s, t, w, v But shortest path from s to t is s—n>—>w—
Reweighting. Adding a constant to every edge length does not necessarily make Dijkstra's algorithm produce shortest paths.
—>m
Adding 8 to each edge weight changes the q shortest path from s—n>—>w—to s->t.
Negative cycles
Def. A negative cycle is a directed cycle for which the sum of its edge lengths is negative.
a negative cycle W : £{W) = ^ £e < 0
34
Shortest paths and negative cycles
Lemma 1. If some v-+t path contains a negative cycle, then there does not exist a shortest v^t path.
Pf. If there exists such a cycle W, then can build a v-+t path of arbitrarily negative length by detouring around Was many times as desired. ■
£(W) < 0
Shortest paths and negative cycles
Lemma 2. If G has no negative cycles, then there exists a shortest v-*t path that is simple (and has < n - 1 edges).
Pf.
• Among all shortest v-+t paths, consider one that uses the fewest edges.
• If that path P contains a directed cycle W, can remove the portion of P corresponding to W without increasing its length. ■
liW) > o
Shortest-paths and negative-cycle problems
Single-destination shortest-paths problem. Given a digraph G = (V,E) with edge lengths lvw (but no negative cycles) and a distinguished node t, find a shortest v-*t path for every node v.
Negative-cycle problem. Given a digraph G = (V,E) with edge lengths lvw, find a negative cycle (if one exists).
shortest-paths tree negative cycle
37
Dynamic programming: quiz 5
Which subproblems to find shortest v^t paths for every node v?
A. OPT(i,v) = length of shortest v^t path that uses exactly i edges.
B. OPT(i,v) = length of shortest v^t path that uses at most edges.
C. Neither A nor B.
38
Shortest paths with negative weights: dynamic programming
Def. OPT(i, v) = length of shortest v-+t path that uses < i edges
Coal. OPTin-l,v) for each v.
by Lemma 2, if no negative cycles, there exists a shortest v~>? path that is simple
Case 1. Shortest v-+t path uses < i- 1 edges.
• 0P7Xi,v) = OPT(i-l,v).
optimal substructure property ^/ (proof via exchange argument)
Case 2. Shortest v-*t path uses exactly i edges.
• if (v, w) is first edge in shortest such v^t path, incur a cost of I
• Then, select best w^t path using 0
Shortest paths with negative weights: implementation
Shortest-Paths(V; E, I, t)
foreach node v G V:
M[0,v] «- oo. M[0,i] ^0.
For i = 1 to n - 1
foreach node v E V7: M[/,v] <-M[/-l,v]. foreach edge (v, w) E: E :
M[i,v] <-min{M[i,v], Af w] + £w }.
40
Shortest paths with negative weights: implementation
Theorem 1. Given a digraph G = (V,E) with no negative cycles, the DP algorithm computes the length of a shortest v^t path for every node v in G(mn) time and G(n2) space.
Pf.
• Table requires G(n2) space.
• Each iteration i takes 0(m) time since we examine each edge once. ■
Finding the shortest paths.
• Approach 1: Maintain successors, v\ that points to next node on a shortest v-*t path using < i edges.
• Approach 2: Compute optimal lengths M[i, v] and consider only edges with M[i, v] = M[i- l,w] + lvw. Any directed path in this subgraph is a shortest path.
41
Dynamic programming: quiz 6
It is easy to modify the DP algorithm for shortest paths to...
A. Compute lengths of shortest paths in OQnri) time and 0(m + n) space.
B. Compute shortest paths in OQnri) time and OQn + n) space. C Both A and B.
D. Neither A nor B.
42
Shortest paths with negative weights: practical improvements
Space optimization. Maintain two 1 D arrays (instead of 2D array).
• d[v] = length of a shortest v^t path that we have found so far.
• successor^] = next node on a v-±t path.
Performance optimization. If d[w] was not updated in iteration i-l, then no reason to consider edges entering w in iteration i.
43
Bellman-Ford-Moore: efficient implementation
Bellman-Ford-Moore(v; E, C, t)
foreach node v£V: d[v] <- °°.
successor\v\ <- null. d[i] «- 0. For i = 1 to w - 1
foreach node wGV:
IF ( d[w] + lvw) d[v] <- + £vw. successor [v] <- w. IF (no v + c(P')
and by Lemma 4, v>v d[v] does not increase _ Z(P) ■
Bellman-Ford-Moore: analysis
Theorem 2. Assuming no negative cycles, Bellman-Ford-Moore computes the lengths of the shortest v^t paths in 0(mn) time and G(n) extra space. Pf. Lemma 2 + Lemma 5. ■
/ \
shortest path exists and after i passes,
has at most n-l edges d[v] < length of shortest path
that uses < iedges
Remark. Bellman-Ford-Moore is typically faster in practice.
• Edge (v,w) considered in pass i+ 1 only if d[w] updated in pass i.
• If shortest path has k edges, then algorithm finds it after < k passes.
47
Dynamic programming: quiz 8
Assuming no negative cycles, which properties must hold throughout Bellman-Ford-Moore?
A. Following successor^] pointers gives a directed v~+t path.
B. If following successors pointers gives a directed v~+t path, then the length of that v^t path is d[v].
C. Both A and B.
D. Neither A nor B.
48
Bellman-Ford-Moore: analysis
Claim. Throughout Bellman-Ford-Moore, following the successor^] gives a directed path from v to t of length d[v\.
Counterexample. Claim is false! • Length of successor v-*t path may be strictly shorter than d\y\.
consider nodes in order: t, 1, 2, I
successor[2] = 1 successor[l] = t
d[2] = 20
?)— 10
[!] = 10
d[t] = 0
successor [3] 43]
49
Bellman-Ford-Moore: analysis
Claim. Throughout Bellman-Ford-Moore, following the successor^] gives a directed path from v to t of length d[v\.
Counterexample. Claim is false! • Length of successor v-*t path may be strictly shorter than d\y\.
consider nodes in order: t, 1, 2, 3
/-
successor[2] = 1 42] = 20,
successor[l] = 3 41] = 2
d[t] = 0
successor[3] = ř 43] = 1
50
Bellman-Ford-Moore: analysis
Claim. Throughout Bellman-Ford-Moore, following the successor^] pointers gives a directed path from v to t of length d[v\.
Counterexample. Claim is false!
• Length of successor v-*t path may be strictly shorter than d\y\.
• If negative cycle, successor graph may have directed cycles.
consider nodes in order: t, 1, 2, 3,
43] = 10 42] = 8
44] = 11 41] = 5
Bellman-Ford-Moore: analysis
Claim. Throughout Bellman-Ford-Moore, following the successor^] pointers gives a directed path from v to t of length d[v\.
Counterexample. Claim is false!
• Length of successor v-*t path may be strictly shorter than d\y\.
• If negative cycle, successor graph may have directed cycles.
consider nodes in order: t, 1, 2, 3, 4
43] = 10 42] = 8
44] = 11 41] = 3
Bellman-Ford-Moore: finding the shortest paths
Lemma 6. Any directed cycle W\n the successor graph is a negative cycle. Pf.
• If successor^] = w, we must have d[v] > d[w] + lvw.
(LHS and RHS are equal when successor^ is set; d[w] can only decrease; d[v] decreases only when successor^] is reset)
• Let vi -»... vk vi be the sequence of nodes in a directed cycle W.
• Assume that (v*,vi) is the last edge in Wadded to the successor graph.
• Just prior to that: d[vi] > d[v2] + £(vi,v2)
d[v2] > d[vs] + £(v2,v3)
d[Vjfc] + Z(Vk-l,Vk)
wr,, i ^ wr,, i , p/,, \ -_ holds with strict inequality
[v*] > [vi] + £(v*,Vi) «- since we are updating d[v*I
• Adding inequalities yields £(vi,v2) + ^(v2,v3) + ... + £(vjm,vjO +£(v*,vi) < 0. ■
i-1
W is a negative cycle
53
Bellman-Ford-Moore: finding the shortest paths
Theorem 3. Assuming no negative cycles, Bellman-Ford-Moore finds shortest v-*t paths for every node v in 0(mn) time and G(n) extra space. Pf.
• The successor graph cannot have a directed cycle. [Lemma 6]
• Thus, following the successor pointers from v yields a directed path to t.
• Let v = vi^ ... vk = t be the nodes along this path P.
• Upon termination, if successor^] = w, we must have d[v] = d[w] + lvw. (LHS and RHS are equal when successor^ is set; d[] did not change)
• Thus, d[vi] = d[v2] + £(vi,v2) N
since algorithm
d[v2] = d[v3] + £(V2, v3) terminated d[Vk-l] = d[Vk\ + l(Vk-l,Vk)
• Adding equations yields d[v] = d[t] + £(vi,v2> + £(>2,V3) + ... + £(v*-i,vjO. ■
/ t
length of path P min length of any v~+t path 0 (Theorem 2)
54
Single-source shortest paths with negative weights
year worst case discovered by
1955 0(n4) Shimbel
1956 0(m n2W) Ford
1958 0(m n) Bellman, Moore
1983 0(n314 m log W) Gabow
1989 0(m nm logOW)) Gabow-Tarjan
1993 0(m nm log W) Goldberg
2005 0(n238W) Sankowsi, Yuster-Zwick
2016 6{nlon\ogW) Cohen-Ma_dry-Sankowski-Vladu
20xx o o o
single-source shortest paths with weights between -W and W
DYNAMIC PROGRAMMING SUMMARY
conditions
• number of diffferent subproblems is polynomial
• problem solution can be easily deduced from solutions of subproblems
• subproblems can be naturally ordered from smallest to largest
memoization
• simple to understand
• no need to dictate the ordering of subproblems
dynamic programming
• no recursion overhead
• lower space complexity
• simple complexity analysis
Greedy Algorithms
Algorithmic paradigms
Greed. Build up a solution incrementally, myopically optimizing some local criterion.
Divide-and-conquer. Break up a problem into independent subproblems; solve each subproblem; combine solutions to subproblems to form solution to original problem.
Dynamic programming. Break up a problem into a series of overlapping subproblems\combine solutions to smaller subproblems to form solution to large subproblem.
fancy name for caching intermediate results in a table for later reuse
2
Greedy Algorithms
Coin Changing
4. Greedy Algorithms I
► coin changing
► interval scheduling
► interval partitioning
► scheduling to minimize lateness
► optimal caching
Coin changing
Goal. Given U. S. currency denominations {1,5,10,25,1 00 }, devise a method to pay amount to customer using fewest coins.
Ex. 34 0)
k <— largest coin denomination Ck such that Ck < x. IF no such k, return "no solution."
Else
X ^ X — Ck.
S ^SU{k}. Return S.
4
Greedy algorithms I: quiz 1 Is the cashier's algorithm optimal?
A. Yes, greedy algorithms are always optimal.
B. Yes, for any set of coin denominations ci 1: 7, 8, 9.
• Cashier's algorithm: 154 = 9 + ?.
• Optimal: 1 54 = 7 + 8.
Properties of any optimal solution (for U.S. coin denominations)
Property. Number of pennies < 4. Pf. Replace 5 pennies with 1 nickel.
Property. Number of nickels < 1. Property. Number of quarters < 3.
Property. Number of nickels + number of dimes < 2. Pf.
• Recall: < 1 nickel.
• Replace 3 dimes and 0 nickels with 1 quarter and 1 nickel;
• Replace 2 dimes and 1 nickel with 1 quarter.
dollars
(10(H)
quarters
(25(t)
dimes nickels (1(H) (5 ff .
• Sorting by finish times takes 0(n log n) time.
Interval scheduling: analysis of earliest-finish-time-first algorithm
Theorem. The earliest-finish-time-first algorithm is optimal.
Pf. [by contradiction]
• Assume greedy is not optimal, and let's see what happens.
• Let iu i2, ... 4 denote set of jobs selected by greedy.
• Let juj2, ...jm denote set of jobs in an optimal solution with
ix = jxj2 =j2, ...,ir= yrfor the largest possible value of r.
job ir+l exists and finishes no later than jr+l
i 1 i
Greedy:
Optimal:
/ t
job jr+l exists why not replace
because m > k job jr+l with job ir+l?
13
Interval scheduling: analysis of earliest-finish-time-first algorithm
Theorem. The earliest-finish-time-first algorithm is optimal.
Pf. [by contradiction]
• Assume greedy is not optimal, and let's see what happens.
• Let iu i2, ... 4 denote set of jobs selected by greedy.
• Let juj2, ...jm denote set of jobs in an optimal solution with
ix = jxj2 =j2, ...,ir= yrfor the largest possible value of r.
job ir+l exists and finishes before jr+l
i 1 i
Greedy:
Optimal:
t
solution still feasible and optimal (but contradicts maximality of r)
14
Greedy algorithms I: quiz 3
Suppose that each job also has a positive weight and the goal is to find a maximum weight subset of mutually compatible intervals. Is the earliest-finish-time-first algorithm still optimal?
A. Yes, because greedy algorithms are always optimal.
B. Yes, because the same proof of correctness is valid.
C. No, because the same proof of correctness is no longer valid.
D. No, because you could assign a huge weight to a job that overlaps the job with the earliest finish time.
1 5
Greedy Algorithms
Interval Partitioning
4. Greedy Algorithms I
► coin changing
► interval scheduling
► interval partitioning
► scheduling to minimize lateness
► optimal caching
Section 4.1
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 lectures occur at the same time in the same room.
Ex. This schedule uses 4 classrooms to schedule 10 lectures.
jobs e and g
9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 time
1 7
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 lectures occur at the same time in the same room.
Ex. This schedule uses 3 classrooms to schedule 10 lectures.
Greedy algorithms I: quiz 4
Consider lectures in some order, assigning each lecture to first available classroom (opening a new classroom if none is available). Which rule is optimal?
A. [Earliest start time] Consider lectures in ascending order of sj.
B. [Earliest finish time] Consider lectures in ascending order offjm
C. [Shortest interval] Consider lectures in ascending order offj-sj.
D. None of the above.
Interval partitioning: earliest-start-time-first algorithm
o
Earliest-Start-Time-First («, 51, S2,..., sn ,fufi,... ,/n)
SORT lectures by start times and renumber so that 51 < S2 ^ ... ^ sn. d <— 0. ^- number of allocated classrooms
for j = 1 to n
IF lecture y is compatible with some classroom Schedule lecture j in any such classroom k.
Else
Allocate a new classroom d + 1. Schedule lecture y in classroom d + 1. depth.
Q. Does minimum number of classrooms needed always equal depth? A. Yes! Moreover, earliest-start-time-first algorithm finds a schedule whose number of classrooms equals the depth.
Interval partitioning: analysis of earliest-start-time-first algorithm
Observation. The earliest-start-time first algorithm never schedules two incompatible lectures in the same classroom.
Theorem. Earliest-start-time-first algorithm is optimal. Pf.
• Let d = number of classrooms that the algorithm allocates.
• Classroom d is opened because we needed to schedule a lecture, say j, that is incompatible with a lecture in each of d- 1 other classrooms.
• Thus, these d lectures each end after Sj.
• Since we sorted by start time, each of these incompatible lectures start no later than sr
• Thus, we have d lectures overlapping at time Sj + e.
• Key observation => all schedules use > d classrooms. ■
23
Greedy Algorithms
Scheduling to Minimize Lateness
4. Greedy Algorithms I
► coin changing
► interval scheduling
► interval partitioning
► scheduling to minimize lateness
► optimal caching
Section 4.2
Scheduling to minimizing lateness
• Single resource processes one job at a time.
• Job j requires tj units of processing time and is due at time dj.
• If y starts at time sj, it finishes at time fj = sj + tj.
• Lateness: lj = max { 0, fj-dj}.
• Goal: schedule all jobs to minimize maximum lateness L^maxy ljm
D 2 3 D 5 6
tj 3 2 1 4 3
6 8 9 9 14
lateness = 2 lateness = 0 max lateness = 6
d3 = 9 J2 = 8 d6= 15 dl = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
25
Greedy algorithms I: quiz 5
Schedule jobs according to some natural order. Which order minimizes the maximum lateness?
A. [shortest processing time] Ascending order of processing time tj.
B. [earliest deadline first] Ascending order of deadline dj.
C. [smallest slack] Ascending order of slack: dj-tj.
D. None of the above.
Minimizing lateness: earliest deadline first
Earliest-Deadline-First (n, ti, t2,..., tn, di, d2,..., dn)
sort jobs by due times and renumber so that di < d2 < ... < d7 f «-0.
For j = 1 to n
Assign job j to interval [t, t + /,].
t ^ t + tj.
return intervals /i], [52,/2], - [5n,/n].
27
Minimizing lateness: no idle time
Observation 1. There exists an optimal schedule with no idle time.
an optimal schedule d = 4
d = 6
d= 12
01 2 3 4 5 6 7 8 9 1011
an optimal schedule d = 4
with no idle time
d = 6
d= 12
2 3 4 5 6 7 8 9 1011
Observation 2. The earliest-deadline-first schedule has no idle time.
28
Minimizing lateness: inversions
Def. Given a schedule S, an inversion is a pair of jobs i and j such that: i < j but j is scheduled before i.
inversion if / < j
a schedule with an inversion
j i
recall: we assume the jobs are numbered so that d\ < di < ... < dn
Observation 3. The earliest-deadline-first schedule is the unique idle-free schedule with no inversions.
Minimizing lateness: inversions
Def. Given a schedule S, an inversion is a pair of jobs i and j such that: i < j but j is scheduled before i.
inversion if / < j
a schedule with an inversion
j i
recall: we assume the jobs are numbered so that d\ < di < ... < dn
Observation 4. If an idle-free schedule has an inversion, then it has an adjacent inversion.
pf_ two inverted jobs scheduled consecutively
• Let i-j be a closest inversion.
• Let k be element immediately to the right of j.
• Case 1. [j>k] Then j-k is an adjacent inversion.
• Case 2. [j di < dj definition
31
Minimizing lateness: analysis of earliest-deadline-first algorithm
Theorem. The earliest-deadline-first schedule S is optimal.
optimal schedule can
Pf. [by contradiction] / have inversions
Define to be an optimal schedule with the fewest inversions.
• Can assume has no idle time. <— observation 1
• Case 1. [ S* has no inversions ] Then S = S*. <— observation 3
• Case 2. [ has an inversion ]
- let i-j be an adjacent inversion <— observation 4
- exchanging jobs i and j decreases the number of inversions by 1 without increasing the max lateness <— key claim
- contradicts "fewest inversions" part of the definition of 5* *
32
Greedy analysis strategies
Greedy algorithm stays ahead. Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's.
Structural. Discover a simple "structural" bound asserting that every possible solution must have a certain value. Then show that your algorithm always achieves this bound.
Exchange argument. Gradually transform any solution to the one found by the greedy algorithm without hurting its quality.
Other greedy algorithms. Gale-Shapley, Kruskal, Prim, Dijkstra, Huffman, ...
33
Greedy Algorithms
Optimal Caching
4. Greedy Algorithms I
► coin changing
► interval scheduling
► interval partitioning
► scheduling to minimize lateness
► optimal caching
Section 4.3
Optimal offline caching
Caching.
• Cache with capacity to store k items.
• Sequence of m item requests dl,d2, ■■-,dm.
• Cache hit: item in cache when requested.
• Cache miss: item not in cache when requested.
(must evict some item from cache and bring requested item into cache)
Applications. CPU, RAM, hard drive, web, browser,
Goal. Eviction schedule that minimizes the number of evictions
Ex. k = 2, initial cache = ab, requests: a,b,c,b,c,a,b. Optimal eviction schedule. 2 evictions. f
(/>
cache
a a b
b a
c c A b
b c b/
c c /b
a a b
b a b
cache miss (eviction)
38
Optimal offline caching: greedy algorithms
LIFO/FIFO. Evict item brought in least (most) recently. LRU. Evict item whose most recent access was earliest. LFU. Evict item that was least frequently requested.
cache
n si
to
I/)
I/)
a a w X y z
d a w X d z
a a w X d z
b a b X d z
c a b c d z
e a b c d e
8 ? ? ? ?
b
FIFO: eject a LRU: eject d
LIFO: eject e
cache miss (which item to eject?)
Optimal offline caching: farthest-in-future (clairvoyant algorithm)
Farthest-in-future. Evict item in the cache that is not requested until farthest in the future.
to
I/)
I/)
cache
a a b c d e
f ? ? ? ? ?
a
b
c
e
8
b
e
d
*
cache miss (which item to eject?)
FF: eject d
Theorem. [Belady 1966] FF is optimal eviction schedule. Pf. Algorithm and theorem are intuitive; proof is subtle.
Greedy algorithms I: quiz 6
Which item will be evicted next using farthest-in-future schedule?
cache
A.
B.
C.
D.
E.
re
C
fl)
• ■ ■ ■ ■
b d b y a
c d b c a
e d e c a
f ? ? ? ?
c
d
a
e
a
c
cache miss (which item to eject?)
41
Reduced eviction schedules
Def. A reduced schedule is a schedule that brings an item d into the cache in step j only if there is a request for d in step j and d is not already in the cache.
a a b c
a a b c
c a d c
d a d c
a a c
b a c b
c a c b
d d c b
d d c d
d enters cache without a request
d enters cache even though already in cache
a a b c
a a b c
c a b c
d a d c
a a d c
b a d
c a c b
d c b
d d c b
an unreduced schedule
a reduced schedule
42
Reduced eviction schedules
Claim. Given any unreduced schedule S, can transform it into a reduced
schedule S' with no more evictions.
Pf. [ by induction on number of steps j ]
• Suppose S brings d into the cache in step j without a request.
• Let c be the item S evicts when it brings d into the cache.
• Case la: d evicted before next request for d.
unreduced schedule S
. . c
c
step j
step j'
0
-id d
-id • • d
-id • d
e e
d enters cache without a request
d evicted before next request for d
■ S' c
• • c
• • c
• c
-d • • B
-d • c
e • • e
might as well leave c in cache until d is evicted
Reduced eviction schedules
Claim. Given any unreduced schedule S, can transform it into a reduced
schedule S' with no more evictions.
Pf. [ by induction on number of steps j ]
• Suppose S brings d into the cache in step j without a request.
• Let c be the item S evicts when it brings d into the cache.
• Case la: d evicted before next request for d.
• Case 1 b: next request for d occurs before d is evicted.
unreduced schedule S
S'
step j
step j'
c c c
-id d
-id • • d
-id • d
d d
d enters cache without a request
d still in cache before next request for d
c c c
• c
-d • • D
-d • c
d • d
might as well leave c in cache until d is requested
Reduced eviction schedules
Claim. Given any unreduced schedule S, can transform it into a reduced
schedule S' with no more evictions.
Pf. [ by induction on number of steps j ]
• Suppose S brings d into the cache in step j even though d is in cache.
• Let c be the item S evicts when it brings d into the cache.
• Case 2a: d evicted before it is needed.
unreduced schedule S
step j
step j'
d\ a c
d\ a c
d\ a 0
d d\ a d3
d d\ a d3
c c a d3
b c a b
d c a d3
di enters cache even though d\ is already in cache
di not needed
di evicted di needed
S'
d\ d\ d\ d\ d\
c c
a a a a a a a a
c c c c
d3
might as well leave c in cache until d$ in evicted
Reduced eviction schedules
Claim. Given any unreduced schedule S, can transform it into a reduced
schedule S' with no more evictions.
Pf. [ by induction on number of steps j ]
• Suppose S brings d into the cache in step j even though d is in cache.
• Let c be the item S evicts when it brings d into the cache.
• Case 2a: d evicted before it is needed.
• Case 2b: d needed before it is evicted.
unreduced schedule S
step j
step j'
d\ a c
d\ a c
d\ a 0
d d\ a d3
d d\ a d3
c c a d3
a c a d3
d c a d3
di enters cache even though d\ is already in cache
di not needed
di needed
S'
d\ a c
d\ a c
d\ a c
d d\ a c
d d\ a
c c a ^
a c a
d c a d3
might as well leave c in cache until d$ in needed
46
Reduced eviction schedules
Claim. Given any unreduced schedule S, can transform it into a reduced
schedule S' with no more evictions.
Pf. [ by induction on number of steps j ]
• Case 1: S brings d into the cache in step j without a request. ✓
• Case 2: S brings d into the cache in step j even though d is in cache. ✓
• If multiple unreduced items in step j, apply each one in turn, dealing with Case 1 before Case 2. ■
\
resolving Case 1 might trigger Case 2
47
Farthest-in-future: analysis
Theorem. FF is optimal eviction algorithm.
Pf. Follows directly from the following invariant.
Invariant. There exists an optimal reduced schedule S that has the same eviction schedule as SFF through the first j steps. Pf. [ by induction on number of steps j ] Base case: j' = 0.
Let S be reduced schedule that satisfies invariant through j steps. We produce S' that satisfies invariant after j+ 1 steps.
• Let d denote the item requested in step j+ 1.
• Since S and SFF have agreed up until now, they have the same cache contents before step j+ 1.
• Case 1: d is already in the cache. S' = S satisfies invariant.
• Case 2: d is not in the cache and S and SFF evict the same item. S' = S satisfies invariant.
48
Farthest-in-future: analysis
Pf. [continued] • Case 3: d is not in the cache; SFF evicts e\ S evicts f * e. - begin construction of S' from S by evicting e instead off
same e step j same e
s S'
same e ■ step j+1 same d
- now S1 agrees with SFF for first j+ 1 steps; we show that having item / in cache is no worse than having item e in cache
- let S' behave the same as S until S' is forced to take a different action (because either S evicts e; or because either e or/ is requested)
49
Farthest-in-future: analysis
Let / be the first step after j+ 1 that S' must take a different action from S; let g denote the item requested in step/.
involves either e or/(or both)
step j'
S'
• Case 3a- g — e ^'a9reeswith ^through firsty+i steps
Can't happen with FF since there must be a request for/ before e.
• Case 3b: g =/. Element/can't be in cache of S; let e' be the item that S evicts.
- \f e' = e, S' accesses/ from cache; now S and S' have same cache
- if e' * e, we make S' evict e' and bring e into the cache; now S and S' have the same cache
We let S' behav^exactly like S for remaining requests.
S' is no longer reduced, but can be transformed into a reduced schedule that agrees with FF through first j+ 1 steps
50
Farthest-in-future: analysis
Let / be the first step after j+ 1 that S' must take a different action from S; let g denote the item requested in step/.
involves wither e or/(or both)
step j'
S'
otherwise s' could have taken the same action
I
• Case 3c: g*e,f. S evicts e. - make S' evict /.
same
step j'
same
S'
now S and S' have the same cache
let S' behave exactly like S for the remaining requests
51
Caching perspective
Online vs. offline algorithms.
• Offline: full sequence of requests is known a priori.
• Online (reality): requests are not known in advance.
• Caching is among most fundamental online problems in CS.
LIFO. Evict item brought in most recently.
LRU. Evict item whose most recent access was earliest.
Theorem. FF is optimal offline eviction algorithm.
• Provides basis for understanding and analyzing online algorithms.
• LIFO can be arbitrarily bad.
• LRU is ^-competitive: for any sequence of requests o, LRU(o) < kFF(o) + k.
• Randomized marking is 0(log^-competitive.
t
FF with direction of time reversed!
see Section 1 3.8
52
Greedy Algorithms
Shortest Paths — Dijkstra's algorithm
4. Greedy Algorithms II
► Dijkstra's algorithm
► minimum spanning trees
► Prim, Kruskal, Boruvka
► single-link clustering
► m in-cost arborescences
Section 4.4
Single-pair shortest path problem
Problem. Given a digraph G = (V,E), edge lengths le > 0, source sGV, and destination tB V, find a shortest directed path from s to t.
Single-source shortest paths problem
Problem. Given a digraph G = (V,E), edge lengths le > 0, source sGV, find a shortest directed path from s to every node.
Assumption. There exists a path from s to every node.
source s
shortest-paths tree
Shortest paths: quiz 1
Suppose that you change the length of every edge of G as follows. For which is every shortest path in G a shortest path in G'?
A. Add 17.
B. Multiply by 17.
C. Either A or B.
D. Neither A nor B.
5
Shortest paths: quiz 2
Which variant in car GPS?
A. Single source: from one node s to every other node.
B. Single sink: from every node to one node t.
C. Source-sink: from one node s to another node t.
D. All pairs: between all pairs of nodes.
Shortest path applications
• PERT/CPM.
• Map routing.
• Seam carving.
• Robot navigation.
• Texture mapping.
• Typesetting in LaTeX.
• Urban traffic planning.
• Telemarketer operator scheduling.
• Routing of telecommunications messages.
• Network routing protocols (OSPF, BGP, RIP).
• Optimal truck routing through given traffic congestion pattern.
Network Flows: Theory, Algorithms, and Applications, by Ahuja, Magnanti, and Orlin, Prentice Hall, 1993.
Dijkstra's algorithm (for single-source shortest paths problem)
Greedy approach. Maintain a set of explored nodes S for which algorithm has determined d[u] = length of a shortest s-+u path.
• Initialize S<- {s}, d[s]^0.
• Repeatedly choose unexplored node v £S which minimizes
o
tt(v) = min [d
u
e = (u,v) : ueS^-■--/ "^^-^ the length of a shortest path from s
to some node u in explored part S, followed by a single edge e = (u, v)
8
Dijkstra's algorithm (for single-source shortest paths problem)
Greedy approach. Maintain a set of explored nodes S for which algorithm has determined d[u] = length of a shortest s-+u path.
• Initialize S<- {s}, d[s]^0.
• Repeatedly choose unexplored node v £S which minimizes
o
tt(v) = min [d
u
e = (u,v) : ueS^-■--/ "^^-^ the length of a shortest path from s
to some node u in explored part S, add V tO S, and Set d[v] <- Jt(v). followed by a single edge e = (w, v)
To recover path, set pred[v] <- e that achieves min.
9
Dijkstra's algorithm: proof of correctness
Invariant. For each node uGS: d[u] = length of a shortest s~*u path. Pf. [ by induction on \S\]
Base case: |5| = 1 is easy since S = { s } and d[s] = 0. Inductive hypothesis: Assume true for |5| > 1.
• Let v be next node added to S, and let (u,v) be the final edge.
• A shortest s^u path plus (u,v) is an s~+v path of length jt(v).
• Consider any other s^v path P. We show that it is no shorter than jt(v).
• Let e = (x,y) be the first edge in P that leaves S, and let P' be the subpath from s to x.
The length of P is already > jt(v) as soon as it reaches y.
l(P) > l(P') + le > d\x\ + le ^ 3t(y) > Jt(v) t t ft
non-negative lengths
inductive hypothesis
definition Dijkstra chose v ofjr(v) instead of y
Dijkstra's algorithm: efficient implementation
o
Critical optimization 1. For each unexplored node v^S :
explicitly maintain jr[v] instead of computing directly from definition
7r(v) = min d[u] + £e
e = (u,v) : u^S
• For each v^S : jt(v) can only decrease (because set S increases).
• More specifically, suppose u is added to S and there is an edge e = (u, v) leaving u. Then, it suffices to update:
jr[v] <— min { jt[v], jr[w] + £e) }
\
recall: for each u e S, n[u] = d[u] = length of shortest s-+u path
Critical optimization 2. Use a min-oriented priority queue (PQ) to choose an unexplored node that minimizes jr[v].
11
Dijkstra's algorithm: efficient implementation
Implementation.
• Algorithm maintains jt[v] for each node v.
• Priority queue stores unexplored nodes, using jr[] as priorities.
• Once u is deleted from the PQ, 7t[u] = length of a shortest s^u path.
dijkstra (V, E, I, s)
foreach v ^ s : jt[v] ™,pred[v] «- null; Jt[s] <- 0. Create an empty priority queue pq.
Foreach v e V: Insert^, v, ji[v]). While (Is-Not-Empty(^))
u «- del-MlN(p<7).
foreach edge e = (u, v) E E leaving u: IF (jt[v] > Jt[u] + £e)
Decrease-Key(/?<7, v, jr[w] + £e). jr[v] <- Jt[u] + le\ pred[v] «- e.
12
Dijkstra's algorithm: which priority queue?
Performance. Depends on PQ: n Insert, n Delete-Min, < m Decrease-Key.
• Array implementation optimal for dense graphs. <— oo2) edges
• Binary heap much faster for sparse graphs. <— 00) edges
• 4-way heap worth the trouble in performance-critical situations.
priority queue Insert Delete-Min Decrease-Key total
unordered array 0(1) 0(n) 0(1) 0(n2)
binary heap 0(\og ri) <9(log n) 0(log n) 0(m log n)
d-way heap (Johnson 1975) 0(d log,; n) 0(d \ogd n) 0(\ogd n) 0(m log,,,/,, n)
Fibonacci heap (Fredman-Tarjan 1984) 0(1) 0(log n) t 0(1) t 0(m + n log n)
integer priority queue (Thorup 2004) 0(1) 0(log log n) 0(1) 0(m + n log log n)
t amortized 13
Shortest paths: quiz 3
How to solve the the single-source shortest paths problem in undirected graphs with positive edge lengths?
A. Replace each undirected edge with two antiparallel edges of same length. Run Dijkstra's algorithm in the resulting digraph.
B. Modify Dijkstra's algorithms so that when it processes node u, it consider all edges incident to u (instead of edges leaving u).
C. Either A or B.
D. Neither A nor B.
Shortest paths: quiz 3
Theorem. [Thorup 1999] Can solve single-source shortest paths problem in undirected graphs with positive integer edge lengths in 0(m) time.
Remark. Does not explore nodes in increasing order of distance from s.
Undirected Single Source Shortest Paths with Positive Integer Weights in Linear Time
Mikkel Thorup AT&T Labs—Research
The single source shortest paths problem (SSSP) is one of the classic problems in algorithmic graph theory: given a positively weighted graph G with a source vertex s, find the shortest path from s to all other vertices in the graph.
Since 1959 all theoretical developments in SSSP for general directed and undirected graphs have been based on Dijkstra's algorithm, visiting the vertices in order of increasing distance from s. Thus, any implementation of Dijkstra's algorithm sorts the vertices according to their distances from s. However, we do not know how to sort in linear time.
Here, a deterministic linear time and linear space algorithm is presented for the undirected single source shortest paths problem with positive integer weights. The algorithm avoids the sorting bottle-neck by building a hierarchical bucketing structure, identifying vertex pairs that may be visited in any order.
1 5
Extensions of Dijkstra's algorithm
Dijkstra's algorithm and proof extend to several related problems:
• Shortest paths in undirected graphs: jr[v] < jz\u\ + l(u,v).
• Maximum capacity paths: jt[v] > min{jr[w], c(u, v)}.
• Maximum reliability paths: jr[v] > jt[u] x y(u, v).
Key algebraic structure. Closed semiring (min-plus, bottleneck, Viterbi, ...).
a + b = b + a a+ (b + c) = (a + b) + c
Greedy Algorithms
Minimum Spanning Trees — Prim, Kruskal and Borůvka
44
4. Greedy Algorithms II
Data Structures and Network Algorithms
CBMS-NSF
BtaONAi. CONFERENCE SERIES IN APPLIED MATHEMATICS
WONHMOIV
üu<»im>in Baue o*
TX tlAXtttnCM. SCUKCK
W710MM. UKI
► Dijkstra's algorithm
► minimum spanning trees
► Prim/ Kruskal, Boruvka
► single-link clustering
► min-cost arborescences
Section 6.1
Cycles
Def. A path is a sequence of edges which connects a sequence of nodes.
Def. A cycle is a path with no repeated nodes or edges other than the starting and ending nodes.
path P = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6) } cycle C = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1) }
22
Cuts
Def. A cut is a partition of the nodes into two nonempty subsets S and V-S. Def. The cutset of a cut S is the set of edges with exactly one endpoint in S.
cut S = { 4, 5, 8 }
cutset D = { (3, 4), (3, 5), (5, 6), (5, 7), (8, 7) }
23
Minimum spanning trees: quiz 1 Consider the cut S = { 1, 4, 6, 7 }. Which edge is in the cutset of S?
A. S is not a cut (not connected)
B. 1-7 C 5-7 D. 2-3
Minimum spanning trees: quiz 2
Let C be a cycle and let D be a cutset. How many edges do C and D have in common? Choose the best answer.
A. 0
B. 2
C. not 1
D. an even number
25
Cycle-cut intersection
Proposition. A cycle and a cutset intersect in an even number of edges.
cycle C = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1) } cutset D = { (3, 4), (3, 5), (5, 6), (5, 7), (8, 7) } intersection C n D = { (3, 4), (5, 6) }
26
Cycle-cut intersection
Proposition. A cycle and a cutset intersect in an even number of edges. Pf. [by picture]
Spanning tree definition
Def. Let H=(V, T) be a subgraph of an undirected graph G = (V,E). H is a spanning tree of G if H is both acyclic and connected.
graph G = (V, E) spanning tree H = (V, T)
28
Minimum spanning trees: quiz 3
Which of the following properties are true for all spanning trees H?
A. Contains exactly | v\ - 1 edges.
B. The removal of any edge disconnects it. C The addition of any edge creates a cycle. D. All of the above.
graph G = (V, E) spanning tree H = (V, T)
29
Spanning tree properties
Proposition. Let H= (V, T) be a subgraph of an undirected graph G = (V,E). Then, the following are equivalent:
• H is a spanning tree of G.
• H is acyclic and connected.
• H is connected and has | v\- 1 edges.
• H is acyclic and has | V\ - 1 edges.
• H is minimally connected: removal of any edge disconnects it.
• H is maximally acyclic: addition of any edge creates a cycle.
Minimum spanning tree (MST)
Def. Given a connected, undirected graph G = (V,E) with edge costs ce, a minimum spanning tree (V, T) is a spanning tree of G such that the sum of the edge costs in Tis minimized.
MST cost = 50 = 4 + 6 + 8 + 5 + 11 + 9 + 7
Cayley's theorem. The complete graph on n nodes has nn~2 spanning trees.
t
can't solve by brute force
32
Minimum spanning trees: quiz 4
Suppose that you change the cost of every edge in G as follows. For which is every MST in G an MST in G' (and vice versa)? Assume c(e) > 0 for each e.
A. c\e) = c(e) + 17.
B. c'(e)=llxc(e).
C. c\e) - logn c(e).
D. All of the above.
33
Applications
MST is fundamental problem with diverse applications.
• Dithering.
• Cluster analysis.
• Max bottleneck paths.
• Real-time face verification.
• LDPC codes for error correction.
• Image registration with Renyi entropy.
• Find road networks in satellite and aerial imagery.
• Model locality of particle interactions in turbulent fluid flows.
• Reducing data storage in sequencing amino acids in a protein.
• Autoconfig protocol for Ethernet bridging to avoid cycles in a network.
• Approximation algorithms for NP-hard problems (e.g., TSP, Steiner tree).
• Network design (communication, electrical, hydraulic, computer, road).
Network Flows: Theory, Algorithms, and Applications, by Ahuja, Magnanti, and Orlin, Prentice Hall, 1993.
34
Fundamental cycle
Fundamental cycle. Let H= (V, T) be a spanning tree of G = (V,E).
• For any non tree-edge e GE: T U {e} contains a unique cycle, say C.
• For any edge/ e C: TU{e}-{f} is a spanning tree.
e
graph G = (V, E) spanning tree H = (V, T)
Observation. If ce < c/, then (V, T) is not an MST.
Fundamental cutset
Fundamental cutset. Let H= (V, T) be a spanning tree of G = (V,E).
• For any tree edge/E T: T -{/} contains two connected components. Let D denote corresponding cutset.
• For any edge e GD : T-{f} U { e } is a spanning tree.
graph G = (V, E) spanning tree H = (V, T)
Observation. If ce < c/, then (V, T) is not an MST.
The greedy algorithm
Red rule.
• Let C be a cycle with no red edges.
• Select an uncolored edge of C of max cost and color it red. Blue rule.
• Let D be a cutset with no blue edges.
• Select an uncolored edge in D of min cost and color it blue.
Greedy algorithm.
• Apply the red and blue rules (nondeterministically!) until all edges are colored. The blue edges form an MST.
• Note: can stop once n- 1 edges colored blue.
37
Greedy algorithm: proof of correctness
Color invariant. There exists an MST (V, T*) containing every blue edge and no red edge.
Pf. [ by induction on number of iterations ]
Base case. No edges colored => every MST satisfies invariant.
Greedy algorithm: proof of correctness
Color invariant. There exists an MST (V, T*) containing every blue edge and no red edge.
Pf. [ by induction on number of iterations ]
Induction step (blue rule). Suppose color invariant true before blue rule.
• let D be chosen cutset, and let/be edge colored blue.
• if/eT*, then 7^ still satisfies invariant.
• Otherwise, consider fundamental cycle C by adding/to T*.
• let e e C be another edge in D.
• e is uncolored and ce > c/ since
- g£P => e not red
- blue rule => e not blue and ce > c/ T*
• Thus, T* \J {f}-{e} satisfies invariant.
Greedy algorithm: proof of correctness
Color invariant. There exists an MST (V, T*) containing every blue edge and no red edge.
Pf. [ by induction on number of iterations ]
Induction step (red rule). Suppose color invariant true before red rule.
• let C be chosen cycle, and let e be edge colored red.
• if e£T*, then 7* still satisfies invariant.
• Otherwise, consider fundamental cutset D by deleting e from T*.
• let/E D be another edge in C.
• /is uncolored and ce > c/ since
- f£T* => f not blue
- red rule => /not red and ce > c/ T*
• Thus, T* U {f}-{e} satisfies invariant. ■
Greedy algorithm: proof of correctness
Theorem. The greedy algorithm terminates. Blue edges form an MST. Pf. We need to show that either the red or blue rule (or both) applies.
• Suppose edge e is left uncolored.
• Blue edges form a forest.
• Case 1: both endpoints of e are in same blue tree.
=> apply red rule to cycle formed by adding e to blue forest.
Case 1
41
Greedy algorithm: proof of correctness
Theorem. The greedy algorithm terminates. Blue edges form an MST. Pf. We need to show that either the red or blue rule (or both) applies.
• Suppose edge e is left uncolored.
• Blue edges form a forest.
• Case 1: both endpoints of e are in same blue tree.
=> apply red rule to cycle formed by adding e to blue forest.
• Case 2: both endpoints of e are in different blue trees.
=> apply blue rule to cutset induced by either of two blue trees. ■
44
4. Greedy Algorithms II
Data Structures and Network Algorithms
CBMS-NSF
BtaONAi. CONFERENCE SERIES IN APPLIED MATHEMATICS
WONHMOIV
üu<»im>in Baue o*
TX tlAXtttnCM. SCUKCK
W710MM. UKI
► Dijkstra's algorithm
► minimum spanning trees
► Pr/Vr?, Kruskal, Boruvka
► single-link clustering
► min-cost arborescences
Section 6.2
Prim's algorithm
Initialize S = any node, T= 0. Repeat n - 1 times:
• Add to Ta min-cost edge with one endpoint in S.
• Add new node to S.
o
by construction, edges in cutset are uncolored
Theorem. Prim's algorithm computes an MST. \ Pf. Special case of greedy algorithm (blue rule repeatedly applied to S).
Kruskal's algorithm
Consider edges in ascending order of cost: • Add to tree unless it would create a cycle.
o
Theorem. Kruskal's algorithm computes an MST. Pf. Special case of greedy algorithm.
all other edges in cycle are blue
• Case 1: both endpoints of e in same blue tree. ^ => color e red by applying red rule to unique cycle.
• Case 2: both endpoints of e in different blue trees.
=> color e blue by applying blue rule to cutset defined by either tree. ■
\
no edge in cutset has smaller cost (since Kruskal chose it first)
Kruskal's algorithm: implementation
Theorem. Kruskal's algorithm can be implemented to run in 0(m\ogm) time.
• Sort edges by cost.
• Use union-find data structure to dynamically maintain connected components.
Kruskal (v, E, c)
sort m edges by cost and renumber so that c{e\) < c(ei) < ... < c(em). T^- 0.
foreach v ev: make-set(v).
For i = 1 to m
(w, v) <- eu
If (Find-Set(w) ± Find-Set(v)) < T T U{et}.
are u and v in same component?
UnionO, v). <
make u and v in same component
Return T.
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of cost:
• Delete edge from Tunless it would disconnect T.
Theorem. The reverse-delete algorithm computes an MST. Pf. Special case of greedy algorithm.
• Case 1. [ deleting edge e does not disconnect T ]
=> apply red rule to cycle C formed by adding e to another path in T between its two endpoints \ ^.
1 v N no edge in C is more expensive
(it would have already been considered and deleted)
• Case 2. [ deleting edge e disconnects T]
=> apply blue rule to cutset D induced by either component ■
\
e is the only remaining edge in the cutset (all other edges in D must have been colored red / deleted)
Fact. [Thorup 2000] Can be implemented to run in 0(mlog n (log log n)3) time.
48
Review: the greedy MST algorithm
Red rule.
• Let C be a cycle with no red edges.
• Select an uncolored edge of C of max cost and color it red. Blue rule.
• Let D be a cutset with no blue edges.
• Select an uncolored edge in D of min cost and color it blue.
Greedy algorithm.
• Apply the red and blue rules (nondeterministically!) until all edges are colored. The blue edges form an MST.
• Note: can stop once n- 1 edges colored blue.
Theorem. The greedy algorithm is correct. Special cases. Prim, Kruskal, reverse-delete, ...
49
Boruvka's algorithm
Repeat until only one tree.
• Apply blue rule to cutset corresponding to each blue tree.
• Color all selected edges blue.
Theorem. Boruvka's algorithm computes the MST. <-costs a"
Pf. Special case of greedy algorithm (repeatedly apply blue rule). ■
Boruvka's algorithm: implementation
Theorem. Boruvka's algorithm can be implemented to run in 0(m\ogn) time. Pf.
• To implement a phase in 0(m) time:
- compute connected components of blue edges
- for each edge (u, v) e E, check if u and v are in different components; if so, update each component's best edge in cutset
• < \og2n phases since each phase (at least) halves total # components. ■
Function Boruvka(\/, E, c)
1 K ^ (f)
2 count <- CountAndLabel(/<)
3 while count > 1 do
4
5
6
7
8
9 10
for / = 1 to count do S[i] Nil forall the (u, v) e E do
if label(u) ^ label {v) then
if c(u, v) < w(S[label(u)]) then if c(u, v) < w(S[label(v)]) then
S[label(u)] <- {u, v) S[label(v)] <- (u,v)
for / = 1 to count do if S[i] ^ nil then add S[i] to K
count <- CountAndLabel(/<)
11 return K
Boruvka's algorithm on planar graphs
Theorem. Boruvka's algorithm (contraction version) can be implemented to
run in 0(n) time on planar graphs.
Pf.
• Each Boruvka phase takes 0{n) time:
- Fact 1: m < 3n for simple planar graphs.
- Fact 2: planar graphs remains planar after edge contractions/deletions.
• Number of nodes (at least) halves in each phase.
• Thus, overall running time < cn + cn/2 + cn/4 + cn/8 + ... =0(n). ■
planar K33 not planar
A hybrid algorithm
Boruvka-Prim algorithm.
• Run Boruvka (contraction version) for log2log2^ phases.
• Run Prim on resulting, contracted graph.
Theorem. Boruvka-Prim computes an MST. Pf. Special case of the greedy algorithm.
Theorem. Boruvka-Prim can be implemented to run in 0(m\og\ogn) time.
• The log2log2ft phases of Boruvka's algorithm take 0(m\og\ogn) time; resulting graph has < n I log2 n nodes and < m edges.
• Prim's algorithm (using Fibonacci heaps) takes 0(m + n) time on a graph with n I log2 n nodes and m edges. ■ \
Pf.
Does a linear-time compare-based MST algorithm exist?
year worst case by 1
1975 0(m log log n) iterated logarithm function
1976 0(m log log n) Cheriton-Tarjan (Q ifn<1 lg n = <
1984 0(m log*w), 0(m + n log n) 1 1 + lg (lg n) if n > 1 Fredman-Tarjan
1986 0(m log (log* n)) Gabow-Galil-Spencer-Tarjan n lg*w
1997 0(m a(n) log a(n)) (-co, l] o Chazelle (1,2] 1
2000 0(m a(n)) Chazelle (2,4] 2
2002 asymptotically optimal n . " (4,16] 3 Pettie-Ramachandran (16,216] 4 a (216,265536] 5
20xx 0(m)
deterministic compare-based MST algorithms
Theorem. [Fredman-Willard 1990] 0(m) in word RAM model. Theorem. [Dixon-Rauch-Tarjan 1992] 0(m) MST verification algorithm. Theorem. [Karger-Klein-Tarjan 1995] 0(m) randomized MST algorithm.
Part IV
Network Flows
38
The Ford-Fulkerson Method
Ford-Fulkerson Algorithm
Capacity-Scaling Algorithm
Shortest Augmenting Path
Dinitz' Algorithm The Push-Relabel Method Network Flows — Applications
Bipartite Matching
Disjoint Paths
Multiple Sources and Sinks
Circulations with Supplies and Demands
The Ford-Fulkerson Method
The Ford-Fulkerson Method
Problem Formulation
7. Network Flow I
Section 7.1
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz algorithm
► simple unit-capacity networks
Flow network
A flow network is a tuple G = (V,E, s, t, c).
• Digraph (V,E) with source s e V and sink tB V.
• Capacity c(e)>0 for each eBE.
assume all nodes are reachable from s
Intuition. Material flowing through a transportation network; material originates at source and is sent to sink.
Minimum-cut problem
Def. An st-cut (cut) is a partition (A,B) of the nodes with sGA and teB.
Def. Its capacity is the sum of the capacities of the edges from A to B.
cap(A, B) = c(e)
e out of A
Minimum-cut problem
Def. An st-cut (cut) is a partition (A,B) of the nodes with sGA and teB.
Def. Its capacity is the sum of the capacities of the edges from A to B.
cap(A, B) = c(e)
e out of A
Minimum-cut problem
Def. An st-cut (cut) is a partition (A,B) of the nodes with sEA and teB.
Def. Its capacity is the sum of the capacities of the edges from A to B.
cap(A, B) = c(e)
e out of A
Min-cut problem. Find a cut of minimum capacity.
capacity =10 + 8+10 =(28
Network flow: quiz 1
Which is the capacity of the given st-cut?
A. 11 (20 + 25-8- 11 -9-6)
B. 34 (8+ 11 +9 + 6)
C. 45 (20 + 25)
D. 79 (20 + 25 + 8 + 11 +9 + 6)
Maximum-flow problem
Def. An ^-flow (flow) /is a function that satisfies:
• For each eGE: o < /(e) < c(e) [capacity]
• For each v£ V-{s,t}\ Yl = Y ^(e) [flow conservation]
e in to v e out of
Maximum-flow problem
Def. An ^-flow (flow) /is a function that satisfies:
• For each eBE: o < /(e) < c(e) [capacity]
• For each vG ^(e) = ^(e) [flow conservation]
e in to v e out of i;
Def. The value of a flow/is: val(f) = J2 ~ J2 f(e)
e out of s e in to s
value =
Maximum-flow problem
Def. An st-f\ow (flow) /is a function that satisfies:
• For each eGE: o < /(e) < c(e) [capacity]
• For each v£ V-{s,t}\ Yl = Y ^(e) [flow conservation]
e in to v e out of v
Def. The value of a flow/is: val(f) = J2 ~ J2
e out of s e in to s
Max-flow problem. Find a flow of maximum value.
value =
The Ford-Fulkerson Method
Ford-Fulkerson Algorithm
7. Network Flow I
Section 7.1
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz algorithm
► simple unit-capacity networks
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) < c(e).
• Augment flow along path P.
• Repeat until you get stuck.
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) < c(e).
• Augment flow along path P.
• Repeat until you get stuck.
flow network G and flow f
0
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) < c(e).
• Augment flow along path P.
• Repeat until you get stuck.
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) < c(e).
• Augment flow along path P.
• Repeat until you get stuck.
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) < c(e).
• Augment flow along path P.
• Repeat until you get stuck.
flow network G and flow f
0/4
\x 2/2 c? 6-9-/6 ^,
-9-/ 10 ^ [ ) -2-/ 9 { )_ 10/10_►( / ) 10+6=16
16
Toward a max-flow algorithm
Greedy algorithm.
• Start with f(e) = 0 for each edge eGE.
• Find an s-+t path P where each edge has f(e) w->f as first augmenting path.
flow network G
2
2
Bottom line. Need some mechanism to "undo" a bad decision.
19
Residual network
Original edge. e = (u,v) e E.
• Flow f(e).
• Capacity c(e).
Reverse edge, Reverse = (Vj uy
• "Undo" flow sent.
Residual capacity.
cf(e) =
c(e) - /(e) if e e E /(e) if e
reverse
original flow network G
u
6 / 17
/ \
flow capacity
■H v
residual network Gf
residual capacity
reverse edge
edges with positive residual capacity
Residual network. Gf=(V,Ef,s,t,cf).
• Ef={e:f(e)< c(e)} U {ereyerse : f(e) > 0}
• Key property: /' is a flow in Gf iff / + /' is a flow in G
where flow on a reverse edge
negates flow on corresponding forward edge
20
Augmenting path
Def. An augmenting path is a simple s-+t path in the residual network Gf.
Def. The bottleneck capacity of an augmenting path P is the minimum residual capacity of any edge in P.
Key property. Let/ be a flow and let P be an augmenting path in Gf. Then, after calling /' <- Augment(/, c, P), the resulting /' is a flow and
val(f') = val(f) + bottleneck(Gf, P).
Augment(/, c, P)
b <— bottleneck capacity of augmenting path P. foreach edge e G P :
IF «-/(*) + 8.
ELSE ^reverse) ^reverse) _ 5
Return /.
21
Network flow: quiz 2
Which is the augmenting path of highest bottleneck capacity?
A. A^F^G^H
B. A^B^C^D
C. A^F^B^G^H
D. A^F^B^G^C^D^H
residual capacity
source
5 > 8 > 4 5 t path P in Gf)
f«- Augment (/, c, P).
Update G/. augmenting path
Return /.
7. Network Flow I
Section 7.1
► Ford-Fulkerson demo
► exponentiol-time example
► pathological example
Ford-Fulkerson algorithm demo
Ford-Fulkerson algorithm demo
network G and flow f
0+8 = 8
residual network Gf
Ford-Fulkerson algorithm demo
network G and flow f
Ford-Fulkerson algorithm demo
network G and flow f
Ford-Fulkerson algorithm demo
network G and flow f
Ford-Fulkerson algorithm demo
network G and flow f
The Ford-Fulkerson Method
Max-Flow Min-Cut Theorem
7. Network Flow I
Section 7.2
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz' algorithm
► simple unit-capacity networks
Relationship between flows and cuts
Flow value lemma. Let/ be any flow and let (A,B) be any cut. Then, the value of the flow/equals the net flow across the cut (A,B).
val(f) = /(e) - E
e out of A e in to A
net flow across cut = 5 + 10 + 10 = 25
Relationship between flows and cuts
Flow value lemma. Let/ be any flow and let (A,B) be any cut. Then, the value of the flow/equals the net flow across the cut (A,B).
val(f) = /(e) - E
e out of A e in to A
net flow across cut = 10 + 5 + 10 = 25
26
Relationship between flows and cuts
Flow value lemma. Let/ be any flow and let (A,B) be any cut. Then, the value of the flow/equals the net flow across the cut (A,B).
val(f) = /(e) - E
e out of A e in to A
net flow across cut = (10+10 + 5 + 10 + 0 + 0) - (5 + 5 + 0 + 0) = 25
edges from B to A
= 25
27
Network flow: quiz 3
Which is the net flow across the given cut?
A. 11 (20 + 25-8- 11 -9-6)
B. 26 (20 + 22 - 8 - 4 - 4) C 42 (20 + 22)
D. 45 (20 + 25)
flow capacity
Relationship between flows and cuts
Flow value lemma. Let/ be any flow and let (A,B) be any cut. Then, the value of the flow/equals the net flow across the cut (A,B).
val(f) = /(e) - E
e out of A e in to A
Pf.
by flow conservation, all terms except for v = s are 0
val(f) = /(e) " E /(e)
e out of s e in to s
- = E ( E /(*> - E /(«)
-uGA \e out of v e in to -u
= E ZW " E /(e) .
e out of A e in to A
29
Relationship between flows and cuts
Weak duality. Let/ be any flow and (A,B) be any cut. Then, val(f) val(f) = cap(A,B). ■
\
weak duality
8/9
\
o
Q_ 10/10 —^(7)
T /
's
0/4
13/16
value of flow = 28
capacity of cut = 28
31
Max-flow min-cut theorem
Max-flow min-cut theorem. Value of a max flow = capacity of a min cut.
strong duality
MAXIMAL FLOW THROUGH A NETWORK
L. R. FORD, Jr. and D. R. FULKERSON
Introduction. The problem discussed in this paper was formulated by T. Harris as follows:
"Consider a rail network connecting two cities by way of a number of intermediate cities, where each link of the network has a number assigned to it representing its capacity. Assuming a steady state condition, find a maximal flow from one given city to the other."
ON THE MAX FLOW MIN CUT THEOREM OP NETWORKS
0. B. Dantzlg D. R. Fulkerson
P-826
April 15, 1955
J)
A Note on the Maximum Flow Through a Network"
P. ELIASt, A. FEINSTEINJ, AND C. E. SHANNON §
Summary -This note discusses the problem of maximizing the rate of flow from one terminal to another, through a network which consists of a number of branches, each of which has a limited capacity. The main result is a theorem: The maximum possible flow from left to right through a network is equal to the minimum value among all simple cut-sets. This theorem is applied to solve a more general problem, in which a number of input nodes and a number of output nodes are used.
from one terminal to the other in the original network passes through at least one branch in the cut-set. In the network above, some examples of cut-sets are (d, e, /), and (b, c, e, g, h), (d, g, h, i). By a simple cutset we will mean a cut-set such that if any branch is omitted it is no longer a cut-set. Thus (d, e, f) and (b, c, e, g, h) are simple
32
Max-flow min-cut theorem
Max-flow min-cut theorem. Value of a max flow = capacity of a min cut. Augmenting path theorem. A flow/ is a max flow iff no augmenting paths.
Pf. The following three conditions are equivalent for any flow/:
i. There exists a cut (A,B) such that cap(A,B) = val(f).
ii. / is a max flow.
iii. There is no augmenting path with respect to/. <— if Ford-Fuikerson terminates,
-* u • then/is max flow
[i=>N]
• This is the weak duality corollary. ■
33
Max-flow min-cut theorem
Max-flow min-cut theorem. Value of a max flow = capacity of a min cut. Augmenting path theorem. A flow/ is a max flow iff no augmenting paths.
Pf. The following three conditions are equivalent for any flow/:
i. There exists a cut (A,B) such that cap(A,B) = val(f).
ii. / is a max flow.
iii. There is no augmenting path with respect to/.
[ ii => iii ] We prove contrapositive: -> iii => -> ii.
• Suppose that there is an augmenting path with respect to/.
• Can improve flow/ by sending flow along this path.
• Thus, / is not a max flow. ■
34
Max-flow min-cut theorem
[iii=>M
• Let/ be a flow with no augmenting paths.
• Let A be set of nodes reachable from s in residual network Gf.
• By definition of A: sGA.
• By definition of flow/ t£A.
val(f)
E /(*) - E /(*)
e out of A
e in to A
flow value lemma
e out of A
— cap(A,B)
edge e = (v,w) with vGß,wGA must have/(e) = 0
original flow network G
edge e = (v, w) with vGA,w£ß must have/(e) = c(e)
35
The Ford-Fulkerson Method
Capacity-Scaling Algorithm
7. Network Flow I
Section 7.3
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz' algorithm
► simple unit-capacity networks
Analysis of Ford-Fulkerson algorithm (when capacities are integral)
Assumption. Every edge capacity c(e) is an integer between 1 and C.
Integrality invariant. Throughout Ford-Fulkerson, every edge flow f(e) and residual capacity cf(e) is an integer.
Pf. By induction on the number of augmenting paths. ■ consider cut a = {s}
(assumes no parallel edges)
/
Theorem. Ford-Fulkerson terminates after at most val(f*) < nC augmenting paths, where/* is a max flow.
Pf. Each augmentation increases the value of the flow by at least 1. ■
Corollary. The running time of Ford-Fulkerson is 0(mnC).
Pf. Can use either BFS or DFS to find an augmenting path in 0(m) time. ■
y f(e) is an integer for every e
Integrality theorem. There exists an integral max flow/*.
Pf. Since Ford-Fulkerson terminates, theorem follows from integrality
invariant (and augmenting path theorem). ■
37
Ford-Fulkerson: exponential example
Q. Is generic Ford-Fulkerson algorithm poly-time in input size?
m, n, and log C
A. No. If max capacity is C, then algorithm can take > C iterations.
o
each augmenting path sends only 1 unit of flow (# augmenting paths = 2C)
Network flow: quiz 4
The Ford-Fulkerson algorithm is guaranteed to terminate if the edge capacities are ...
A. Rational numbers.
B. Real numbers.
C. Both A and B.
D. Neither A nor B.
39
Choosing good augmenting paths
Use care when selecting augmenting paths.
• Some choices lead to exponential algorithms.
• Clever choices lead to polynomial algorithms.
Pathology. When edge capacities can be irrational, no guarantee that Ford-Fulkerson terminates (or converges to a maximum flow)!
Goal. Choose augmenting paths so that:
• Can find augmenting paths efficiently.
• Few iterations.
Choosing good augmenting paths
Choose augmenting paths with: • Max bottleneck capacity ("fattest").
Sufficiently large bottleneck capacity. + Fewest edges. <-ahead
how to find? — next
Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems
Dokl. Akad. Nauk SSSR Tom 194 (1970), No. 4
Soviet Math. Dokl. Vol. 11 (1970), No. 5
JACK EDMONDS
University of Waterloo, Waterloo, Ontario, Canada AND
RICHARD M. KARP
University of California, Berkeley, California
abstiiact. This paper presents new algorithms for the maximum flow problem, the Hitchcock transportation problem, and the general minimum-cost flow problem. TTpper bounds on the numbers of steps in these algorithms are derived, and are shown to compare favorably with upper bounds on the numbers of steps required by earlier algorithms.
Edmonds-Karp 1972 (USA)
ALGORITHM FOR SOLUTION OF A PROBLEM OF MAXIMUM FLOW IN A NETWORK WITH
POWER ESTIMATION
UDC 518.5
E. A. DINIC
Different variants of the formulation of the problem of maximal stationary flow in a network and its many applications are given in [l]. There also is given an algorithm solving the problem in the case where the initial data are integers (or, what is equivalent, commensurable). In the general case this algorithm requires preliminary rounding off of the initial data, i.e. only an approximate solution of the problem is possible. In this connection the rapidity of convergence of the algorithm is inversely proportional to the relative precision.
Dinitz 1970 (Soviet Union)
invented in response to a class exercises by Adel'son-Vel'skiT
41
Capacity-scaling algorithm
Overview. Choosing augmenting paths with "large" bottleneck capacity.
• Maintain scaling parameter A. ough not necessarily largest
• Let Gy(A) be the part of the residual network containing only those edges with capacity > A.
• Any augmenting path in GAA) has bottleneck capacity > A.
Capacity-scaling algorithm
Capacity-Scaling(G)
foreach edge e G E : f(e) <- 0. A largest power of 2 < C.
while (A > 1)
G/(A) <- A-residual network of G with respect to flow /. while (there exists an s-+t path P in G/(A))
/<- Augment (/, c, P).
Update G/(A). A-scaling phase
A^ A/2. Return /
43
Capacity-scaling algorithm: proof of correctness
Assumption. All edge capacities are integers between 1 and C.
Invariant. The scaling parameter A is a power of 2.
Pf. Initially a power of 2; each phase divides A by exactly 2. ■
Integrality invariant. Throughout the algorithm, every edge flow f(e) and
residual capacity cf(e) is an integer.
Pf. Same as for generic Ford-Fulkerson. ■
Theorem. If capacity-scaling algorithm terminates, then /is a max flow. Pf.
• By integrality invariant, when A= 1 => G/(A) = Gf.
• Upon termination of A = 1 phase, there are no augmenting paths.
• Result follows augmenting path theorem ■
44
Capacity-scaling algorithm: analysis of running time
Lemma 1. There are 1 + |_log2C| scaling phases.
Pf. Initially C/2 < A < C; A decreases by a factor of 2 in each iteration. ■
Lemma 2. Let/be the flow at the end of a A-scaling phase. Then, the max-flow value < val(f) + m A. Pf. Next slide.
Lemma 3. There are < 2m augmentations per scaling phase.
• Let/be the flow at the beginning of a A-scaling phase.
• Lemma 2 => max-flow value < val(f) + m(2A).
• Each augmentation in a A-phase increases val(f) by at least A. ■
Theorem. The capacity-scaling algorithm takes 0(m2\ogC) time.
Pf.
or equivalently, at the end
Pf.
• Lemma 1 + Lemma 3 => 0(mlog C) augmentations.
• Finding an augmenting path takes 0(m) time. ■
45
Capacity-scaling algorithm: analysis of running time
Lemma 2. Let/be the flow at the end of a A-scaling phase.
Then, the max-flow value < val(f) + mA.
Pf.
• We show there exists a cut (A, B) such that cap(A,B) < va/(/) + mA
• Choose A to be the set of nodes reachable from s in Gy(A).
• By definition of A: s£A.
• By definition of flow/ t£A.
edge e = (v,w) with vGß,wGA must have/() < A
original flow network
val(f) = /(e) " E /(e)
^ e out of A e in to A
flow value . ^ x—^
lemma > ^ (c(e) - A) - ^ A
e out of A e in to A
> £ c(e) - Y, A " E A
e out of A e out of A e in to A
> cap(A, B) — mA ■
edge e - (v, w) with v G A, w E ß must have/(e) > c(e) - A
The Ford-Fulkerson Method
Shortest Augmenting Path
Dexter C. Kozen
Section 17.2
7. Network Flow I
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz algorithm
► simple unit-capacity networks
Shortest augmenting path
Q. How to choose next augmenting path in Ford-Fulkerson? A. Pick one that uses the fewest edges.
\
can find via BFS
Shortest-Augmenting-Path(G)
foreach eGE: f(e) <- 0.
Gf <— residual network of G with respect to flow/. while (there exists an s~*t path in Gf)
[ P <- Breadth-First-Search(G/). )
/ <- Augment (/ c, P).
Update Gf.
Return /
48
Shortest augmenting path: overview of analysis
Lemma 1. The length of a shortest augmenting path never decreases. Pf. Ahead. \
number of edges
Lemma 2. After at most m shortest-path augmentations, the length of a shortest augmenting path strictly increases. Pf. Ahead.
Theorem. The shortest-augmenting-path algorithm takes 0(m2n) time. Pf.
• 0(m) time to find a shortest augmenting path via BFS.
• There are < mn augmentations.
- at most m augmenting paths of length k <— Lemma 1 + Lemma 2
- at most n-l different lengths ■
\
augmenting paths are simple paths
49
Shortest augmenting path: analysis
Def. Given a digraph G = (V,E) with source s, its level graph is defined by:
• l(v) = number of edges in shortest s-+v path.
• LG = (V,EG) is the subgraph of G that contains only those edges (v,w)EE with l(w) = l(v)+ 1.
level graph Lg
Network flow: quiz 5
Which edges are in the level graph of the following digraph?
A. D^F.
B. E^F.
C- Both A and B. D. Neither A nor B.
Shortest augmenting path: analysis
Def. Given a digraph G = (V,E) with source s, its level graph is defined by:
• £(v) = number of edges in shortest s-+v path.
• LG = (V,EG) is the subgraph of G that contains only those edges (y,w)BE with £(w) = £(v)+ 1.
Key property. P is a shortest s-+v path in G iff P is an s^v path in L,
level graph Lg
1=0 1=1 1=2 1=3
Shortest augmenting path: analysis
Lemma 1. The length of a shortest augmenting path never decreases.
• Let/and/' be flow before and after a shortest-path augmentation.
• Let LG and LG> be level graphs of G^and Gf .
• Only back edges added to Gy
(any s-+t path that uses a back edge is longer than previous length) ■
1=0 1=1 1=2 1=3
level graph Lg'
Shortest augmenting path: analysis
Lemma 2. After at most m shortest-path augmentations, the length of a shortest augmenting path strictly increases.
• At least one (bottleneck) edge is deleted from LG per augmentation.
• No new edge added to LG until shortest path length strictly increases. ■
Shortest augmenting path: review of analysis
Lemma 1. Throughout the algorithm, the length of a shortest augmenting path never decreases.
Lemma 2. After at most m shortest-path augmentations, the length of a shortest augmenting path strictly increases.
Theorem. The shortest-augmenting-path algorithm takes 0(m2n) time.
Shortest augmenting path: improving the running time
Note. S(mn) augmentations necessary for some flow networks.
• Try to decrease time per augmentation instead.
• Simple idea => 0(mn2) [Dinitz 1970] <— ahead
• Dynamic trees => 0(mn\ogn) [Sleator-Tarjan 1983]
A Data Structure for Dynamic Trees
Daniel D. Sleator and Robert Endre Tarjan
Bell Laboratories, Murray Hill, New Jersey 07974 Received May 8, 1982; revised October 18, 1982
A data structure is proposed to maintain a collection of vertex-disjoint trees under a sequence of two kinds of operations: a link operation that combines two trees into one by adding an edge, and a cut operation that divides one tree into two by deleting an edge. Each operation requires 0(log n) time. Using this data structure, new fast algorithms are obtained for the following problems:
(1) Computing nearest common ancestors.
(2) Solving various network flow problems including finding maximum flows, blocking flows, and acyclic flows.
(3) Computing certain kinds of constrained minimum spanning trees.
(4) Implementing the network simplex algorithm for minimum-cost flows.
The most significant application is (2); an 0(mn log «) time algorithm is obtained to find a maximum flow in a network of n vertices and m edges, beating by a factor of log n the fastest algorithm previously known for sparse graphs.
56
The Ford-Fulkerson Method
Dinitz' Algorithm
Dexter C. Kozen
7. Network Flow I
Section 18.1
► max-flow and min-cut problems
► Ford-Fulkerson algorithm
► max-flow min-cut theorem
► capacity-scaling algorithm
► shortest augmenting paths
► Dinitz algorithm
► simple unit-capacity networks
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations • Construct level graph LG.
within a phase, length of shortest augmenting path does not change
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
construct level graph
level graph Lc
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update lg; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
augment
level graph Lc
60
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
advance
level graph Lc
61
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
advance
level graph Lc
63
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
augment
level graph Lc
64
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
advance
0
level graph Lc
65
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
retreat
0
level graph Lc
66
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
retreat
level graph Lc
67
Dinitz' algorithm
Two types of augmentations.
• Normal: length of shortest path does not change.
• Special: length of shortest path strictly increases.
Phase of normal augmentations.
• Construct level graph LG.
• Start at s, advance along an edge in LG until reach t or get stuck.
• If reach t, augment flow; update LG; and restart from s.
• If get stuck, delete node from LG and retreat to previous node.
Dinitz' algorithm (as refined by Even and Itai)
initialize^,/)
Lg level-graph of G/. P <-0.
Goto Advancer).
Retreat(v)
If (v = s) Stop. Else
Delete v (and all incident edges) from Lg. Remove last edge (w, v) from P.
Goto Advance(w).
Advance(v) If (v = i)
augment(P).
Remove saturated edges from Lg. P «-0.
Goto Advance(s).
If (there exists edge (v, w) E Lg) Add edge (v, w) to P.
Goto Advance(w).
Else
Goto Retreat(v).
Network flow: quiz 6 How to compute the level graph Lg efficiently?
A. Depth-first search.
B. Breadth-first search.
C. Both A and B.
D. Neither A nor B.
70
Dinitz' algorithm: analysis
Lemma. A phase can be implemented to run in 0(mn) time. Pf.
• Initialization happens once per phase. <- o(m) using bfs
• At most m augmentations per phase. <- o(mn) per phase
(because an augmentation deletes at least one edge from LG)
• At most n retreats per phase. <- o(m + «) per phase
(because a retreat deletes one node from LG)
• At most mn advances per phase. <- o(mn) per phase
(because at most n advances before retreat or augmentation) ■
Theorem. [Dinitz 1 970] Dinitz' algorithm runs in 0(mn2) time. Pf.
• By Lemma, O(mn) time per phase.
• At most n-l phases (as in shortest-augmenting-path analysis). ■
71
The Ford-Fulkerson Method
Summary
Augmenting-path algorithms: summary
year method # augmentations running time
1955 augmenting path nC 0(m n C)
1972 fattest path m log (mC) 0(nr log n log (mC))
1972 capacity scaling m log C 0(m2 log C)
1985 improved capacity scaling m log C 0(m n log C)
1970 shortest augmenting path m n 0(m2 n)
1970 level graph m n 0(m n2)
1983 dynamic trees m n 0(m n log n)
fat paths
shortest paths
augmenting-path algorithms with m edges, n nodes, and integer capacities between 1 and C
72
Maximum-flow algorithms: theory highlights
year method worst case discovered by
1951 simplex 0(m n2 C) Dantzig
1955 augmenting paths 0(m n C) Ford-Fulkerson
1970 shortest augmenting paths 0(m n2) Edmonds-Karp, Dinitz
1974 blocking flows 0(n3) Karzanov
1983 dynamic trees 0(m n log n) Sleator-Tarjan
1985 improved capacity scaling 0(m n log C) Gabow
1988 push-relabel 0(m n log (n21 m)) Goldberg-Tarjan
1998 binary blocking flows 0(m312 log {n21 in) log C) Goldberg-Rao
2013 compact networks 0(m n) Orlin
2014 interior-point methods 6{mmm log C) Lee-Sid ford
2016 electrical flows 6(mwn Cin) Madry
20xx o o o
max-flow algorithms with m edges, n nodes, and integer capacities between 1 and c 73
The Push-Relabel Method
THOMAS H .ÍORHEN CHARLES E. LflíERSOw RONALD L. R I V E S T CLIFFORD STEIN
INTRODUCTION TO
ALGORITHM S
T H I H O EDITION
kapitola 26.4.
Ford-Fulkerson method vs Goldberg method
aka augmenting path method vs push relabel method
• global vs local character
• update flow along an augmenting path vs update flow on edges
• flow conservation vs preflow
pre-flow is a function f with
capacity condition for e G E\ 0 < ^(e) < c(e) relaxed flow conservation for v G V \ {s, £}:
E f(e)^ E ^)
e into v e out of v
overflowing vertex
vertex v G V \ {s, t} with ^ f (e) > ^ f (e)
e into \/ e out of v
excess flow into vertex v
the quantity ef(v) — ^(e) — ^(e)
e into v e out of v
a pre-flow becomes a flow if no intermediate node has an excess
42
height function is a function h : V —> No
height function h is compatible with preflow f iff source h(s) = \ V\ = n sink h(t) = 0
height difference h(v) < h(w) + 1 for every edge (\/, w) of the residual network Gf
if /7(\/) > h(w) +1 then (\/, w) is not an edge in the residual network Gf
43
Lema
If f is a preflow and h is an height function compatible with f then there is no path from the source s to the sink t in the residual network
• assume that Gf contains a path p = (vq, vi, • • •, ^/c) with vq = s and
^ = t
• w.l.o.g. p is simple and thus k < n
• because h is a height function h[v\) < h(vj+i) + 1 for / = 0,1,..., k — 1
• combining inequalities over p yields h(s) < h(t) + k
• because h(t) — 0, we have h(s) < k < n, which contradits the requirement h(s) = n
44
Lema
If f is a preflow and h is an height function compatible with f then there is no path from the source s to the sink t in the residual network
• assume that Gf contains a path p = (vq, vi, • • •, ^/c) with vq = s and
^ = t
• w.l.o.g. p is simple and thus k < n
• because h is a height function h[v\) < h(vj+i) + 1 for / = 0,1,..., k — 1
• combining inequalities over p yields h(s) < h(t) + k
• because h(t) — 0, we have h(s) < k < n, which contradits the requirement h(s) = n
Lema
If f is a flow and h is an height function compatible with f then f is a maximal flow.
44
initialization — height function
• h(v) — 0 for alle v 6 V, v ^ s
• h(s) — n
initialization — preflow
• ^(s, v) = c(s, v) for each (s, i/)gF
• ^(l/, v) = 0 for all other edges
initial preflow and height function are compatible
45
Algorithm: Generic-Push-Relabel Input: flow network G = (\/, E, s, t, c) Output: maximal flow f Initialize-PreFlow while true do
if no node is overflowing then return f
select an overflowing vertex v
if v has a neigbor w in Gf such that /?(\/) > h(w) then Push(/r, /7, \/, w)
else
Relabel^, ft, v)
Algorithm: Initialize-PreFlow_
for v e V do h(v) <- 0; e f (v) <- 0 h(s) <- n
for e G E do f (e) <- 0 for (s, v) e E do
f (s, v) <- c(s, \/); ef (v) <- c(s, \/); ef (s) <- ef (s) - c(s, u)
Push applies when v si overflowing, Cf(v, w) > 0, and h(w) < h(v)
Function PusH(f, /7, w)
1 Af(v, w) <— min(ef (\/), Cf(v, w))
2 if (\/, i/i/) G E then f (y, w) <<— f (y, w) + Af (y, w)
4 else
f (w, y) <<— y) — Af (y, w)
6 ef(v) «— ef(v) — Af(v, i/i/)
7 (i/i/) ^- (i/i/) + Af(v, i/i/)
8 return f, /?
we car? change (i.e. increase or decrease) flow from v to w by Af (v, i/i/) without causing ef(v) to become negative or the capacity c(v, w) to be exceeded
48
Relabel applies when v si overflowing and for all w G V such that (v, w) G we have h(v) < h(w)
Function Relabel^, h, v) l h(v) <— 1 + min{/7(i/i/)|(\/, w) G Ef} return h
when v is relabeled, Ef must contain at least one edge that leaves v, so that the minimization in the code is over a nonempty set
49
A demo of push-relabel algo: initialization
A maximum-flow instance
Initial pre-flow f
and residual graph G_f
e(u)=3
93 /106
A demo of push-relabel algo: Step 1
u Q/1 v Q/2 t Initial pre-flow f __anC' res'c'ua' 9raPn
e(u)=3
RelabeK u )
u
v n/~ t Initial pre-flow f e(u)=3 n/j^ak^ and residual graph G_f
94 /106
A demo of push-relabel algo: Step 2
h(v)A
h(v)A
2 ■-
1
Push( u, v )
e(u)=2 m
e(v)=l
95 /106
A demo of push-relabel algo: Step 3
96 /106
A demo of push-relabel algo: Step 4
97 /106
A demo of push-relabel algo: Step 5
98 /106
A demo of push-relabel algo: Step 6
99 /106
correctness loop invariant
1. f is a preflow
2. the height function h is compatible with f
• Initialize-PreFlow makes f a preflow and h compatible with f
• Push complies with capacities of edges and if a new edge appears in the residual network, then this edge fulfills the height difference
• Relabel operation affects only height attributes and preserves compatibility
at termination, each inner vertex must have an excess 0, f is flow and is a maximum flow
50
complexity — bound on Relabel operations
• the initial height of all vertices (except the source s)
• everyRelabel operation increases the height by 1
• we need a bound on the maximal height
Let f be a preflow. Then for every overflowing vertex v, there is a simple path from v to s in the residual network Gf.
• let B — {v\ there is no path from v to s in Gf}
• let us sum up the excesses of all vertices in B,
Ee^) = E( E f(e)- E f(e))^°
v£B v£B e into v e out of v
• edge (x,y) with x,y 6 B contributes to the sum J2veB Gf{v) with zero value
• for edge (x,y) with x ^ B and y G 8, the flow f((x,y)) is zero (otherwise there would be a path from y to s in Gf)
• edge (x,y) with x G 8, y ^ 8 contributes to the sum eKv) with the value — f((x,y))
• EvGfi eK^) = " Ee out of B f(e) > 0
• flows are nonegative and thus J2e out of B f (e) = 0 and all vertices in B have zero excess
52
At any time during the execution of Generic-Push-Relabel we have h(v) <2n-l for all veV.
• initially, h(s) = n and h(t) — 0 and these values never change
• when v is relabeled, it is overflowing and there is a simple path p from v to s if Gf
• there are at most n — 1 edges on p, every edge fulfills the height difference condition [i.e. every edge decreases the height at most by
i)
• h(v) - h(s) < n - 1, i.e. h(v) < 2n - 1
During the execution of Generic-Push-Relabel, the number of relabel operations is at most 2/7 — 1 per vertex and at most
(2n - l)(n - 2) < 2n2 overall.
53
complexity — bound on Push operations
• there are two types of Push operations
• the operation Push(/r, /7, v, w) is saturating push iff edge (\/, w) in the residual network becomes saturate, i.e. Q-(v, w) = 0 afterward
• otherwise the operation is nonsaturating push
54
The number of saturating pushes is at most 2nm.
• for any pair of vertices v, w 6 V, we will count the saturating pushes from v to w
• if there is such a push, (\/, w) is an edge of the residual network and h(v) = h(w) + 1
• in order for another push from v to w to occur later, h(w) must increase at leat by 2
• heights start at 0 and never exceed 2n — 1; the number of times any vertex can have its height increased by 2 is less than n
• for a network graph with m edges there can be up to 2m edges in the residual network, which gives the upper bound 2nm on the number of saturating pushes
55
The number of nonsaturating pushes is at most An (n + m).
• let us define a potential function as 0 = Y2V ef(v)>o Ku)
• initially, 0 = 0
• nonsaturating push decreases 0 by at least 1
• relabeling a vertex v increases 0 by less 2n, since the set over which the sum is taken is the same and the relabeling cannot increases v's height by more than its maximum possible height 2n-l
• saturating push from v to w increases 0 by less than 2n, since no heights change and only vertex w, whose height is at most 2n — 1, can possibly become overflowing
• the total amount of increase in 0 is less than 2n • 2n2 + 2n • 2nm = 4r?2(r? + m)
• since 0 > 0, the total amount of decrease, and therefore the total number of nonsaturating pushes, is less than 4r?2(r? + m)
56
During the execution of Generic-Push-Relabel on any flow network G — (V, E, c,s, t), the number of basic operations is
0{V2E).
• the push-relabel method allows to apply the basic operations in any order at all
• by choosing the order carefully and managing the network data structure efficiently, we can solve the maximum flow proglem faster than the 0(V2E) bound
• there is an implementation whose running time is 0(V3) which is asymptotically at least as good as 0(V2E), and even better for dense networks
57
Network Flows — Applications
Network Flows — Applications
Bipartite Matching
Section 7.5
7. Network Flow II
► bipartite matching
► disjoint paths
► extensions to max flow
► survey design
► airline scheduling
► image segmentation
► project selection
► baseball elimination
Matching
Def. Given an undirected graph G = (V,E), subset of edges MQE is a matching if each node appears in at most one edge in M.
Max matching. Given a graph G, find a max-cardinality matching.
Bipartite matching
Def. A graph G is bipartite if the nodes can be partitioned into two subsets L and R such that every edge connects a node in L with a node in R.
Bipartite matching. Given a bipartite graph G = (LU R,E), find a max-cardinality matching.
R
matching: 1-1', 2-2', 3-4', 4-5'
Bipartite matching: max-flow formulation
Formulation.
• Create digraph G = (L U R U {s, t}, E').
• Direct all edges from L to R, and assign infinite (or unit) capacity.
• Add unit-capacity edges from s to each node in L.
• Add unit-capacity edges from each node in R to t.
Max-flow formulation: proof of correctness
Theorem. 1-1 correspondence between matchings of cardinality k in G and integral flows of value k in G'.
Pf. => for each edge e:j{e) there exists a max flow/*in G' that is integral.
• 1-1 correspondence^ /* corresponds to max-cardinality matching. ■
Network flow II: quiz 1
What is running time of Ford-Fulkerson algorithms to find a max-cardinality matching in a bipartite graph with \L \ = \R \ = n?
A. 0(m + n)
B. 0(mn)
C. 0(mn2)
D. 0(m2n)
Network Flows — Applications
Disjoint Paths
Section 7.6
7. Network Flow II
► bipartite matching
► disjoint paths
► extensions to max flow
► survey design
► airline scheduling
► image segmentation
► project selection
► baseball elimination
Edge-disjoint paths
Def. Two paths are edge-disjoint if they have no edge in common.
Edge-disjoint paths problem. Given a digraph G = (V,£)and two nodes s and t, find the max number of edge-disjoint s-+t paths.
Ex. Communication networks.
Edge-disjoint paths
Def. Two paths are edge-disjoint if they have no edge in common.
Edge-disjoint paths problem. Given a digraph G = (V,£)and two nodes s and t, find the max number of edge-disjoint s-+t paths.
Ex. Communication networks.
Edge-disjoint paths
Max-flow formulation. Assign unit capacity to every edge.
Theorem. 1-1 correspondence between & edge-disjoint s-+t paths in G
and integral flows of value k in G'.
Pf.
• Let Px, ...,Pk be k edge-disjoint s-+t paths in G.
• Set /(e) =
1 edge e participates in some path Pj 0 otherwise
Since paths are edge-disjoint,/is a flow of value k.
27
Edge-disjoint paths
Max-flow formulation. Assign unit capacity to every edge.
Theorem. 1-1 correspondence between & edge-disjoint s~+t paths in G and integral flows of value k in G'. Pf. ^
• Let /be an integral flow in G' of value k.
• Consider edge (s, u) with f(s,u) = 1.
- by flow conservation, there exists an edge (u, v) with f(u, v) = 1
- continue until reach t, always choosing a new edge
• Produces k (not necessarily simple) edge-disjoint paths. ■
Edge-disjoint paths
Max-flow formulation. Assign unit capacity to every edge.
Theorem. 1-1 correspondence between & edge-disjoint s~+t paths in G and integral flows of value k in G'.
Corollary. Can solve edge-disjoint paths problem via max-flow formulation. Pf.
• Integrality theorem => there exists a max flow/*in G' that is integral.
• 1-1 correspondence^ /* corresponds to max number of edge-disjoint s-+t paths in G. ■
Network Flows — Applications
Multiple Sources and Sinks
Network flow II: quiz 4
Which extensions to max flow can be easily modeled?
A. Multiple sources and multiple sinks.
B. Undirected graphs.
C Lower bounds on edge flows. D. All of the above.
42
Multiple sources and sinks
Def. Given a digraph G = (V,E) with edge capacities c(»>0and multiple source nodes and multiple sink nodes, find max flow that can be sent from the source nodes to the sink nodes.
flow network G
43
Multiple sources and sinks: max-flow formulation
• Add a new source node s and sink node t.
• For each original source node si add edge (s,si) with capacity °o.
• For each original sink node tj, add edge (tj,i) with capacity °o.
Claim. 1-1 correspondence betweens flows in G and G'.
44
Network Flows — Applications
Circulations with Supplies and Demands
Circulation with supplies and demands
Def. Given a digraph G = (V,E) with edge capacities c(»>0and node demands d(v), a circulation is a function f(e) that satisfies:
• For each eGE: 0 < f(e) < c(e) (capacity)
• For each v£V: Yl ^(e) ~ Yl f(e) = d(v) (flow conservation)
e in to v e out of v
Circulation with supplies and demands: max-flow formulation
• Add new source s and sink t.
• For each v with d(v) < 0, add edge (s, v) with capacity -d(v).
• For each v with d(v) > 0, add edge (v, t) with capacity d(v).
Claim. G has circulation iff G' has max flow of value D - d^ =
Circulation with supplies and demands
Integrality theorem. If all capacities and demands are integers, and there exists a circulation, then there exists one that is integer-valued.
Pf. Follows from max-flow formulation + integrality theorem for max flow.
Theorem. Given (V,E,c,d), there does not exist a circulation iff there exists a node partition (A,B) such that 2vEfid(v) > cap(A,B).
Pf sketch. Look at min cut in G'.
demand by nodes in B exceeds supply of nodes in B plus max capacity of edges going from A to B
47
Circulation with supplies, demands, and lower bounds
Def. Given a digraph G = (V,E) with edge capacities c(e)>0, lower bounds 1(e) >0, and node demands d(v), a circulation f(e) is a function that satisfies: • For each eGE: [ 1(e) < f(e) J< c(e)
For each v e v: E ^(e)
(capacity)
(flow conservation)
e in to ^
e out of v
Circulation problem with lower bounds. Given (V,E,£,c,d), does there exist a feasible circulation?
48
Circulation with supplies, demands, and lower bounds
Max-flow formulation. Model lower bounds as circulation with demands.
• Send 1(e) units of flow along edge e.
• Update demands of both endpoints.
lower bound upper bound
^ \ / ^
0-[2, 9] -*0
d(v) d(w) flow network G
capacity
/
v
7
d(v) + 2
d(w) - 2
flow network G'
Theorem. There exists a circulation in G iff there exists a circulation in G'. Moreover, if all demands, capacities, and lower bounds in G are integers, then there exists a circulation in G that is integer-valued.
Pf sketch. f(e) is a circulation in G ifff'(e) = f(e) - 1(e) is a circulation in G'.
49
String Matching
STRING MATCHING
• exact string matching
• edit distance
• local and global alignment
• approximate matching
• indexing
EXACT STRING MATCHING
• strings over a finite alphabet Z
• given two strings, a text 7~[l..r?] and a pattern P[l..m], find the first substring (all substrings) of the text that is the same as the pattern
more formally
• for any shift s, let 7~s denote the substring 7~[s..s + m — 1]
• find the smallest shift (all shifts) s such that Ts — P (or report that there is none)
59
ALGORITHMS
algorithm preprocessing searching
brute force 0 0((n-m + l)m)
Karp Rabin 0(m) 0((n-m + l)m)
finite automata 0{m\Z\) 0(n)
Knuth Morris Pratt 0(m) 0(n)
Boyer Moore 0(m+ |E|) 0((n- m + l)m)
auerag"e complexity of algorithms Karp Rabin and Boyer Moore is much better than the given worst case complexity
60
BRUTE FORCE ALGORITHM
Algorithm: AlmostBruteForce(7~[l..r?], P[l..m])
for s <— 1 to n — m + 1 do equal <— True
/ • ■ + 10(p[2] + 10-p[1])...))
• we can compute £s+i in constant time (to make this we need to precompute the constant 10m_1)
ts+i = 10(ts - lO^77"1 • T[s]) + T[s + m]
63
Algorithm: NumberSearch(7[l..r?], P[l..m])
1 S <- 10"7"1
2 p^O
3 ti <- 0
4 for / «— 1 to rn do
5
6
p <- 10 • p + P[/] ti <- 10 • ti + T[i]
7 for s <— 1 to r? — m + 1 do
8
9
if p = fs then print s
ts+i 4- 10 • (ts - S • T[s]) + T[s + m]
complexity: the number of arithmetic operations, acting on numbers with up to m digits, is O(n)
64
KARP-RABIN FINGERPRINTING
Perform all arithmetic modulo some prime number q.
• choose q so that the value lOq fits into a standard integer variable
• values (p mod q) and (ts mod q) are called the fingerprints
• we can compute (p mod q) and (t\ mod q) in O(m) time p mod q =
P[m] + 10(P[m-l] + --- + 10-P[l] mod q)...) mod q
• similarly ts+i mod q
• if (p mod q) 7^ (ts mod q), then certainly P ^ Ts
• if (p mod q) — (ts mod q), we can't tell whether P = 7~s or not; we simply do a brute force comparison
• the overall running time is 0(n + Fm), where F is the number of false matches
• the expected number of false matches is 0(n/m)
65
Algorithm: KarpRabin(7[l..n], P[l..m])
1 q <— a random number between 2 and \m2 log m
2 S lO^"1
3 p^O
4 ti <- 0
5 for / «— 1 to m do
6
7
p «— (10 • p mod q) + P[i] mod q ti <— (10 • ti mod q) + 7"[/] mod q
8 for s «— 1 to r? — m + 1 do
9
10
11
if p = ts then
if P = T5 then print s
£s^i <- (10 • (^ - (S • 7~[s] mod q)) + T[s + m] mod q
66
String Matching
Finite State Machines and Knuth-Morris-Pratt Algorithm
FINITE STATE MACHINES
• for a given pattern P[l..m] construct a finite automaton A = ({0,...,m},Z,S,{0},{m})
• the transition function S for a state q and symbol x £ 51 is the length of the longest prefix of P[l..m] that is also a suffix of P[l..q]x
Function delta(P, Z) l for q <— 0 to m do
2
3
4
5
for x g 51 do
k ^- min(m + 1, q + 2) repeat /c <— k — 1 until P[l
(5(q,x) ^ /c
/c] is a suffix of P[l • • • q]x
6 return S
complexity of the preprocessing is in 0(m3|5I|)
there is an optimalized version with complexity 0(m|5I|)
67
Algorithm: Finite Automaton Matcher(7, A)
1 q
2 for /V 1 to n do
3 q <- 6(q, T[i])
4 if q — m then print / — m
complexity of string matching is in 0(r?) can we avoid the expensive preprocessing?
68
REDUNDANT COMPARISONS
• character-by-character comparison
• once we have found a match for a text character, we never need to do another comparison with that text character again
• the next reasonable shift is the smallest value of s such that T[s...i — 1], which is a suffix of the previously-read text, is also a proper prefix of the pattern
• KMP algorithm implements of both of these ideas through a special type of finite-state machines
69
KNUTH-MORRIS-PRATT ALGORITHM (KMP)
• every state in the string-matching machine is labeled with a character from the pattern, except two special states labeled S and F
• each state has two outging edges, a success edge and a failure edge
• the success edges define a path through the characters of the pattern in order, starting at S and ending at F
• failure edges always point to earlier characters in the pattern
we use the finite state machine to search for the pattern as follows
• at all times, we have a current text character T[i] and a current state of the machine, which is usually labeled by some pattern character P\j)
• 'f = P[y], or the current label is S, follow the success edge to the next state and increment /
• 'f P[y], follow the failure edge back to an earlier state, but do not change / 70
KMP — implementation
in a real implementation we need only the failure function encoded in an array fail[l..m]
Algorithm: KnuthMorrisPratt(7[l..n], P[l..m])
1 ComputeFailure(P[l..m])
2 j <— 1
3 for i' ^r- 1 to n do
4
5
6
7
8
while j > 0 and T[i] ^ P\j] do
j 0 and P[i] ^ P\j] do
73
KMP — complexity of the failure function
• just as we did for KMP, we can analyze ComputeFailure by amortizing character mismatches againgst eralier character matches
• since there are at most m character matches, ComputeFailure runs in O(m) time
74
String Matching
Boyer Moore Algorithm
Can we improve on the naTve algorithm?
P: word
T: There would have been a time for such a word .......word..........................................>
u doesn't occur in P, so skip next two alignments
P: word
T: There would have been a time for such a word
■.......word ..........................................>
word skip!
word skip! word
Boyer-Moore
Learn from character comparisons to skip pointless alignments
1. When we hit a mismatch, move P along until
the mismatch becomes a match "Bad character rule"
2. When we move P along, make sure characters that matched in the last alignment also match in the next alignment
3. Try alignments in one direction, but do character comparisons in opposite direction
"Good suffix rule" For longer skips
P; word
T: There would have been a time for such a word ........word.........................................>
Boyer, RS and Moore, JS. "A fast string searching algorithm." Communications of the ACM 20.10 (1977): 762-772.
Boyer-Moore: Bad character rule
Upon mismatch, skip alignments until (a) mismatch becomes a match, or (b) P moves past mismatched character, (c) If there was no mismatch, don't skip
Step 1 • T: GCT T®T GCTACCTTTTGCGCGCGCGCGGAA P: C(E)TJ^TTGC cose(o)
S 2. T: GCTTCTGC T@C CTTTTGCGCGCGCGCGGAA P ' P- TiCCTTTTGC Cose(b)
Step 3:
Step 4:
(etc)
T: GCTTCTGCTACCTTTTGCGCGCGCGCGGAA
P; CCTTTTGC Case(c)
<....................
T: GCTTCTGCTACCTTTTGCGCGCGCGCGGAA p: CCTTTTGC
Boyer-Moore: Bad character rule
Step 1:
Step 2:
Step 3:
T: G C T f C TG Cj
P: CCTT TTGCJ
T: GCTT C TG C|
P: C C TTTJ
T: GCTT CTGC!
P:
TiA CCTTTTGCGCGCGCGCGGAA
t;accttttgcgcgcgcgcggaa
t!g C
TiA CCTTTTGCGCGCGCGCGGAA i CCTTTTGC
tt tttttt
Up to step 3, we skipped 8 alignments
5 characters in Twere never looked at
Boyer-Moore: Good suffix rule
Let t = substring matched by inner loop; skip until (a) there are no mismatches between P and t or (b) P moves past t
t
CGTGC CfTXejTTACTTACTTACTTACGCG A A
c t(ta^ttA c
CGTGC C(T A C T T A^T TACTTACTTACG C G A A (CTTAC)TTAC
Step 1:
T: P:
Step 2:
7:
Step 3:
T: P:
CGTGCCTACTTACTTACTTACTTACGCGAA
CTTACTTAC
Boyer-Moore: Good suffix rule
Let t = substring matched by inner loop; skip until (a) there are no mismatches between P and t or (b) P moves past t
Step 3:
t
S 1. T: C G T G C C(TAT)T TACTTACTTACTTACGCGAA P; C T(TA"C)T T A C
t occurs in its entirety to the left within P
Ste 2- T: CGTGCCP"ACTTAqTTACTTACTTACGCGAA eP ' P; (CTTAC)TTAC
prefix of P matches a si/ffix of f
I* CGTGCCTACTTACTTACTTACTTACGCGAA P: CTTACTTAC
Case (a) has two subcases according to whether t occurs in its entirety to the left within P (as in step 1), or a prefix of P matches a suffix of t (as in step 2)
Boyer-Moore: Putting it together
How to combine bad character and good suffix rules?
T: GTTATAGCTGA tQg CGGCCjTAGCGGCGAA
bad char says skip 2, good suffix says skip 7
Take the maximum! (7)
Boyer-Moore: Putting it together
Use bad character or good suffix rule, whichever skips more
Step 1 ■ ^'GTTATAG C®G ATCGCGGCGTAGCGGCGAA
P: GQAGC G G CjG be: 6, gs:0 bad character
Step 2- T: GTTATAGCT G A TQpT5)G CGTAGCGGCGAA
P: G T A@[C]G)G C G be: 0, gs: 2 good suffix
Step 3- T: GTTATAGCTGAT^CGGCG)TAGCGGCGAA
P:
TAGCGGCG
be: 2, gs:7 good suffix
Step 4:
T: P:
GTTATAGCTGATCGCGGCGTAGCGGCGAA
GTAGCGGCG
Step 1:
Step 2:
11 characters of Twe ignored
T: GTTATAGCTGATCGCGGCGTAGCGGCGAA
P: GTAGCGGCG
T: GTTATAGCTGATCGCGGCGTAGCGGCGAA
P: GTAGCGGCG
Step 3:
Step 4:
T: GTTATAGCTGATCGCGGCGTAGCGGCGAA P: GTAGCGGCG
T: GTTATAGCTGATCGCGGCGTAGCGGCGAA p: GTAGCGGCG ■■■■■■ ■■ □ □□□[]■■
Skipped 15 alignments
B oyer-Mo ore: Preprocessing
Pre-calculate skips for all possible mismatch scenarios! For bad character rule and P = TCGC:
P
I
T c G c
A
c - -
G -
T -
Boyer-Moore: Preprocessing
Pre-calculate skips for all possible mismatch scenarios! For bad character rule and P = TCGC:
T c c
A 0 1 2 3
C 0 - 0 -
G 0 1 - 0
CD - 0 CD 2
T: A A(T)C A A T A G C P: Cf)C (GlC
This can be constructed efficiently. See Gusfield 2.2.2.
Boyer-Moore: Good suffix rule
We learned the weak good suffix rule; there is also a strong good suffix rule
f
T: CTTGCCTACTTACTTACT
P; CTTAC
Weak: C
T T
T AC
TACTTAC
guaranteed Stron9: CTTACTTAC
mismatch!
Strong good suffix rule skips more than weak, at no additional penalty
Strong rule is needed for proof of Boyer-Moore's 0(n + m) worst-case time. Gusfield discusses proof(s) in first several sections of ch. 3
Boyer-Moore: Worst case
Boyer-Moore, with refinements in Gusfield, is 0(n + m) time Given n < m, can simplify to O(m)
Is this better than naive?
For naive, worst-case # char comparisons is n{m - n + 1) Boyer-Moore: O(m), naive: 0{nm)
Reminder: |P| = n \T\ = m
Boyer-Moore: Best case
What's the best case?
P: bbbb
T: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbb bbbb bbbb bbbb bbbb bbbb bbbb bbbb bbbb bbbb bbbb
Every alignment yields immediate mismatch and bad character rule skips n alignments
How many character comparisons?
floor(m I n)
Naive vs B oyer-Mo ore
As m & n grow, # characters comparisons grows with...
\P\ = n
\J] = m Naive matching Boyer-Moore
Worst case rrvn m
Best case m m / n
Performance comparison
Simple Python implementations of naive and Boyer-Moore:
Naive matching Boyer-Moore
# character comparisons wall clock time # character comparisons wall clock time
P: "tomorrow" T: Shakespeare's complete works 5,906,125 2.90 s 785,855 1.54 s
P: 50 nt string from Alu repeat* T: Human reference (hg19) chromosome 1 307,013,905 137 s 32,495,111 55 s
17 matches I 71 = 5.59 M
336 matches I T\ = 249M
GCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGG
Boyer-Moore implementation
http://j.mp/CG_BoyerMoore
def boyer_moore(p, p_bm, t):
""" Do Boyer-Moore matching """ i = 0
occurrences = []
while i < len(t) - len(p) +1: # left to right shift = 1
mismatched = False_
for j in range(len(p)-l, -1, -1): # right to left if p[j] != t[i+j]:
skip_bc = p_bm.bad_character_rule(t[i+j]) skip_gs = p_bm.good_suffix_rule(j) shift = max(shift, skip_bc, skip_gs) mismatched = True
_break_
if not mismatched:
occurrences.append(i) skip_gs = p_bm.match_skip() shift = max(shift, skip_gs) i += shift return occurrences
Preprocessing: Boyer-Moore
Make lookup tables for bad character & good suffix rules
Boyer-Moore
> f
Results
Preprocessing: NaTve algorithm
Results
Preprocessing: Boyer-Moore
Preprocessing: trade one-time cost for reduced work overall via reuse
Boyer-Moore preprocesses P into lookup tables that are reused
reused for each alignment of P to Ti
If you later give me T2,1 reuse the tables to match Pto T2
If you later give me T3,1 reuse the tables to match Pto T3
• • •
Cost of preprocessing is amortized over alignments & texts