0% found this document useful (0 votes)
44 views65 pages

Lec04-Rec Analysis

This document discusses analyzing recursive algorithms. It provides examples of solving recurrence relations, including: 1) x(n) = x(n-1) + 5 for n > 1, x(1) = 0, which has the solution x(n) = 5n 2) x(n) = x(n-1) + n for n > 0, x(0) = 0, which has the solution x(n) = n(n+1)/2 3) x(n) = x(n/2) + n for n > 1, x(1) = 1 (solved for n = 2k), which has the solution x(n)

Uploaded by

alitheaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views65 pages

Lec04-Rec Analysis

This document discusses analyzing recursive algorithms. It provides examples of solving recurrence relations, including: 1) x(n) = x(n-1) + 5 for n > 1, x(1) = 0, which has the solution x(n) = 5n 2) x(n) = x(n-1) + n for n > 0, x(0) = 0, which has the solution x(n) = n(n+1)/2 3) x(n) = x(n/2) + n for n > 1, x(1) = 1 (solved for n = 2k), which has the solution x(n)

Uploaded by

alitheaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

CS 350 Algorithms and Complexity

Winter 2019

Lecture 4: Analyzing Recursive Algorithms

Andrew P. Black

Department of Computer Science


Portland State University
General Plan for Analysis of Recursive algorithms
✦ Decide on parameter n indicating input size
✦ Identify algorithm’s basic operation
✦ Determine worst, average, and best cases
for input of size n
✦ Set up a recurrence relation, with initial
condition, for the number of times the basic
operation is executed
✦ Solve the recurrence, or at least ascertain
the order of growth of the solution (see
Levitin Appendix B)
2
Ex 2.4, Problem 1(a)
! Use a piece of paper and do this now,
individually.
" Solve this recurrence relation:

x(n) = x(n 1) + 5 for n > 1


x(1) = 0

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

replace n by n−1: x(n 1) = x(n 2) + 5

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1):

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2):

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize:

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize: = x(n i) + 5i 8i < n

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize: = x(n i) + 5i 8i < n

put i = (n−1) :

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize: = x(n i) + 5i 8i < n

put i = (n−1) : = x(n (n 1)) + 5(n 1)

6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize: = x(n i) + 5i 8i < n

put i = (n−1) : = x(n (n 1)) + 5(n 1)

substitute for x(1):


6
My Solution
x(n) = x(n 1) + 5 for all n > 1
x(1) = 0

replace n by n−1: x(n 1) = x(n 2) + 5

substitute for x(n−1): x(n) = x(n 2) + 5 + 5

substitute for x(n−2): = x(n 3) + 5 + 5 + 5

generalize: = x(n i) + 5i 8i < n

put i = (n−1) : = x(n (n 1)) + 5(n 1)

substitute for x(1): = 5(n 1)


6
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

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 :

x(n) = x(n/2) + n for n > 1


x(1) = 1

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 :

x(n) = x(n/2) + n for n > 1


x(1) = 1

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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1

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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1
" Check:

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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1
" Check:
x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4

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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1
" Check:
x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4
x(n) = 3x(n 1) definition of recurrence
= 3 ⇥ [4 ⇥ 3(n 1) 1 ] substitute solution
= 4 ⇥ 3n 1 = x(n) 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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1
" Check:
x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4
x(n) = 3x(n 1) definition of recurrence
= 3 ⇥ [4 ⇥ 3(n 1) 1 ] substitute solution
= 4 ⇥ 3n 1 = x(n) 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

x(n) = 3x(n 1) for n > 1, x(1) = 4


the solution is
x(n) = 4 ⇥ 3n 1
" Check:
x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4
x(n) = 3x(n 1) definition of recurrence
= 3 ⇥ [4 ⇥ 3(n 1) 1 ] substitute solution
= 4 ⇥ 3n 1 = x(n) 9
c. x(n) = x(n − 1) + n for n > 0, x(0) = 0

Ex 2.4, Problem 2
d. x(n) = x(n/2) + n for n > 1, x(1) = 1 (solve for n = 2k )

