Lec04-Rec Analysis
Lec04-Rec Analysis
Winter 2019
Andrew P. Black
3
Individual Problem (Q1):
Solve the recurrence
x(n) = x(n﹣1) + 5 for n > 1
x(1) = 0
What’s the answer?
4
Individual Problem (Q1):
Solve the recurrence
x(n) = x(n﹣1) + 5 for n > 1
x(1) = 0
What’s the answer?
A. x(n) = n﹣1
B. x(n) = 5n
C. x(n) = 5n﹣5
D. None of the above
5
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
replace n by n−1:
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
generalize:
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
put i = (n−1) :
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0
7
Ex 2.4, Problem 1(c)
! Use a piece of paper and do this now,
individually.
" Solve this recurrence relation:
x(n) = x(n 1) + n for n > 0
x(0) = 0
Answer?
A. x(n) = n2 C. x(n) = n(n+1)/2
B. x(n) = n2/2 D. None of the above
7
Ex 2.4, Problem 1(d)
! Use a piece of paper and do this now,
individually.
k
" Solve this recurrence relation for n = 2 :
8
Ex 2.4, Problem 1(d)
! Use a piece of paper and do this now,
individually.
k
" Solve this recurrence relation for n = 2 :
Answer?
A. x(n) = 2n+1 C. x(n) = n(n+1)
B. x(n) = 2n – 1 D. None of the above
8
What does that mean?
9
What does that mean?
! “Solving a Recurrence relation” means:
9
What does that mean?
! “Solving a Recurrence relation” means:
" find an explicit (non-recursive) formula that
satisfies the relation and the initial condition.
9
What does that mean?
! “Solving a Recurrence relation” means:
" find an explicit (non-recursive) formula that
satisfies the relation and the initial condition.
" For example, for the relation
9
What does that mean?
! “Solving a Recurrence relation” means:
" find an explicit (non-recursive) formula that
satisfies the relation and the initial condition.
" For example, for the relation
9
What does that mean?
! “Solving a Recurrence relation” means:
" find an explicit (non-recursive) formula that
satisfies the relation and the initial condition.
" For example, for the relation
9
What does that mean?
! “Solving a Recurrence relation” means:
" find an explicit (non-recursive) formula that
satisfies the relation and the initial condition.
" For example, for the relation
Ex 2.4, Problem 2
d. x(n) = x(n/2) + n for n > 1, x(1) = 1 (solve for n = 2k )
a. Set up and solve a recurrence relation for the number of times the
algorithm’s basic operation is executed.
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
= C(n 2) + 2
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
= C(n 2) + 2
= C(n i) + i 8i < n (generalize)
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
= C(n 2) + 2
= C(n i) + i 8i < n (generalize)
Put i = n: = C(0) + n
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
= C(n 2) + 2
= C(n i) + i 8i < n (generalize)
Put i = n: = C(0) + n
= 1+n
11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n
C(n) = C(n 1) +1
= [C(n 2) + 1] + 1
= C(n 2) + 2
= C(n i) + i 8i < n (generalize)
Put i = n: = C(0) + n
= 1+n
Now check! 11
e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k )
Ex 2.4, Problem 3
2. Set up and solve a recurrence relation for the number of calls made by
F (n), the recursive algorithm for computing n!.
3. Consider the following recursive algorithm for computing the sum of the
first n cubes: S(n) = 13 + 23 + ... + n3 .
Algorithm S(n)
//Input: A positive integer n
//Output: The sum of the first n cubes
if n = 1 return 1
else return S(n − 1) + n ∗ n ∗ n
a. Set up and solve a recurrence relation for the number of times the
algorithm’s basic operation is executed.
Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1 12
else return Q(n − 1) + 2 ∗ n − 1
e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k )
e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k )
Ex 2.4, Problem 3
2. Set up and solve a recurrence relation for the number of calls made by
2. Set
F upthe
(n), andrecursive
solve a algorithm
recurrence for
relation
computingfor the
n!.number of calls made by
F (n), the recursive algorithm for computing n!.
3. Consider the following recursive algorithm for computing the sum of the
3. Consider
first the following
n cubes: S(n) = 133recursive
+ 233 + ...algorithm
+ n33 . for computing the sum of the
first n cubes: S(n) = 1 + 2 + ... + n .
Algorithm S(n)
Algorithm
//Input: S(n) integer n
A positive
//Input: A positive integer n
//Output: The sum of the first n cubes
//Output: The sum of the first n cubes
if n = 1 return 1
if n = 1 return 1
else return S(n − 1) + n ∗ n ∗ n
else return S(n − 1) + n ∗ n ∗ n
a. Set up and solve a recurrence relation for the number of times the
a. Set up and solve a recurrence relation for the number of times the
algorithm’s basic operation
algorithm’s basic operation is
is executed.
executed.
b.
b. How does this
How does this algorithm
algorithm compare
compare with
with the
thestraightforward
straightforwardnonrecursive
nonrecursive
algorithm for computing
algorithm for computing this
this function?
function?
4. S 1 the
4. Consider
Consider the following
following recursive
recursive algorithm.
algorithm.
for i 2 to n do
S
Algorithm
Algorithm i⇥i⇥i
S + Q(n)
Q(n)
return SA
//Input:
//Input: A positive
positive integer
integer nn
n=
if n
if = 11 return
return 11 12
else return Q(n − 1) + + 22 ∗∗ nn −
− 11
algorithm’s basic operation is executed.
Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1
27
13
algorithm’s basic operation is executed.
Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1
27
13
algorithm’s basic operation is executed.
Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1
27
13
Ex 2.4, Problem 8
b. Set up a recurrence relation for the number of additions made by
the nonrecursive version of this algorithm (see Section 2.3, Example 4)
and solve it.
7. a. Design a recursive algorithm for computing 2n for any nonnegative
integer n that is based on the formula: 2n = 2n−1 + 2n−1 .
c. Draw a tree of recursive calls for this algorithm and count the number
of calls made by the algorithm.
Solution to Problem 8
A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0,
36
15
the one for the recursive version:
Solution to Problem 8
A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0,
A(n) = 2A(n − 1) + 1 36
= 2[2A(n − 2) + 1] + 1 = 22 A(n − 2) + 2 + 1
= 22 [2A(n − 3) + 1] + 2 + 1 = 23 A(n − 3) + 22 + 2 + 1
= ...
= 2i A(n − i) + 2i−1 + 2i−2 + ... + 1
= ...
= 2n A(0) + 2n−1 + 2n−2 + ... + 1 = 2n−1 + 2n−2 + ... + 1 = 2n − 1.
n-1 n-1 15
(n) = 2A(n − 1) + 1, A(0) = 0.
Solution to Problem 8
= 2A(n − 1) + 1
= 2[2A(n − 2) + 1] + 1 = 22 A(n − 2) + 2 + 1
= 22 [2A(n − 3) + 1] + 2 + 1 = 23 A(n − 3) + 22 + 2 + 1
= ...
= 2i A(n − i) + 2i−1 + 2i−2 + ... + 1
= ...
= 2n A(0) + 2n−1 + 2n−2 + ... + 1 = 2n−1 + 2n−2 + ... + 1 = 2n − 1.
n-1 n-1
0 0 0 0 0 0 0 0
16
b. Which of the algorithms Min1 or Min2 is faster? Can you sug-
gest an algorithm for the problem they solve that would be more efficient
Ex 2.4, Problem 11
than either of them?
denoted det A, can be defined as a11 for n = 1 and, for n > 1, by the
recursive formula n
'
det A = sj a1j det Aj ,
j=1
b.◃ Without solving the recurrence, what can you say about the solu-
tion’s order of growth as compared to n! ?
17
9. von Neumann neighborhood revisited Find the number of cells in the von
b. Which of the algorithms Min1 or Min2 is faster? Can you sug-
gest an algorithm for the problem they solve that would be more efficient
Ex 2.4, Problem 11
than either of them?
denoted det A, can be defined as a11 for n = 1 and, for n > 1, by the
recursive formula n
'
det A = sj a1j det Aj ,
j=1
b.◃ Without solving the recurrence, what can you say about the solu-
tion’s order of growth as compared to n! ?
17
9. von Neumann neighborhood revisited Find the number of cells in the von
denoted det A, can be defined as a11 for n = 1 and,
recursive formula
my solution
'n
det A = sj a1j det Aj ,
j=1
18
denoted det A, can be defined as a11 for n = 1 and,
recursive formula
my solution
'n
det A = sj a1j det Aj ,
j=1
18
denoted det A, can be defined as a11 for n = 1 and,
recursive formula
my solution
'n
det A = sj a1j det Aj ,
j=1
18
denoted det A, can be defined as a11 for n = 1 and,
recursive formula
my solution
'n
det A = sj a1j det Aj ,
j=1
Without solving this relation, what can you say about M’s
order of growth, compared to n! ?
18
Problem Problem 3, Levitin 2e §2.5
The maximum values of the Java primitive type int is 231 1. Find the smallest
n for which the nth Fibonacci number is not going to fit in a variable of type
int.
19
Solution
20
Using the formula F (n) = 5 φ roun
Solution
(approximately) the following inequalit
1 n
! We need the smallest n s.t.√
Fib(n) > 31
φ > 2 − 1 or φ
5
After taking natural logarithms of both
√ 31
ln( 5(2 −
n>
ln φ
Thus, the answer is n = 47.
Solution
(approximately) the following inequalit
31
a. The question is to find the smallest value of n such that F (n) > 2
Using the formula F (n) = √15 φn rounded to the nearest integer, we
1 n
! We need thethe smallest n s.t. Fib(n) > 31
(approximately) following √
inequality: φ > 2 − 1 or φ
5√
1 n
√ φ > 231 − 1 or φn > 5(231 − 1).
5
After taking natural logarithms of both
√ 31
After taking natural logarithms of both hand sides, we obtain
√ 31 ln( 5(2 −
ln( 5(2 − 1)) n>
n> ≈ 46.3. ln φ
ln φ
Thus, the answer Thus, the
is n = 47. answer is n = 47.
b. Similarly, we have to find the smallest value of n such that F (n
263 − 1. Thus, b. Similarly, we have to find the sma
2163 − 1. Thus, √
n 63 n
√ φ >2 − 1, or φ > 5(263 − 1) 20
5
Using the formula F (n) = 5 φ roun
Solution
(approximately) the following inequalit
31
a. The question is to find the smallest value of n such that F (n) > 2
Using the formula F (n) = √15 φn rounded to the nearest integer, we
1 Recall Eqn
31 2.12:
! We need thethe smallest n s.t.√Fib(n) > 2 − 1 or φ
n
(approximately) following inequality: φ >
5√ F ib(n) =
1 n 31 n 31
√ φ > 2 − 1 or φ > 5(2 − 1).
5
After taking natural logarithms of pboth 1
where = 2 (1 + 5)
√
After taking natural logarithms of both hand sides, we obtain
31
√ 31 ln( 5(2 −
ln( 5(2 − 1)) n>
n> ≈ 46.3. ln φ
ln φ
Thus, the answer Thus, the
is n = 47. answer is n = 47.
b. Similarly, we have to find the smallest value of n such that F (n
263 − 1. Thus, b. Similarly, we have to find the sma
2163 − 1. Thus, √
n 63 n
√ φ >2 − 1, or φ > 5(263 − 1) 20
5
Using the formula F (n) = 5 φ roun
Solution
(approximately) the following inequalit
31
a. The question is to find the smallest value of n such that F (n) > 2
Using the formula F (n) = √15 φn rounded to the nearest integer, we
1 Recall Eqn
31 2.12:
! We need thethe smallest n s.t.√Fib(n) > 2 − 1 or φ
n
(approximately) following inequality: φ >
5√ F ib(n) =
1 n 31 n 31
√ φ > 2 − 1 or φ > 5(2 − 1).
5
After taking natural logarithms of pboth 1
where = 2 (1 + 5)
√
After taking natural logarithms of both hand sides, we obtain
! Take natural logs √ of both sides:
31
ln( 5(2 −
ln( 5(231 − 1)) n>
n> ≈ 46.3. ln φ
ln φ
Thus, the answer Thus, the
is n = 47. answer is n = 47.
b. Similarly, we have to find the smallest value of n such that F (n
263 − 1. Thus, b. Similarly, we have to find the sma
2163 − 1. Thus, √
n 63 n
√ φ >2 − 1, or φ > 5(263 − 1) 20
5
Using the formula F (n) = 5 φ roun
Solution
(approximately) the following inequalit
31
a. The question
uestion is tothe
is to find findsmallest
the smallest value
value ofofnnsuch
such that
that FF(n)
(n)>>2 2
1 n1 n
Using the formula (n) = rounded
e formula F (n) = √5 φ 5 φrounded
F √
1 tothe
to the nearest
nearest integer,
Recall Eqn integer,
2.12:
we
! We need thethe smallest n s.t. Fib(n) >
n 31
(approximately) following inequality:
mately) the following inequality:
√ φ > 2 − 1 or φ
5
√ 31 F ib(n) =
1 n 31 n
1 n√5 φ 31> 2 − 1 or nφ >√ 5(231 − 1).
√ φ After
>2 − taking
1 or natural
φ > 5(2 − 1). 1 of p
logarithms both
5 where = 2 (1 + 5)
√ 31
After taking natural logarithms of both hand sides, we obtain
! Take logarithms
king natural natural logs ofof
√ both
both sides:
hand sides, ln(
we 5(2
obtain −
ln( 5(231 − 1)) n>
n >√ ≈ 46.3. ln φ
31ln φ
ln( 5(2 − 1))
Thus, the answernThus,
is>n = 47.
thelnanswer ≈ 46.3.
is n = 47.
φ
eb. answer
Similarly, we=have
is n 47. to find the smallest value of n such that F (n
263 − 1. Thus,b. Similarly, we have to find the sma
63
2
arly, we have to find− 1.
the Thus, √ 63 n such that F
1 n 63 smallestn value of 20
√ φ > 2 − 1, or φ > 5(2 − 1)
Thus, 5
Using the formula F (n) = 5 φ roun
tion is to find the smallest value of n such that F (n)
Solution
rmula F (n) = (approximately)
√1 n the following inequalit
φ rounded to the nearest integ 31
a. The question
uestion is tothe
is to find find the smallest
5 smallest
value
value ofofnnsuch
such that
that FF(n)
(n)>>2 2
1 n1 n
Using the formula (n) = rounded
eely)
formula F φ 5 φrounded
√
F (n) = √5inequality:
the following 1 tothe
to the nearest
nearest integer,
Recall Eqn integer,
2.12:
we
! We need thethe smallest n s.t. Fib(n) >
n 31
(approximately) following
mately) the following inequality:inequality:√ φ > 2 − 1 or φ
1 n 1 31n 31 5√ F ib(n) =
n n √ 3131
√ 1φ n>√ 2φ 31>−21 −or 1 orφ φ >
n
> 5(2
√ 5(231
− 1).
− 1).
>5 2 −
5√ φ After taking
1 or natural
φ > 5(2 − 1). 1 of p
logarithms both
5 where = 2 (1 + 5)
√ 31
After taking natural logarithms of both hand sides, we obtain
king ! Take
natural
natural natural logs
logarithms
logarithms of
ofof
√ both
both
both sides:
hand
hand sides,
sides, we
ln(
we obtain
5(2
obtain −
ln( 5(231 − 1)) n>
n>√√ 31 31ln φ
≈ 46.3. ln φ
ln( 5(2
ln( 5(2 − −1))1))
n n>>
Thus, theln ≈ ≈ 46.3.
46.3.
Thus, the answer is n = 47. lnanswer
φφ is n = 47.
b. answer
e Similarly,
is we=have
n 47. to find the smallest value of n such that F (n
nswer is n = 47.
263 − 1. Thus,b. Similarly, we have to find the sma
63
2
arly, we have to find− 1.
the Thus, √ 63 n such that F
1 n 63 smallestn value of
we have to√5find
,Thus, 2 −smallest
φ > the 1, or φ >value − 1)n such20 th
5(2 of
Using the formula F (n) = 5 φ roun
tion is to find the smallest value of n such that F (n)
Solution
rmula F (n) = (approximately)
√1 n the following inequalit
φ rounded to the nearest integ 31
a. The question
uestion is tothe
is to find find the smallest
5 smallest
value
value ofofnnsuch
such that
that FF(n)
(n)>>2 2
1 n1 n
Using the formula (n) = rounded
eely)
formula F φ 5 φrounded
√
F (n) = √5inequality:
the following 1 tothe
to the nearest
nearest integer,
Recall Eqn integer,
2.12:
we
! We need thethe smallest n s.t. Fib(n) >
n 31
(approximately) following
mately) the following inequality:inequality:√ φ > 2 − 1 or φ
1 n 1 31n 31 5√ F ib(n) =
n n √ 3131
√ 1φ n>√ 2φ 31>−21 −or 1 orφ φ >
n
> 5(2
√ 5(231
− 1).
− 1).
>5 2 −
5√ φ After taking
1 or natural
φ > 5(2 − 1). 1 of p
logarithms both
5 where = 2 (1 + 5)
√ 31
After taking natural logarithms of both hand sides, we obtain
king ! Take
natural
natural natural logs
logarithms
logarithms of
ofof
√ both
both
both sides:
hand
hand sides,
sides, we
ln(
we obtain
5(2
obtain −
ln( 5(231 − 1)) n>
n>√√ 31 31ln φ
≈ 46.3. ln φ
ln( 5(2
ln( 5(2 − −1))1))
n n>>
Thus, theln ≈ ≈ 46.3.
46.3.
Thus, the answer is n = 47. lnanswer
φφ is n = 47.
e ! Thus,
b. answer
Similarly,
is wethe
n = answer
have
47. to findisthe
n = 47
smallest value of n such that F (n
nswer is n = 47.
263 − 1. Thus,b. Similarly, we have to find the sma
63
2
arly, we have to find− 1.
the Thus, √ 63 n such that F
1 n 63 smallestn value of
we have to√5find
,Thus, 2 −smallest
φ > the 1, or φ >value − 1)n such20 th
5(2 of