02 Algorithm Analysis
02 Algorithm Analysis
A LGORITHM A NALYSIS
‣ Computational Tractability
‣ asymptotic order of growth
‣ implementing Gale–Shapley survey of common running times
‣
2. A LGORITHM A
NALYSIS
computational tractability
asymptotic order of growth
implementing Gale–Shapley survey of common running times
A strikingly modern thought
Analytic Engine
3
Models of computation: Turing machines
assume w ≥ log2 n
Word RAM.
・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, …
program a[i] i
.
.
.
output …
Caveat. At times, need more refined model (e.g., multiplying n-bit integers). 5
Brute force
Brute force. For many nontrivial problems, there is a natural brute-force search
algorithm that checks every possible solution.
・ Typically takes 2 steps (or worse) for inputs of size n.
n
・Unacceptable in practice.
Ex. Stable matching problem: test all n! perfect matchings for stability. 6
Polynomial running time
Desirable scaling property. When the input size doubles, the algorithm should slow
down by at most some multiplicative constant factor C.
7
Polynomial running time
Abstract
Chen, Grigni, andAbstractPapadimitriou (WADS’97 and
Chen, Grigni, and Papadimitriou (WADS’97 and STOC’98)
STOC’98) have introduced a modified notion of planarity,
where two faces are considered haveadjacent
introduced a modified
if they share atnotion of planarity, where two
least one point.
The corresponding faces are
abstract graphs are considered
called mapadjacent if they share at least one point.
Thewhether
corresponding abstract Figure 1. Large
graphs.
Chen et.al. raised the question of map graphs can graphs are called map graphs.
Exceptions. Some exponential-time algorithms are used widely in practice because the
worst-case instances don’t arise.
9
Other types of analyses
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 O(n) primitive
computational steps using a resizing array.
SECTION 2.2
Big O notation
Upper bounds. f (n) is O(g(n)) if there exist constants c > 0 and n0 ≥ 0 such that
0 ≤ f (n) ≤ c · g (n) for all n ≥ n0.
c · g(n)
・ f (n) is O(n ).
2
choose c = 50, n = 1
0
2
Typical usage. Insertion sort makes O(n ) compares to sort n elements.
12
Analysis of algorithms: quiz 1
C. Both A and B.
D. Neither A nor B.
13
Big O notational abuses
One-way “equality.” O(g(n)) is a set of functions, but computer scientists often write f
(n) = O(g(n)) instead of f (n) ∈ O(g(n)).
14
Big O notation: properties
Reflexivity. f is O( f ).
Lower bounds. f (n) is Ω(g(n)) if there exist constants c > 0 and n0 ≥ 0 such that
f (n) ≥ c · g(n) ≥ 0 for all n ≥ n0.
f (n)
Typical usage. Any compare-based sorting algorithm requires Ω(n log n) compares in
the worst case.
16
Analysis of algorithms: quiz 2
B. f (n) is Ω(g(n)) if there exists a constant c > 0 such that f (n) ≥ c · g(n) ≥ 0 for
infinitely many n.
C. Both A and B.
D. Neither A nor B.
17
Big Theta notation
Tight bounds. f (n) is Θ (g(n)) if there exist constants c1 such > 0, c2 > 0, and n0 ≥ 0
that 0 ≤ c1 · g(n) ≤ f (n) ≤ · g(n) for all n ≥ n0. c2 · g(n)
c2
f (n)
Ex. f (n) = 32n2 + 17n + 1. c1 · g(n)
・ f (n) is Θ (n2 ). choose c = 32, c = 50, n = 1
1 2 0
between ½ n log 2 n
and n log2 n
19
Analysis of algorithms: quiz 3
f (n)
B. f (n) is Θ (g(n)) if lim = c >for0 some constant 0 < c < ∞.
n g
(n)
C. Both A and B.
2n n
f (n) =
3n n
g(n) = n
f (n)
Proposition. If lim = c >for0 some constant 0 < c < ∞ then f (n) is Θ (g(n)).
n g
(n)
Pf.
・By definition of the limit, for any ε > 0, there exists n0 such that
f (n)
c c+
g
(n)
for all n ≥ n0.
・ Choose ε = ½ c > 0.
・Multiplying by g(n) yields 1/2 c · g(n) ≤ f (n) ≤ 3/2 c · g(n) for all n ≥ n0.
・Thus, f (n) is Θ (g(n)) by definition, with c1 = 1/2 c and c2 = 3/2 c. ▪
f (n)
Proposition. If lim = 0 , then f (n) is O(g(n)) but not Ω(g(n)).
n g
(n)
f (n)
Proposition. If lim = , then f (n) is Ω(g(n)) but not O(g(n)).
n g
(n) 21
Asymptotic bounds for some common functions
d d
Polynomials. Let f (n) = a0 + a1 n + … + ad n with ad > 0. Then, f (n) is Θ (n ).
Pf. d
a0 + a 1 n + . . . + a d
lim n = ad > 0
n nd
d
Logarithms and polynomials. loga n is O(n ) for every a > 1 and every d > 0.
Pf.
lim log da = 0
n nn
d ) for every
Exponentials and polynomials. n is O(r n r > 1 and every d > 0.
Pf. nd
lim n = 0
n r
Θ (n log n).
Factorials. n! is 2 Pf.
Stirling’s formula: n n
n! 2 n
e 22
Big O notation with multiple variables
Upper bounds. f (m, n) is O(g(m, n)) if there exist constants c > 0, m0 ≥ 0, and n0 ≥
0 such that 0 ≤ f (m, n) ≤ c · g (m, n) for all n ≥ n0 and m ≥ m0.
Typical usage. In the worst case, breadth-first search takes O(m + n) time to find a
shortest path from s to t in a digraph with n nodes and m edges.
23
2. A LGORITHM A
NALYSIS
‣ computational tractability
‣ asymptotic order of growth
‣ implementing Gale–Shapley
‣ survey of common running
times
SECTION 2.3
Efficient implementation
25
Efficient implementation
26
Data representation: making a proposal
next proposal to
hospital h 3 4 1 5 2 null
least favorite
favorite
pref[]
1 st 2 nd 3 rd 4 th 5 th 6 th 7 th 8 th
8 3 7 1 4 5 6 2
student prefers hospital 4 to 6 since
rank[4] < rank[6]
1 2 3 4 5 6 7 8
rank[]
4 th 8 th 2 nd 5 th 6 th 7 th 3 rd 1 st
for i = 1 to n rank[pref[i]] = i
2
Bottom line. After Θ (n ) preprocessing time (to create the n ranking arrays), it takes O(1)
time to accept/reject a proposal. 28
Stable matching: summary
2
Theorem. Can implement Gale–Shapley to run in O(n ) time.
Pf.
・ Θ (n2 ) preprocessing time to create the n ranking arrays. ・ There are O(n )
2 ▪
proposals; processing each proposal takes O(1) time.
Theorem. In the worst case, any algorithm to find a stable matching must query the
2
hospital’s preference list Ω(n ) times.
29
2. A LGORITHM A
NALYSIS
‣ computational tractability
‣ asymptotic order of growth
‣ implementing Gale–Shapley
‣ survey of common running
times
SECTION 2.4
Constant time
bounded by a constant,
Examples. which does not depend on input size n
・ Conditional branch.
・Arithmetic/logic operation.
・Declare/initialize a variable.
・Follow a link in a linked list.
・Access element i in an array.
・Compare/exchange two elements in an array.
・…
31
Linear time
Merge two sorted lists. Combine two sorted linked lists A = a1, a2, …, an and B = b1, b2,
…, bn into a sorted whole.
i ← 1; j ← 1.
WHILE (both lists are nonempty)
IF (ai ≤ bj) append ai to output list and increment i.
ELSE append bj to output list and increment j.
Append remaining elements from nonempty list to output list.
32
TARGET SUM
TARGET-SUM. Given a sorted array of n distinct integers and an integer T, find two
that sum to exactly T ?
input
−20 10 20 30 35 40 60 70 T = 60
(sorted)
i j
33
Logarithmic time
Search in a sorted array. Given a sorted array A of n distinct integers and an integer x, find
index of x in array.
remaining elements
O(log n) algorithm. Binary search.
・Invariant: If x is in the array, then x is in A[lo .. hi]. After k
・iterations of WHILE loop, (hi − lo + 1) ≤ n / 2 k k ≤ 1 + log2 n.
lo ← 1; hi ← n.
WHILE (lo ≤ hi)
mid ← ⎣(lo + hi) / 2⎦.
IF (x < A[mid]) hi ← mid − 1.
O(log n)
https://www.facebook.com/pg/npcompleteteens
36
SEARCH IN A SORTED ROTATED ARRAY
35
90
50
85
60
80
75 65
67
80 85 90 95 20 30 35 50 60 65 67 75
1 2 3 4 5 6 7 8 9 10 11 12
37
Linearithmic time
Linearithmic time. Running time is O(n log n).
40
Quadratic time
Closest pair of points. Given a list of n points in the plane (x1, y1), …, (xn, yn), find the
pair that is closest to each other.
min ← ∞.
FOR i = 1 TO n
FOR j = i + 1 TO n
d ← (xi − xj)2 + (yi − 2
yj) .
IF (d < min)
min ← d.
Remark. Ω(n )2seems inevitable, but this is just an illusion. [see §5.4]
42
Cubic time
FOR i = 1 TO n
FOR j = i + 1 TO n
FOR k = j + 1 TO n
IF (ai + aj + ak = 0)
RETURN (ai, aj, ak).
O(n2) algorithm.
44
Polynomial time
k
Polynomial time. Running time is O(n ) for some constant k > 0.
Independent set of size k. Given a graph, find k nodes such that no two are joined by
an edge.
k is a constant
k
Exponential time. Running time is O(2 ) for nsome constant k > 0.
S* ← ∅.
FOREACH subset S of n nodes:
Check whether S is an independent set. IF
(S is an independent set and ⎢S⎟ > ⎢S*⎟)
S* ← S. independent set of max cardinality
RETURN S*.
47
Exponential time
k
Exponential time. Running time is O(2 ) for nsome constant k > 0.
Euclidean TSP. Given n points in the plane, find a tour of minimum length. O(n � n!)
π* ←
∅.
FOREACH permutation π of n points:
Compute length of tour corresponding to π.
IF (length(π) < length(π*))
π* ← π.
for simplicity, we’ll assume Euclidean
RETURN π*. distances are rounded to nearest integer (to avoid
issues with infinite precision)
48
Analysis of algorithms: quiz 4
A. O( 2n doesn’t include 3 n
C. Both A and B.
D. Neither A nor B.
49