View
242
Download
0
Category
Preview:
Citation preview
Recurrence relation
• A recurrence relation is an equation that recursively defines
a sequence: each term of the sequence is defined as a
function of the preceding term(s)
• For instance 2 n=1
f(n) =
f(n-1)+n n>1
2
def bugs(n):
if n <= 1:
do_something()
else:
bugs(n-1)
bugs(n-2)
for i in range(n):
do_something_else()
5
Example
1
2
if 1( )
( 1) ( 2) otherwise
c nT n
T n T n nc
def yosemite(n):
if n == 1:
do_something()
else:
for i in range(1,n-1):
yosemite(i)
do_something_different()
8
Example
1
1
2
1
if 1
( )( ) otherwise
n
i
c n
T nT i c
9
MergeSort
• MergeSort is a divide & conquer algorithm
– Divide: divide an n-element sequence into two
subsequences of approx n/2 elements
– Conquer: sort the subsequences recursively
– Combine: merge the two sorted subsequences to
produce the final sorted sequence
MergeSort
def mergesort(A):
if len(A) < 2:
return A
else:
m = len(A)/2
l = mergesort(A[:m])
r = mergesort(A[m:])
return merge(l,r)
10 l r
… A … …
m
Merge of MergeSort def merge(l,r):
result, i, j = [], 0, 0
while i < len(l) and j < len(r):
if l[i] <= r[j]:
result.append(l[i])
i += 1
else:
result.append(r[j])
j += 1
result += l[i:]
result += r[j:]
return result 12
16
Methods
Two methods for solving recurrences
– Iterative substitution method
– Master method
Will be on the quiz
17
Iterative substitution
• Assume n large enough
• Substitute T on the right-hand side of the recurrence relation
• Iterate the substitution until we see a pattern which can be
converted into a general closed-form formula
20
Verify the correctness
• How to verify the solution is correct?
• Use proof by induction!
• Important: make sure the constant c works for both the base
case and the induction step
21
Proof by induction
T (n) =1 if n = 1
2T (n / 2) + n otherwise
ìíï
îï
Fact: T (n)ÎO(n log2n)
Proof. Base case: T (2) = 2T (1) + 2 = 4 £ c(2log22) = 2c.
Hence, c ³ 2.
Induction hypothesis: T (n / 2) £ cn
2log
2
n
2
Induction: T (n) = 2T (n / 2) + n
£ 2cn
2log
2
n
2+ n
= cn log2
n
2+ n = cn log
2n - cn log
22 + n
= cn log2n + n(1- c) £ cn log
2n when c ³1
Choose c = 2.
The constant c used in the
induction and the base case
has to be the same !
22
Wrong proof by induction
T (n) =1 if n = 1
2T (n / 2) + n otherwise
ìíï
îï
Fact (wrong): T (n) ÎO(n)
Proof. Base case: T (1) = 1£ c1, hence c ³1
Induction hypothesis: T (n / 2) £ c(n / 2)
Induction: T (n) = 2T (n / 2) + n
£ 2c(n / 2) + n
= cn + nÎO(n)
proof is WRONG, but where is the mistake?
Towers of Hanoi
def hanoi(n, a='A', b='B', c='C'):
if n == 0:
return
hanoi(n-1, a, c, b)
print a, '->', c
hanoi(n-1, b, a, c)
25
29
Problem
1
Problem: Solve exactly (by iterative substitution)
4 1( )
4 ( 1) 3 1
Solution: ( ) 4 4 1n n
nT n
T n n
T n
Proof?
Let’s solve the following recurrence in general:
𝑇 𝑛 = 𝑎𝑇 𝑛/𝑏 + 𝑂(𝑛𝑑) where a > 0, b > 1, T(1) = 1 Do repeated substitutions: 𝑇 𝑛 = 𝑎𝑇 𝑛/𝑏 + 𝑐𝑛𝑑
= 𝑎[𝑎𝑇(𝑛/𝑏2) + 𝑐(𝑛/𝑏)𝑑] + 𝑐𝑛𝑑 = 𝑎2𝑇 𝑛/𝑏2 + 𝑐 𝑎/𝑏𝑑 𝑛𝑑 + 𝑐𝑛𝑑
…
= 𝑎𝑗𝑇 𝑛/𝑏𝑗 + 𝑐𝑛𝑑 𝑎/𝑏𝑑𝑗−1
+⋯+ 𝑎/𝑏𝑑2+ 𝑎/𝑏𝑑 + 1
…
= 𝑎log𝑏 𝑛𝑇 1 + 𝑐𝑛𝑑 ∙ 𝑎/𝑏𝑑𝑖
log𝑏 𝑛−1
𝑖=0
= 𝑛log𝑏 𝑎 + 𝑐𝑛𝑑 ∙ (𝑎/𝑏𝑑)𝑖log𝑏 𝑛−1
𝑖=0
𝑛log𝑏 𝑎 + 𝑐𝑛𝑑 ∙ (𝑎/𝑏𝑑)𝑖log𝑏 𝑛−1
𝑖=0
Case 1: 𝑎 < 𝑏𝑑 (𝑑 > log𝑏 𝑎). The second term is a geometric series with the ratio smaller than
1, so (𝑎/𝑏𝑑)𝑖= 𝑂(1)log𝑏 𝑛−1𝑖=0 . The first term is 𝑛log𝑏 𝑎 with log𝑏 𝑎 < log𝑏 𝑏
𝑑 = 𝑑, so we get
𝑇 𝑛 = 𝑂(𝑛𝑑). Case 2: 𝑎 = 𝑏𝑑. The first term is 𝑛𝑑. In the summation, we have log𝑏 𝑛 terms and they are all equal a/𝑏𝑑 = 1, so the second term is c𝑛𝑑log𝑏 𝑛. Thus we get 𝑇 𝑛 = 𝑂(𝑛𝑑 log 𝑛). Case 3: 𝑎 > 𝑏𝑑(𝑑 < log𝑏 𝑎). Summing the geometric series in the second term, we get
(𝑎/𝑏𝑑)𝑖=log𝑏 𝑛−1
𝑖=0
(𝑎/𝑏𝑑)log𝑏 𝑛−1
𝑎/𝑏𝑑 − 1=
𝑏𝑑
𝑎 − 𝑏𝑑(𝑎log𝑏 𝑛/𝑏𝑑 log𝑏 𝑛 − 1)
=𝑏𝑑
𝑎 − 𝑏𝑑(𝑛log𝑏 𝑎/𝑛𝑑 − 1)
So
𝑇 𝑛 = 𝑛log𝑏 𝑎 + 𝑐𝑏𝑑
𝑎−𝑏𝑑𝑛log𝑏 𝑎 − 𝑛𝑑 = 𝑂(𝑛log𝑏 𝑎).
Divide-and-Conquer Master Theorem
Theorem (Master Theorem): If 𝑇 𝑛 = 𝑎𝑇 𝑛/𝑏 + 𝑂(𝑛𝑑) for some constants 𝑎 > 0, 𝑏 > 1, and d ≥ 0, then
𝑇 𝑛 =
𝑂(𝑛𝑑) 𝑖𝑓 𝑑 > log𝑏 𝑎
𝑂(𝑛𝑑 log 𝑛) 𝑖𝑓 𝑑 = log𝑏 𝑎
𝑂(𝑛log𝑏 𝑎) 𝑖𝑓 𝑑 < log𝑏 𝑎
𝑇 𝑛 =
𝑂(𝑛𝑑) 𝑖𝑓 𝑎 < 𝑏𝑑
𝑂(𝑛𝑑 log 𝑛) 𝑖𝑓 𝑎 = 𝑏𝑑
𝑂(𝑛log𝑏 𝑎) 𝑖𝑓 𝑎 > 𝑏𝑑
36
Master method: binary search
3
2 4
6
7
13
15
18
17 20
9
• Search for key 13:
15 6 7 13
𝑇 𝑛 = 𝑇 𝑛/2 + 𝑂(1) a = 1; b = 2; d = 0; logba = log21 = 0; logba = d; 𝑇 𝑛 = 𝑂(𝑛0 log 𝑛) =𝑂(log 𝑛)
44
Linear-time selection
• Problem: Select the i-th smallest element in an unsorted array of size n (assume distinct elements)
• Trivial solution: sort A, select A[i] time complexity is O(n log n)
• Can we do it in linear time? Yes, thanks to Blum, Floyd, Pratt, Rivest, and Tarjan
45
Linear-time selection
Select (A, start, end, i) /* i is the i-th order statistic */
1. divide input array A into [n/5] groups of size 5
(and one leftover group if n % 5 is not 0)
2. find the median of each group of size 5 by sorting
the groups of 5 and then picking the middle element
3. call Select recursively to find x, the median of the [n/5]
medians
4. partition array around x, splitting it into two arrays L (elements smaller than x) and R (elements bigger than x)
5. k |L|+1 if (i = k) then return x
else if (i < k) then Select (L, i)
else Select (R, i – k )
[r] means the ceiling (rounding to the next integer) of real number r
Python linear-time selection
46
def selection(a, rank):
n = len(a)
if n <= 5:
return rank_by_sorting(a, rank)
medians = [rank_by_sorting(a[i:i+5], 3)
for i in range(0, n-4, 5)]
median = selection(medians, (len(medians) + 1) // 2)
L, R = [], []
for x in a:
if x < median:
L += [x]
else:
R += [x]
if rank <= len(L):
return selection(L, rank)
else:
return selection(R, rank - len(L))
47
Let us run Select(A, 1, 28, 11), where
A={12, 34, 0, 3, 22, 4, 17, 32, 3, 28, 43, 82, 25, 27,
34, 2, 19, 12, 5, 18, 20, 33, 16, 33, 21, 30, 3, 47}
Note that the elements in this example are not
distinct.
Example
48
First make groups of 5
4
17
32
3
28
12
34
0
3
22
43
82
25
27
34
2
19
12
5
18
20
33
16
33
21
30
3
47
Example
49
Then find medians in each group
4
3
17
32
28
0
3
12
34
22
25
27
34
43
82
2
5
12
19
18
20
16
21
33
33
3
30
47
Example
50
Then find median of medians
4
3
17
32
28
0
3
12
34
22
25
27
34
43
82
2
5
12
19
18
20
16
21
33
33
3
30
47
12, 12, 17, 21, 30, 34
Example
51
Use 17 as the pivot value and partition original array
4
3
17
32
28
0
3
12
34
22
25
27
34
43
82
2
5
12
19
18
20
16
21
33
33
3
30
47
12, 12, 17, 21, 30, 34
Example
52
After partitioning
L = {12, 0, 3, 4, 3, 2, 12, 5, 16, 3}
L contains 10 elements smaller than 17
{17} this is the 11-th smallest
R = {34, 22, 32, 28, 43, 82, 25, 27, 34, 19, 18, 20, 33,
33, 21, 30, 47}
R contains 17 elements bigger than 17
Example
53
Linear-time selection
• Finding the median of medians guarantees that x causes a
“good split”
• At least a constant fraction of the n elements ≤ x and a
constant fraction > x
• Analysis: we need to find the worst case for the size of L
and R
54
Observation: At least 1/2 of the medians found in step
2 are greater than the median of medians x. So at least
half of the [n/5] groups contribute 3 elements that are
bigger than x, except for the one group with less than 5
elements and the group with x itself
Linear-time selection: analysis
x
55
Linear-time selection: analysis
• Therefore there are
3([1/2 [n/5]] – 2) ≥ (3n/10) – 6
elements are > x (or < x)
• So worst-case split has at most (7n/10) + 6 elements in
“big” section of the problem, that is:
max{|L|,|R|} < (7n/10) + 6
56
Running Time:
1. O(n) (break into groups of 5)
2. O(n) (sorting 5 numbers and finding median is O(1) time)
3. T([n/5]) (recursive call to find median of medians)
4. O(n) (partition is linear time)
5. T(7n/10 + 6) (maximum size of subproblem)
Recurrence relation
T(n) = T([n/5]) + T(7n/10 + 6) + O(n) n > 80 = Θ(1) n ≤ 80
Linear-time selection: analysis
57
Fact: T(n) = T([n/5])+T(7n/10 + 6)+O(n) is O(n) Proof:
Base case: easy (omitted).
T(n) = T([n/5]) + T(7n/10 + 6) + O(n)
≤ c[n/5] + c(7n/10 + 6) + O(n)
≤ c((n/5) + 1) + 7cn/10 + 6c + O(n)
= cn – [c(n/10 – 7) – dn]
≤ cn This step holds since n ≥ 80 implies (n/10 – 7) is positive.
Choosing c big enough makes
c(n/10 – 7) – dn positive,
so last line holds.
Linear-time selection: analysis
58
Summary (1/3)
• Goal: analyze the worst-case time-complexity of iterative and recursive algorithms
• Tools:
– Pseudo-code
– Big-O, Big-Omega, Big-Theta notations
– Recurrence relations
– Discrete Math (summations, induction proofs, methods to solve recurrence relations)
59
Summary (2/3)
• Pure iterative algorithm:
– Analyze the loops
– Determine how many times the inner core is repeated as a function of the input size
– Determine the worst-case for the input
– Write the number of repetitions as a function of the input size
– Simplify the function using big-O or big-Theta notation (optional)
60
Summary (3/3)
• Recursive + iterative algorithm:
– Analyze the recursive calls and the loops
– Determine how many recursive calls are made and
the size of the arguments of the recursive calls
– Determine how much extra processing (loops) is
done
– Determine the worst-case for the input
– Derive a recurrence relation
– Solve the recurrence relation
– Simplify the solution using big-O, or big-Theta
Recommended