Relations: 3. Recursive Algorithms and Recurrence
Relations: 3. Recursive Algorithms and Recurrence
D(n)=n . D(n-1)
D(n)= n. D(n-1)
= n. (n-1). D(n-2)
= n. (n-1). (n-2). … x 3 x 2 x D(1)
n!
⇒ D(n) ∈ O(n! )
ALGORITHM Factorial(n)
// Recursively calculates n! for positive integer n
if n=1
return 1
else
return n*Factorial(n-1)
F(1) = 0
F(n) = F(n-1) + 1, n>1
F(n)-F(n-1)=1
F(n-1)-F(n-2)=1
We can continue to do this until the row beginning with F(2), which
contains the first mention of the base case (n-1 = 2-1 = 1).
F(n) - F(n-1) = 1
+ F(n-1) – F(n-2) = 1
+ F(n-2) – F(n-3) = 1 n-1 equations
…. …. …
+ F(2) - F(1) = 1
F(n) - F(1) = n-1
→ F(n) = F(1) + n - 1
→ F(n) = n-1, since F(1)=0
→ F(n) ∈ O(n).
Aside:
The analysis ignored the fact that large values will build up quickly
and so it was not in this case ideal to count integer multiplications
as elementary. However it was a simple example that showed the
techniques that can also be used in much more complex cases.
* * *
The expression
D(n)= n. D(n-1)
is clearly also a first order recurrence of the same kind, but here
the multiplying coefficient depends on n. This is the next step up in
complication, where for a general n-dependent multiplier b(n)
Again by inspection
f(n) = b(n)xf(n-1)
= b(n)xb(n-1)xf(n-2)
= b(n)xb(n-1)x...xb(a+1)xf(a)
"$ n &$
f(n) = # ! b(i)' xf(a)
$%i=a+1 $(
(1) is the most general case of a first order (it makes reference to
only one smaller sized instance), homogeneous (there is nothing
on the r.h.s. that doesn't multiply a smaller sized instance, such as
a polynomial in n) recurrence relation.
F(n) = F(n-1) + 1
?
This is a simple example of a first order, inhomogeneous
recurrence relation, for which the general form (with non-constant
coefficients b(n), c(n)) is
same coefficient
c(n)
where d(n) = .
b(a +1)xb(a + 2)x...xb(n)
(3) is now in a much simpler form (the important thing is that the
multiplier of the smaller-input instance of the function on the r.h.s.
is now 1) and can be solved by the ladder method we used in the
case of F(n) (where d(n) =1, independent of n) to give the solution
n "$ n &$
f(n) = ) b(i) # f(a) + ! d(j)'
i=a+1 %$ j=a+1 ($
Change variable:
n
n n
f(n) = 3 g(n) (b(i) = 3 for all i, so ! b(i) = 3 )
i=1
This gives
3n
n
1
g(n) – g(0) = ∑3 i
i=1
⎡ a(1 − an ) ⎤
= ⎢ ⎥
⎣ 1 − a ⎦ a = 1
3
1 ⎛ 1 ⎞
= ⎜1 − n ⎟
2 ⎝ 3 ⎠
Hence
3n " 1% 1
f(n) = $1! n ' = 3n !1
2# 3 & 2
( ) ( O(3n )
Try the solution f(n) = α n (see Brassard and Bratley p.65 for a
justification of this choice):
characteristic equation
k
f (n) = ∑ c iρni
i=1
Example (k=2):
f(0) = 0
f(1) = 1
f(n) = 3f(n-1) – 2f(n-2), n>1
αn - 3αn-1 + 2αn-2 = 0
α2 – 3α + 2 = 0
→ (α - 1)(α - 2) = 0
→ f(n) = 2n – 1
f(0) = 1
f(1) = 5
f(2) = 19
f(n) = 7f(n-1) – 14f(n-2) + 8f(n-3), n>2
α3 – 7α2 + 14α - 8 = 0
→ (α - 1) (α - 2) (α - 4) = 0
→ f(n) = 4n + 2n - 1 ∈ O( 4n )
xn = ρ n, xn = n ρ n, …, xn = nm-1ρ n
Example (k=2):
f(0) = 1
f(1) = 5
f(n) = 2f(n-1) – f(n-2), n>1
α2 – 2α + 1 = 0
→ (α - 1)2 = 0
→ f(n) = 4n + 1
f(0) = 2
f(1) = 9
f(2) = 29
f(n) = 5f(n-1) – 8f(n-2) + 4f(n-3), n>2
α3 – 5α2 + 8α - 4 = 0
→ (α - 1) (α - 2)2 = 0
with
Fib(0) = 0
Fib(1) = 1
1
Helianthus flower, L Shyamal 2006
2
Young cones of a Colorado spruce,
http://www.noodlesnacks.com 2008
α2 – α – 1 = 0
1± 5
!1,2 =
2
and hence a general solution for the Fibonacci series values of the
form
Fib(n) = c1ρ1n + c2ρ2n
1 1
c1 + c2 = 0 (n=0) → c1 = , c2 = !
5 5
c1 ρ1 + c2 ρ2 = 1 (n=1)
and hence
n n
1 !# 1+ 5 $& 1 !# 1' 5 $& 1 n !n
Fib(n) =
5"
# 2 &
%
'
5"
# 2 &
%
=
5
( '( ( )
in which
!
! = (1+ 5) / 2 " 1.61803 , ! = "1/ ! # "0.61803
Fib(n) ! O("n )
!
since as | ! | < 1 the contribution from the oscillating second part
of the solution falls to zero very rapidly as n increases.
ALGORITHM Fib1(n)
// Calculates the nth Fibonacci number for integer n ≥ 0
if n ≤ 1
return n
else
return Fib1(n-1)+Fib1(n-2)
F(0) = 0
F(1) = 0
F(n) = F(n-1) + F(n-2) + 1
(In fact it does fall within the slightly expanded range of functional
forms considered in Brassard and Bratley (p.68) but we will not
cover these more general methods of solution.)
G(0) = 1
G(1) = 1
G(n) = G(n-1) + G(n-2)
ALGORITHM Fib2(n)
// Calculates the nth Fibonacci number for integer n ≥ 0
A[0] <− 0
A[1] <− 1
for i <− 2 to n do
A[i] <− A[i-1]+A[i-2]
return A[n]
CHANGE OF VARIABLE
ALGORITHM Exp1(a,n)
// Computes an for positive integer n
if n=1
return a
else
return a*Exp1(a,n-1)
a → a2 → a4 → a8 → a16 → a32
ALGORITHM Exp2(a,n)
// Computes an for positive integer n
if n=1
return a
else
if even(n)
return [Exp2(a,n/2)]2
else
return a*[Exp2(a,(n-1)/2)]2
(divisions in each case give
integer result)
E((n-1)/2) + 2 if n odd
Suppose for the moment that n is not only even but is a power
of 2. Make the change of variable
n → 2k
E(2k) = E(2k/2) + 1
= E(2k-1) + 1
Ek – Ek-1 = 1
Ek – Ek-1 = 1
+ Ek-1 – Ek-2 = 1 k equations
+… … …
+ E1 - E0 = 1
Ek - E0 = k
So
Ek = E0 + k
→ E(2k) = k
Since
n = 2k ⇔ k = log2 n
E(n) = log2 n
conditional asymptotic
notation
if
E(n) ∈ O(log n)
Example : f(1) = 1
f(n) = 4 f(n/2) + n2, n>1
fk = 4fk-1 + 4k
→ 4k gk = 4. 4k-1 gk-1 + 4k
gk - gk-1 =1
+ gk-1 – gk-2 =1
+ … … …
+ g1 - g0 =1
gk - g0 =k
fk = 4 k f0 + 4 k . k
∈ O(n2log n)
f(n) ∈ O(n2log n)
fk = 4 fk-1 – 4 fk-2
α2 – 4α + 4 = 0
→ (α - 2)2 = 0
fk = c 1 2 k + c 2 k 2 k
log2n
f(n) = c1 n + c2 n log2n
Is this O(n log n)? It depends on the base cases, which determine
c1 and c2.
f(1) = c1 + c2 log21 = c1 = 1
0
f(2) = 2c1 + 2 c2 log2 2 = 2(c1 + c2) = 2
1
→c1 = 1, c2 = 0
→f(n)=n
O(n) not O(n log n)
We can write the two n-bit numbers which are to be multiplied like
this:
a b c d
( a . 2n/2 + b ) × ( c. 2n/2 + d )
a b
Mk = 4 Mk-1 + a1 2k + a2
4kNk = 4. 4k-1Nk-1 + a1 2k + a2
+… …
+ N1 - N0 = a1/21 + a2/41
k
Nk - N0 = ∑ (a
i=1
1
/ 2i + a 2 / 4 i )
i =1 ⎝ 2 ⎠ i =1 ⎝ 4 ⎠
⎩ ⎭
∑
k
i
(
⎛ 1 ⎞ 1/ 2 1 − (1/ 2)
⎜ ⎟ =
k
)
i =1 ⎝ 2 ⎠ 1 − 1/ 2
1
= 1− <1
2k
∑
k
i
(
⎛ 1 ⎞ 1/ 4 1 − (1/ 4)
⎜ ⎟ =
k
)
i =1 ⎝ 4 ⎠ 1 − 1/ 4
1 ⎛ 1 ⎞
= ⎜1 − k ⎟ < 1/3
3 ⎝ 4 ⎠
Hence
we end up with
i i
⎧ k
⎛ 2 ⎞ k
⎛ 1 ⎞ ⎫
M(2 ) = 3 ⎨a0 + a1 ∑ ⎜ ⎟ + a 2 ∑ ⎜ ⎟ ⎬
k k
i =1 ⎝ 3 ⎠ i =1 ⎝ 3 ⎠
⎩ ⎭
where
i
k
⎛ 2 ⎞ ⎛ ⎛ 2 ⎞k ⎞
∑ ⎜ ⎟ = 2⎜⎜1 − ⎜ ⎟ ⎟⎟ < 2
i=1 ⎝ 3 ⎠
⎝ ⎝ 3 ⎠ ⎠
i k
k
⎛ 1 ⎞ 1 ⎛⎜ ⎛ 1 ⎞ ⎞⎟
∑ ⎜ ⎟ = ⎜1 − ⎜ ⎟ ⎟ < ½
i=1 ⎝ 3 ⎠ 2 ⎝ ⎝ 3 ⎠ ⎠
and hence
⎛ a ⎞
M(2k ) < 3 k ⎜ a 0 + 2a1 + 2 ⎟
⎝ 2 ⎠
log 3 ⎛ a ⎞
M(n) < n 2 ⎜ a0 + 2a1 + 2 ⎟
⎝ 2 ⎠
using
3log2 n ≡ (2log2 3 )log2 n
= (2log2 n )log2 3
= nlog2 3
Dividing into 3 parts of n/3 bits, and using a similar -- but more
complicated -- trick to reduce the number of multiplications in this
case to get just 5 (as opposed to the 9 that would be näively
required) leads by similar techniques to