Computational Tractability Asymptotic Order of Growth Implementing Gale-Shapley Survey of Common Running
Computational Tractability Asymptotic Order of Growth Implementing Gale-Shapley Survey of Common Running
ALGORITHM
‣ computational tractability
ANALYSIS
‣ asymptotic order of growth
‣ implementing Gale–Shapley
‣ survey of common running
times
• What is an algorithm?
• A systematic approach
• Efficiency in terms of
• Computation
• space
• What is algorithm analysis??
• Different alternatives???
• Sorting
• Buble sort, merge sort, etc.
• Which one is efficient?
Basics of Algorithm Analysis
• Efficiency????
• (1)An algorithm is efficient if, when implemented, it runs quickly
on real input instances
• Where and how well, it is implemented
• What is Real input instance,
• The scale of the problem to unexpected range
• (2) efficiency that is platform-independent, instance-
independent, and of predictive value with respect to increasing
input sizes
• “size” parameter N; input size, the
function is represented suppressing the
rest of the details
• Stable Matching problem
• N is the preference lists
• 2n preference lists, each is of size n, N = 2n*n= 2n
Worst-Case Running Times and Brute-Force
Search
• Polynomials
• f (n) = a0 + a1 n + a2 n2 + . . .+ ad nd for some integer constant d > 0, where the final coefficient a d
> 0 is nonzero
• d is degree
• their asymptotic rate of growth is determined by their “high-order term”, i.e. degree
• polynomials raise n to a fixed exponent
• E.g. T(n) = pn2 + qn + r
• (2.7) Let f be a polynomial of degree d, in which the coefficient a d is positive. Then f = O(nd).
• Proof. We write f = a0 + a1 n + a2 n2 + . . .+ ad nd, where ad > 0. The upper bound is a direct
application of (2.5). First, notice that coefficients aj for j < d may be negative, but in any case we
have ajnj ≤ |aj|nd for all n ≥ 1. Thus each term in the polynomial is O(nd). Since f is a sum of a
constant number of functions, each of which is O(nd), it follows from (2.5) that f is O(nd).
Asymptotic Bounds for Some Common Functions
• Polynomials
• Using O(·) notation, it’s easy to formally define polynomial time: a
polynomial-time algorithm is one whose running time T(n) is O(n d) for some
constant d, where d is independent of the input size.
• d may not an integer
• O(n1.59)
• O(n1/2).
• O(n log n). log n ≤ n for all n ≥ 1, and hence n log n ≤ n2 for all n ≥ 1. In other words, if
an algorithm has running time O(n log n), then it also has running time O(n2), and so it
is a polynomial-time algorithm
Asymptotic Bounds for Some Common Functions
• Logarithms
• logb n =x >> bx=n
• logarithms are very slowly growing functions
• if we round it down to the nearest integer, it is one less than the number of digits in
the base-b representation of the number n.
• for example, 1+ log2 n, rounded down, is the number of bits needed to represent n.)
• (2.8) For every b > 1 and every x > 0, we have logb n = O(nx).
• >> , O(log n), base is not important
Asymptotic Bounds for Some Common Functions
• Exponentials
• f (n) = rn for some constant base r.
• r > 1, a very fast-growing function.
• Exponentials raise a fixed number to n as a power
• (2.9) For every r > 1 and every d > 0, we have nd = O(rn).
• people write “The running time of this algorithm is exponential,”
Without specifying which exponential function they have in mind
What does it mean?
Implementing the Stable Matching Algorithm
Using Lists and Arrays
• What is an algorithm?
• A systematic approach
• Efficiency in terms of
• Computation, number of steps, stored-program concept, data structure for storing the data…
• Space
Implementing the Stable Matching Algorithm
Using Lists and Arrays (Cont…)
• Array
Implementing the Stable Matching Algorithm
Using Lists and Arrays (Cont…)
• Every man and woman
preference list
• Free men/women
• Engaged pair
• For above, need data
structure
• Preprocessing data for data
structure choice
• An array is less good for
dynamically maintaining a list
of elements that changes
over time, such as the list of
free men in the S-M algori
• dynamic set of elements is
• via a linked list
• doubly linked list
• See next slide for deletion
• Linked list cannot find the ith
element of the list in O(1)
time
Implementing the Stable Matching Algorithm
Using Lists and Arrays (Cont…)
• Implementing the Stable
Matching Algorithm
• O(n2) for steps
• we need to be able to
implement each iteration
in constant time
• M and W
• Size n
• Ordered
• Associated number i, mi, wi
• ManPref[m, i] to denote
the ith woman on man
m’s preference list,
• WomanPref[w, i]
• What data structure for
each of the above and
why?
Implementing the Stable Matching Algorithm Using Lists and Arrays
(Cont…)
1. We need to be able to
identify a free man
i. as a linked list
ii. First free man
iii. delete m from the list if he becomes engaged
iv. m’ can be inserted at the front of the list,
v. in constant time.
vi. What if it was array????
2. We need, for a man m, to
be able to identify the
highest-ranked woman to
whom he has not yet
proposed.
i. maintain an extra array Next that indicates for
each man m the position of the next woman he
will propose to on his list.
ii. initialize Next[m]= 1
iii. If a man m needs to propose to a woman, he’ll
propose to w = ManPref[m, Next[m]], and
iv. once he proposes to w, we increment the value of
Next[m] by one, regardless of whether or not w
accepts the proposal.
Implementing the Stable Matching Algorithm Using Lists and Arrays
(Cont…)
• For a woman w, we need to
decide if w is currently
engaged, and if she is, we
need to identify her current
partner.
• maintaining an array Current of length n, where
Current[w] is the woman w’s current partner m’.
• set Current[w] to a special null symbol when we need to
indicate that woman w is not currently engaged; at the
start of thealgorithm,
• Current[w] is initialized to this null symbol for all women
w.
• For a woman w and two men
m and m’, we need to be
able to decide, again in
constant time, which of m or
m’ is preferred by w.
• Keeping the women’s preferences in an array
WomanPref, analogous to the one we used for men,
does not work,
• O(n) to find m and m’ in WomanPref array
we create an n × n array
Ranking, where Ranking[w,m]
contains the rank of man m in
the sorted order of w’s
preferences.
By a single pass through w’s
preference list, we can create
this array in
linear time for each woman,
for a total initial time
investment proportional to
n2.
Then, to decide which of m or
m’ is preferred by w, we
simply compare
the values Ranking[w,m] and
Ranking[w,m’].
This allows us to execute step
(4) in constant time, and
2.4 A Survey of Common Running Times
• Linear Time
• O(n)
• its running time is at most a
constant factor times the size of
the input
• to process the input
• in a single pass,
• spending a constant amount of
time on each item of input
encountered.
2.4 A Survey of Common Running Times (cont…)
• Linear Time
• Merging Two
Sorted Lists,
Figure 2.2
• Another way,
we could just
throw the two
lists together,
ignore the fact
that they’re
separately
arranged in
ascending order,
• and run a sorting algorithm,
• Which one is good????
2.4 A Survey of Common Running Times (cont…)
• Quadratic Time
• Suppose, a plane to find the closest two points
• Bruteforce solution
• Enumerate all the pairs
• compute the distance between each pair
• Smallest distance pair is the answer
• The number of pairs of points = O(n2 )
• It results due to
• performing a search over all pairs of input items and spending constant time per pair.
• nested loops
2.4 A Survey of Common Running Times (cont…)
• Cubic Time
• O(n3) time
2.
ALGORITHM
‣ computational tractability
ANALYSIS
‣ asymptotic order of growth
‣ implementing Gale–Shapley
‣ survey of common running
times
SECTION 2.1
A strikingly modern thought
Analytic Engine
3
9
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 …
Ex. Stable matching problem: test all n! perfect matchings for stability. 42
Polynomial running time
Desirable scaling property. When the input size doubles, the algorithm
should slow down by at most some multiplicative constant factor C.
Abstract
44
Worst-case analysis
45
Other types of analyses
SECTION 2.2
Big O notation
・ f (n) is
・ 2).f (n) is neither O(n) nor O(n log
O(n
n). n0 n
12
Analysis of algorithms: quiz 1
Let f(n) = 3n2 + 17 n log2 n + 1000. Which of the following are true?
O(n3).
C. Both A and B.
D. Neither A nor B.
13
Big O notational abuses
・ Or restrict to a
ℝ. plotting, limits, calculus
subset.
50
Big O notation: properties
Reflexivity. f is
O( f ).
Constants. If f is O(g) and c > 0, then c f is O(g).
n0
Ω(n).
・ f (n) is not Ω(n3).
n0 n
52
Analysis of algorithms: quiz 2
B. f (n) is Ω(g(n)) iff 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 > 0, c2 > 0, and n0 ≥
0
c2 · g(n)
such that 0 ≤ c1 · g(n) ≤ f (n) ≤ c2 · g(n) for all n ≥ n0.
f
(n)
Ex. f (n) = 32n2 + 17n + c1 · g(n)
1. choose c1 = 32, c2 = 50, n0 = 1
・ f (n) is
・(n2).f (n) is neither Θ (n) nor
Θ
Θ (n3). n0 n
between ½ n log2 n
and n log2 n
19
Analysis of algorithms: quiz 3
f
B. f (n) is Θ (g(n)) lim = for some constant 0 < c < ∞.
n→ ∞ (n)
g(n) c
iff
C. Both A and B.
2n i7 n ib
f (n) =
2p2M
3n i7 n ib
g(n) = n Q//
Proposition. If lim
f
n→∞ g(n) = c for some constant 0 < c < ∞ then f (n) is
(n)
Θ (g(n)).
Pf.
・ By definition of the limit, for any ε > 0, there exists n0 such
that
f
c—ϵ ≤ ≤ c+
(n)
g(n)
ϵ
for all n ≥ n0.
・ Choose ε = ½ c > 0.
1/2 c · g(n) ≤ f (n) ≤ 3/2 c · g(n) for all n ≥ n0.
・ Multiplying by g(n)
・ Thus, f (n) is Θ(g(n)) by definition, with c1 = 1/2 c and c2 = 3/2 c. ▪
yields
Proposition. If lim f
n → ∞ (n)
g(n) = 0 , then f (n) is O(g(n)) but not
Ω(g(n)).
Proposition. If lim f
n → ∞ (n)
g(n) = ∞ , then f (n) is Ω(g(n)) but not
O(g(n)). 21
Asymptotic bounds for some common functions
Logarithms and polynomials. loga n is O(nd ) for every a > 1 and every d >
Pf. 0.
lim log a n =
n→ ∞ nd
0
Exponentials and polynomials. n d is O(rn ) for every r > 1 and every d > 0.
Pf. lim nd = 0
n
n→ ∞ r
23
2.
ALGORITHM
‣ computational tractability
ANALYSIS
‣ asymptotic order of growth
‣ implementing Gale–Shapley
‣ survey of common running
times
SECTION 2.3
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
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
Theorem. Can implement Gale–Shapley to run in O(n2)
time. Pf.
・ Θ(n2) preprocessing time to create the n ranking
arrays.
・ There are O(n2) proposals; processing each proposal
takes O(1) time.
29
2.
ALGORITHM
‣ computational tractability
ANALYSIS
‣ 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.
・ Access
Follow aelement
link in ai in an array.
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
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
remaining elements
O(log n) algorithm. Binary search.
・ Invariant: If x is in the array, then x is in A[lo ..
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)
https://www.facebook.com/pg/npcompleteteens
SEARCH IN A SORTED ROTATED
ARRAY
35
90
50
85
60
80
65
75 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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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 M G R E S O R T E X A M P L E
E G M R E S O R T E X A M P L E
E G M R E S O R T E X A M P L E
E G M R E S O R T E X A M P L E
E G M 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 X
A E E E E G L M M O P R R S T X
39
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. Ω(n2) seems inevitable,
− yj)2. but this is just an illusion. [see §5.4]
IF (d < 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-
SUM
3-SUM. Given an array of n distinct integers, find three that sum to
0.
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*)
independent set of max 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 Euclidean
RETURN π*. distances are rounded to nearest integer
(to avoid issues with infinite precision)
48
Analysis of algorithms: quiz 4
A. doesn’t include 3n
O( 2n)
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