e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k )


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
F(n)n ≝
first if n S(n)
cubes: = 0 = 13 + 23 + ... + n3 .
then return 1
Algorithm S(n)else return F(n – 1) ⨉ 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.

b. How does this algorithm compare with the straightforward nonrecursive


algorithm for computing this function? 10
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

Let C(n) be the number of calls made in computing F (n)

11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

11
F(n) ≝ if n = 0
my solution then return 1
else return F(n – 1) ⨉ n

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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

Let C(n) be the number of calls made in computing F (n)


C(n) = C(n 1) + 1
C(0) = 1 (when n = 0, there is 1 call)

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.

b. How does this algorithm compare with the straightforward nonrecursive


algorithm for computing this function?
4. Consider the following recursive algorithm.

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.

Ex 2.4, Problem 4(a)


b. How does this algorithm compare with the straightforward nonrecursive
algorithm for computing this function?
4. Consider the following recursive algorithm.

Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1

a. Set up a recurrence relation for this function’s values and solve it


to determine what this algorithm computes.

b. Set up a recurrence relation for the number of multiplications made by


this algorithm and solve it.

c. Set up a recurrence relation for the number of additions/subtractions


made by this algorithm and solve it.

27
13
algorithm’s basic operation is executed.

Ex 2.4, Problem 4(a)


b. How does this algorithm compare with the straightforward nonrecursive
algorithm for computing this function?
4. Consider the following recursive algorithm.

Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1

a. Set up a recurrence relation for this function’s values and solve it


to determine what this algorithm computes.

b. Set up a recurrence relation for the number of multiplications made by


this algorithm and solve it.

c. Set up a recurrence relation for the number of additions/subtractions


made by this algorithm and solve it.

27
13
algorithm’s basic operation is executed.

Ex 2.4, Problem 4(a)


b. How does this algorithm compare with the straightforward nonrecursive
algorithm for computing this function?
4. Consider the following recursive algorithm.

Algorithm Q(n)
//Input: A positive integer n
if n = 1 return 1
else return Q(n − 1) + 2 ∗ n − 1

a. Set up a recurrence relation for this function’s values and solve it


to determine what this algorithm computes.

b. Set up a recurrence relation for the number of multiplications made by


this algorithm and solve it.

c. Set up a recurrence relation for the number of additions/subtractions


made by this algorithm and solve it.

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 .

b. Set up a recurrence relation for the number of additions made by


the algorithm and solve it.

c. Draw a tree of recursive calls for this algorithm and count the number
of calls made by the algorithm.

d. Is it a good algorithm for solving this problem?


8. Consider the following recursive algorithm.

Algorithm Min1 (A[0..n − 1])


//Input: An array A[0..n − 1] of real numbers
if n = 1 return A[0]
else temp ← Min1 (A[0..n − 2])
if temp ≤ A[n − 1] return temp 14
else return A[n − 1]
the one for the recursive version:

Solution to Problem 8
A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0,

with the solution A(n) = ⌊log2 n⌋ + 1.


7. a. Algorithm Power (n)
We didn’t
//Computes 2n recursively by the formula 2n = 2n−1 + 2n−1
//Input: A nonnegative integer n
//Output: Returns 2n simplify!
if n = 0 return 1
else return P ower(n − 1) + P ower(n − 1)

36

15
the one for the recursive version:

Solution to Problem 8
A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0,

with the solution A(n) = ⌊log2 n⌋ + 1.


7. a. Algorithm Power (n)
We didn’t
//Computes 2n recursively by the formula 2n = 2n−1 + 2n−1
//Input: A nonnegative integer n
//Output: Returns 2n simplify!
if n = 0 return 1
else return P ower(n − 1) + P ower(n − 1)
b. A(n) = 2A(n − 1) + 1, A(0) = 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.

c. The tree of recursive calls for this algorithm looks as follows:


n

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.

he tree of recursive calls for this algorithm looks as follows:


n

n-1 n-1

n-2 n-2 n-2 n-2


... ... ...
1 1 1 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?

10. The determinant of an n-by-n matrix


⎡ ⎤
a11 a1n
⎢ a21 a2n ⎥
A=⎢ ⎣
⎥,

