Upload
albert-mcdonald
View
215
Download
2
Embed Size (px)
Citation preview
Recursion,Divide and Conquer
Lam Chi Kit (George)
HKOI2007
Meaning of recursion
To recur means to happen again. In computer science, we say that a
subroutine (function or procedure) is recursive when it calls itself one or more times.
We call the programming technique by using recursive subroutine(s) as recursion.
The correctness, time complexity and space complexity can be proved by mathematical induction.
Concept of recursion
Consider the function.F()
temp←2*F()RETURN temp
When F() is called,F()=temp=2*F()It will not solve the equation. It continues.
=2*(2*F()) =2*(2*(2*F())) ...
temp
temp
temp
Stack
Concept of recursion
The concept of parameter(s) is needed. This can be implemented by:
Pass by value Pass by pointer Pass by reference Use of global variable
Concept of recursion
Consider the functionF(n)
temp←n*F(n-1)RETURN temp
When F(1) is called,F(1)=temp=1*F(0) =1*(0*F(-1))It will not simplify the expression. It continues.
=1*(0*(-1*F(-2))) ...
n, temp
n, temp
n, temp
Stack
Concept of recursion
The concept of terminating condition(s) is/are needed.
It is determined by the parameter(s).
Concept of recursion
Consider the functionF(n)
IF (n=0)RETURN 1
ELSERETURN n*F(n-1)
When F(2) is called,F(2)=2*F(1) =2*(1*F(0)) =2*(1*(1)) =2*(1*1) =2*1 =2
n
n
n
Stack
Structure of recursion
Similar to mathematic induction, recursion requires the following two components Recurrence relation(s) Base case(s)
Example
F(n)
IF (n=0) RETURN 1 //Base Case
ELSE RETURN n*F(n-1)
//Recurrence Relation
Recurrence in mathematical functions Factorial
_ Permutation
_ Combination
_ Pascal relationship
_ Integer power
_
)!1(! nnn
rnrn CrnC 1)/(
rnrnrn CCC 111
rnrn PnP 1
1 nn xxx
Problem Discussion
S is a string of N bits (either ‘0’ or ‘1’). Furthermore, there is no two consecutive ‘0’s in S. Find the number of solutions to S. Denote S(N) as any strings of N bits with no two
consecutive ‘0’s. When N>=2, N is either
Beginning with ‘1’, followed by S(N-1) Beginning with ’01’, followed by S(N-2)
S(1) must either be ‘0’ or ‘1’. S(0) is the null string ‘’.
Problem Discussion
Let f(N) be the number of solutions to S(N).
2when )2()1(
1when 2
0when 1
)(
NNfNf
N
N
Nf
Problem Discussion
This puzzle is called the Tower of Hanoi. There are three pegs and N disks of different
sizes. Initially all disks are stacked on the same peg,
with the smaller disk on top of the larger disk. In each step, you can move a disk from the top of
a stack to the top of another peg, provided that this will not result in a larger disk stacked on top of a smaller disk.
Your goal is to move all disks to another peg.
Problem Discussion
Suppose N=4
Problem Discussion
Suppose procedure MOVE_DISK(N,A,B) moves a single disk N from
A to B within one step. F(N,X,Y,Z) can move a stack of N disks from peg
X to peg Z via peg Y F(N,X,Y,Z) where N≥1
F(N-1,X,Z,Y) MOVE_DISK(N,X,Z) F(N-1,Y,Z,X)
Problem Discussion
Given a sequence of n distinct integers. Output all permutations of the sequence.
Let F(n) be a procedure that permute a[1..n]. When n>0, for all 1≤i ≤ n, swap a[i] to a[n] and then permute
a[1..n-1] by calling F(n-1)F(n) IF(n=0) OUTPUT ELSE FOR i = 1 to n SWAP(a[i],a[n]) F(n-1) SWAP(a[i],a[n])
Problem Discussion
Suppose a[ ]=(7,8,9)F(0) * * * * * *F(1) i=1 i=1 i=1 i=1 i=1 i=1F(2) i=1___i=2___ i=1___i=2___ i=1___i=2___F(3) i=1____________i=2____________i=3____________
i
i
i
Stack
9
23
a[ ] 87
1
987 987
21
1
Output: 897, 987, 978, 798, 879, 789
Complexity of recursion
First Order Linear Recurrence
In particular, suppose a>1, c≠-1
0)()1()(
0)0()(
nnhnfng
nhnf
n
i
n
ij
jgihnf0 1
)()()(
)()()1()(
)()()()1()( 1
n
cc
anfbnfanf
nnfnnfnf
Complexity of recursion
Recurrence Iteration and Recursion Tree
_f(4)=1+2f(3)=1+2+4f(2)=1+2+4+8f(1)=1+2+4+8+f(0)
1)1(2)( nfnf
Complexity of recursion
_f(8)=8+f(7)=8+7+f(6)=8+7+6+f(5)=8+7+6+5+f(4)=8+7+6+5+4+f(3)=8+7+6+5+4+3+f(2)=8+7+6+5+4+3+2+f(1)= 8+7+6+5+4+3+2+1+f(0)
nnfnf )1()(
Complexity of recursion
_f(256)=256+f(128)=256+128+f(64)=256+128+64+f(32)=256+128+64+32+f(16)=256+128+64+32+16+f(8)=256+128+64+32+16+8+f(4)=256+128+64+32+16+8+4+f(2)=256+128+64+32+16+8+4+2+f(1)
nn
fnf )2
()(
Complexity of recursion
_
f(32)=32+2(f(16))=32+2(16)+4(f(8))=32+2(16)+4(8)+8(f(4))=32+2(16)+4(8)+8(4)+16(f(2))=32+2(16)+4(8)+8(4)+16(2)+32(f(1))
nn
fnf )2
(2)(
Complexity of recursion
Master Theorem
In particular, suppose b>1,___
1,1 where)()/()( banfbnaTnT
0 somefor )()( if ))(()(
)()( if )log()(
0 somefor )()( if )()(
log
loglog
loglog
a
aa
aa
b
bb
bb
nnfnfnT
nnfnnnT
nnfnnT
)log()()/()(
)()()/()(
nnnfnbnbTnf
nnfnbnTnf
Problem Discussion
Compute Bp mod M B,P are integers in [0,2147483647] M is an integer in [1,46340]
f(p)=B*f(p-1) Bp= B*Bp-1
Θ(p)
Problem Discussion
Notice that
Θ(log p)
odd is when )(
even is when )(22/
22/
BBB
BBB
p
pp
Properties of recursion
NP solutions, including exponential, factorial and combinatorial, are usually easy to be implemented by recursion.
It requires more memory than iteration. Every recursive function can be transformed
into iterative function. Every iterative function can be transformed
into recursive function.
Meaning of Divide and Conquer In military, “divide and conquer” is a kind of
strategy. In computer science, “divide and conquer” is
a programming technique by breaking down a problem into one or more sub problems.
“Divide and conquer” is usually implemented by recursion.
Structure of Divide and Conquer Divide
Break a problem into sub problem(s) Conquer
Solve all sub problem(s) Combine
Solve the problem using the results of the sub problem(s)
Problem Discussion
You are given a 2k* 2k square board with one square taken away.
An “L piece” is made up of 3 squares sharing a common corner. You can freely rotate the any of these pieces.
You are required to cover the board with these pieces without overlapping.
Problem Discussion
Example
Problem Discussion
Illustration
Recursion outside Computer Science Names
PHP : PHP Hypertext Preprocessor GNU : GNU is Not Unix
Mathematics Definition of the set of non negative numbers N
0 is in N If n is in N, then n+1 is in N.
Images Fractals
Challenge Problems
Consider the following recursive function defined on pairs of nonnegative numbers.
Find the closed form of the function. Prove your answer algebraically.
otherwise1)1,1(
0when 0
0when 0
),(
nmnmf
n
m
nmf
Challenge Problems
Consider the following recursive function defined on triple of nonnegative numbers.
Find the closed form of the function. Prove your answer geometrically.
otherwise1
)1,1,1(0when
0when
0when
),,(
cbaacbcab
cbafcc
bb
aa
cbaf
Challenge Problems
Find a closed form solution for the nth Fibonacci Number.
Prove the Master Theorem.
Challenge Problems
Consider the following algorithm. F(x,p)
IF (p=0) RETURN 1 IF (p=1) RETURN x BINARY_SEARCH for the largest int q s.t. q2≤p RETURN F(F(x,q),q)*F(x,p-q2)
What is the purpose of F(x,p)? Prove your answer.
Challenge Problems
Describe an efficient non recursive algorithm to compute Bp mod M. B,P are integers in [0,2147483647] M is an integer in [1,46340]
State the time and space complexity.
Challenge Problems
Consider the problem “Tower of Hanoi”. Describe an algorithm which requires the
minimum number of moves. Prove that it requires the minimum number of moves.
Show that this sequence of moves is unique. Calculate the minimum number of moves
required in terms of n.
Challenge Problems
Consider the problem “Tower of Hanoi”. Suppose you have to move a stack from Peg 0 to
Peg 2 via Peg 1. Suppose the ith largest disk is on Peg pi(t) after t
moves. Denote hi(t) be the number of disks under the ith
largest disk after t moves. Consider the algorithm that requires the minimum
number of moves. Show that pi(t)+hi(t)+i is always odd.
Challenge Problems
Consider the problem “L pieces”. Explain whether it is possible to have a faster
algorithm in terms of complexity. Calculate the worst case minimum of colours
required such that no two pieces sharing a common edge has the same colour.
Reference
http://en.wikipedia.org/ http://www.hkoi.org/