Escolar Documentos
Profissional Documentos
Cultura Documentos
Dr. Ying Lu
ylu@cse.unl.edu
September 8, 2010
1
CSCE 310: Data Structures & Algorithms
2
Example: a recursive algorithm
Algorithm:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
3
Example: another recursive algorithm
Algorithm F(n)
// Compute the nth Fibonacci number recursively
//Input: A nonnegative integer n
//Output: the nth Fibonacci number
if n 1 return n
else return F(n-1) + F(n-2)
4
Example: recursive evaluation of n !
Definition: n ! = 1*2*…*(n-1)*n
Recursive definition of n!:
Algorithm:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
5
Example: recursive evaluation of n !
M(n) = M(n-1) + 1 for n > 0
6
Example: recursive evaluation of n !
Definition: n ! = 1*2*…*(n-1)*n
Recursive definition of n!:
Algorithm:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
M(n) = M(n-1) + 1
Initial Condition: M(0) = ?
7
Example: recursive evaluation of n !
Recursive definition of n!:
Algorithm:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
M(n) = M(n-1) + 1
Initial condition: M(0) = 0
Explicit formula for M(n)?
8
Time efficiency of recursive algorithms
Steps in analysis of recursive algorithms:
Decide on parameter n indicating input size
Identify algorithm’s basic operation
Determine worst, average, and best case for inputs of size n
n disks
A C
10
EXAMPLE: tower of hanoi
B B
A C A C
Move(A, C)
11
EXAMPLE: tower of hanoi
B
A C
A C
A C 12
EXAMPLE: tower of hanoi
B
A C
A C
B
A C
13
A C
EXAMPLE: tower of hanoi
B
A C
A C
B
TOWER(n, A, B, C)
A C
14
A C
EXAMPLE: tower of hanoi
B
TOWER(n-1, A, C, B)
B
A C
A C
B
Move(A, C)
TOWER(n, A, B, C)
A C
B TOWER(n-1, B, A, C)
15
A C
EXAMPLE: tower of hanoi
TOWER(n, A, B, C) {
if n<1 return;
TOWER(n-1, A, C, B);
Move(A, C);
TOWER(n-1, B, A, C)
}
16
EXAMPLE: tower of hanoi
TOWER(n, A, B, C) {
if n<1 return;
TOWER(n-1, A, C, B);
Move(A, C);
TOWER(n-1, B, A, C)
}
Algorithm analysis:
Input size? Basic operation?
17
EXAMPLE: tower of hanoi
TOWER(n, A, B, C) {
if n<1 return;
TOWER(n-1, A, C, B);
Move(A, C);
TOWER(n-1, B, A, C)
}
Algorithm analysis:
Do we need to differentiate best case, worst
case & average case for inputs of size n?
18
EXAMPLE: tower of hanoi
TOWER(n, A, B, C) {
if n<1 return;
TOWER(n-1, A, C, B);
Move(A, C);
TOWER(n-1, B, A, C)
}
Algorithm analysis:
Set up a recurrence relation and initial
condition(s) for C(n)
19
In-Class Exercise
P. 76 Problem 2.4.1 (c): solve this recurrence relation:
x(n) = x(n-1) + n
P. 76 Problem 2.4.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.
20
Example: BinRec(n)
Algorithm BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec( n/2 ) + 1
21
Smoothness rule
If T(n) (f(n))
for values of n that are powers of b, where b 2,
then
T(n) (f(n))
22
Example: BinRec(n)
Algorithm BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec( n/2 ) + 1
23
Fibonacci numbers
The Fibonacci sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
Fibonacci recurrence:
2nd order linear homogeneous
F(n) = F(n-1) + F(n-2)
recurrence relation
F(0) = 0 with constant coefficients
F(1) = 1
24
Solving linear homogeneous recurrence
relations with constant coefficients
Easy first: 1st order LHRRCCs:
C(n) = a C(n -1) C(0) = t … Solution: C(n) = t an
Extrapolate to 2nd order
L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = ?
25
Solving linear homogeneous recurrence
relations with constant coefficients
Easy first: 1st order LHRRCCs:
C(n) = a C(n -1) C(0) = t … Solution: C(n) = t an
Extrapolate to 2nd order
L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = ?
26
Computing Fibonacci numbers
1. Definition based recursive algorithm
Algorithm F(n)
// Compute the nth Fibonacci number recursively
//Input: A nonnegative integer n
//Output: the nth Fibonacci number
if n 1 return n
else return F(n-1) + F(n-2)
27
Computing Fibonacci numbers
2. Nonrecursive brute-force algorithm
Algorithm Fib(n)
// Compute the nth Fibonacci number iteratively
//Input: A nonnegative integer n
//Output: the nth Fibonacci number
F[0] 0; F[1] 1
for i 2 to n do
F[i] F[i-1] + F[i-2]
return F[n]
28
Computing Fibonacci numbers
1 1 5 n
F (n) ( ) rounded to the nearest integer
5 2
Special care in its implementation:
30
Announcement
Reading List of this Week
• Chapter 3