an1 ann

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

where sj is +1 if j is odd and -1 if j is even, a1j is the element in row


1 and column j, and Aj is the (n − 1)-by-(n − 1) matrix obtained from
matrix A by deleting its row 1 and column j.

a.◃ Set up a recurrence relation for the number of multiplications made


by the algorithm implementing this recursive definition. (Ignore multiplications by sj .)

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?

10. The determinant of an n-by-n matrix


⎡ ⎤
a11 a1n
⎢ a21 a2n ⎥
A=⎢ ⎣
⎥,

an1 ann

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

where sj is +1 if j is odd and -1 if j is even, a1j is the element in row


1 and column j, and Aj is the (n − 1)-by-(n − 1) matrix obtained from
matrix A by deleting its row 1 and column j.

a.◃ Set up a recurrence relation for the number of multiplications made


by the algorithm implementing this recursive definition. (Ignore multiplications by sj .)

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

where sj is +1 if j is odd and -1 if j is even, a1j is t


1 and column j, and Aj is the (n − 1)-by-(n − 1) ma
matrix A by deleting its row 1 and column j.

a.◃ Set up a recurrence relation for the number of mu


by the algorithm implementing this recursive definition

b.◃ Without solving the recurrence, what can you sa


tion’s order of growth as compared to n! ?
9. von Neumann neighborhood revisited Find the numbe
Neumann neighborhood of range n (see Problem 11 in
setting up and solving a recurrence relation.

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

where s is +1 if j is odd and -1 if j is even, a is t


Let M (n) be the number j
of multiplications made in 1j
1 and column j, and Aj is the (n − 1)-by-(n − 1) ma
computing the determinant
matrix A byof an n ⇥
deleting its n matrix.
row 1 and column j.
M (1) = 0 a.◃ Set up a recurrence relation for the number of mu
X n by the algorithm implementing this recursive definition
M (n) = (1 + M (n 1)) 8n > 1
j=1 b.◃ Without solving the recurrence, what can you sa
tion’s order of growth as compared to n! ?
9. von Neumann neighborhood revisited Find the numbe
Neumann neighborhood of range n (see Problem 11 in
setting up and solving a recurrence relation.

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

where s is +1 if j is odd and -1 if j is even, a is t


Let M (n) be the number j
of multiplications made in 1j
1 and column j, and Aj is the (n − 1)-by-(n − 1) ma
computing the determinant
matrix A byof an n ⇥
deleting its n matrix.
row 1 and column j.
M (1) = 0 a.◃ Set up a recurrence relation for the number of mu
X n by the algorithm implementing this recursive definition
M (n) = (1 + M (n 1)) 8n > 1
j=1 b.◃ Without solving the recurrence, what can you sa
tion’s order of growth as compared to n! ?
M (n) = n ⇥ (1 + M (n 1)) 8n > 1
9. von Neumann neighborhood revisited Find the numbe
M (n) = n + nM (nNeumann
1) neighborhood of range n (see Problem 11 in
setting up and solving a recurrence relation.

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

where s is +1 if j is odd and -1 if j is even, a is t


Let M (n) be the number j
of multiplications made in 1j
1 and column j, and Aj is the (n − 1)-by-(n − 1) ma
computing the determinant
matrix A byof an n ⇥
deleting its n matrix.
row 1 and column j.
M (1) = 0 a.◃ Set up a recurrence relation for the number of mu
X n by the algorithm implementing this recursive definition
M (n) = (1 + M (n 1)) 8n > 1
j=1 b.◃ Without solving the recurrence, what can you sa
tion’s order of growth as compared to n! ?
M (n) = n ⇥ (1 + M (n 1)) 8n > 1
9. von Neumann neighborhood revisited Find the numbe
M (n) = n + nM (nNeumann
1) neighborhood of range n (see Problem 11 in
setting up and solving a recurrence relation.

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.

Recall Eqn 2.10


2.12:
1 n
F ib(n) = p rounded to the nearest integer
5
1
p
where = 2 (1 + 5)

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.

b. Similarly, we have to find the sma


263 − 1. Thus,
20
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 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

You might also like