Recursion: Understanding the Concept and Applications
E N D
Presentation Transcript
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.
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
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
Consider the function F(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
Concept of recursion • The concept of terminating condition(s) is/are needed. • It is determined by the parameter(s).
Consider the function F(n) IF (n=0) RETURN 1 ELSE RETURN 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 Concept of recursion
Structure of recursion • Similar to mathematic induction, recursion requires the following two components • Recurrence relation(s) • Base case(s) • Example
Recurrence in mathematical functions • Factorial • _ • Permutation • _ • Combination • _ • Pascal relationship • _ • Integer power • _
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).
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])
i i i Stack Problem Discussion • Suppose a[ ]=(7,8,9) F(0) * * * * * * F(1) i=1 i=1 i=1 i=1 i=1 i=1 F(2) i=1___i=2___ i=1___i=2___ i=1___i=2___ F(3) i=1____________i=2____________i=3____________ 7 7 9 7 9 8 9 8 8 1 2 1 Output: 897, 987, 978, 798, 879, 789 3 1 2
Complexity of recursion • First Order Linear Recurrence • In particular, suppose a>1, c≠-1
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)
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)
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)
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))
Complexity of recursion • Master Theorem • In particular, suppose b>1, _ _ _
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)
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.
Challenge Problems • Consider the following recursive function defined on triple of nonnegative numbers. • Find the closed form of the function. Prove your answer geometrically.
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/