2 - 2 - Analysis of Algorithm. Analysis of Algorithm
2 - 2 - Analysis of Algorithm. Analysis of Algorithm
ALGORITHM ANALYSIS
Running Time Analysis
• Mathematical analysis of worst-case running time of an algorithm as
function of input size. Why these choices?
• Mathematical: describes the algorithm. Avoids hard-to-control
experimental factors (CPU, programming language, quality of
implementation), while still being predictive.
• Worst-case: just works. (“average case” appealing, but hard to
analyze)
• Function of input size: allows predictions. What will happen on a new
input?
Efficiency
• When is an algorithm efficient?
• Stable Matching Brute force: Ω(n!)
• Propose-and-Reject?: O(n2)
• We must have done something clever
• Question: Is it Ω(n2) ?
Efficient implementation
stack).
・ Can find an unmatched hospital in O(1) time.
26
Data representation: making a proposal
next proposal
to
hospital h 3 4 1 5 2 null
favorit least
e favorite
hospitals.
1 2 3 4 5 6 7 8
rank[]
for i = 1 to n
rank[pref[i]] = i
Bottom line. After Θ(n2 ) preprocessing time (to create the n ranking
arrays), it takes O(1) time to accept/reject a proposal. 28
Stable matching:
summary
29
Survey of common running times
Constant time
・ Arithmetic/logic operation.
・ Declare/initialize a
variable.
・ Access
Follow aelement in an array.
link in ai linked list.
・ 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.
32
Append remaining elements from nonempty list to
TARGET
S UM
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
( s orted)
i j
33
Logarithmic time
remaining
O(log n) algorithm. Binary elements
hi]. ‹ k ≤ 1 + log2 n.
・ After k iterations of WHILE loop, (hi − lo + 1) ≤ n / 2k
lo 1; hi n.
WHILE (lo ≤ hi)
mid ⎣(lo +
hi) / 2⎦.
IF (x < A[mid]) hi mid − 1.
ELSE IF (x > A[mid]) lo mid + 1.
ELSE RETURN mid.
RETURN −1. 35
Logarithmic time
O(log n)
h t t p s : / / w w w. f a c e b o o k . c o m / p g / n p c o m p l e t e t e e n s
S EARCH IN A
SORTED ROTATED ARRAY
s o r t e d circular array
20 30
95
35
90
50
85
60
80
65
75 67
s o r t e d rotated array
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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
O(n log n) algorithm. Mergesort. M E R G E S O R T E X A M P L E
E M R G E S O R T E X A M P L E
E MG R E S O R T E X A M P L E
E GM R E S O R T E X A M P L E
E GM R E S O R T E X A M P L E
E GM R E S O R T E X A M P L E
E GM R E O R S T E X A M P L E
E E G M O R R S T E X A M P L E
E E G M O R R S E T X A M P L E
E E G M O R R S E T A X M P L E
E E G M O R R S A E T X M P L E
E E G M O R R S A E T X M P L E
E E G M O R R S A E T X M P E L
E E G M O R R S A E T X E L M P
E E G M O R R S A E E L M P T X39
LARGEST EMPTY INTERVAL
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 −
Remark. Ω(n ) seems inevitable,
2
yj)2. but this is just an illusion. [see
§5.4]
IF 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).
Remark. Ω(n3) seems inevitable, but O(n2) is not hard. [see next
slide]
43
3-
S UM
O(n2) algorithm.
44
Polynomial time
S* .
FOREACH subset S of n nodes:
Check whether S is an independent set.
IF (S is an independent set and ⎢S⎟ >
⎢S*⎟)
i nde pe nde nt s e t o f ma x cardinality
S*
S. RETURN
S*.
47
Exponential time
π* .
FOREACH permutation π of n points:
Compute length of tour corresponding to π.
IF (length(π) < length(π*))
π* π.
for simplicity, we’ll assume
RETURN π*. Euclidean
distances are rounded to nearest
integer (to avoid issues with infinite
precision)
48
Analysis of algorithms: quiz 4
B. O(2cn) for some constant c > 0. includes 3n but doesn’t include n! = 2Θ(n log n)
C. Both A and B.
D. Neither A nor B.
49