Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 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 The concept of terminating condition(s) is/are needed. It is determined by the parameter(s). Concept of recursion Consider the function F(n) IF (n=0) RETURN 1 ELSE RETURN n*F(n-1) n n n Stack When F(2) is called, F(2)=2*F(1) =2*(1*F(0)) =2*(1*(1)) =2*(1*1) =2*1 =2 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 r nn1 Pr _C n r (n / r )n1 Cr Pascal relationship _P n Combination n_! n (n 1)! _C n r n1 Cr 1 n1Cr Integer power x x x _n n 1 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). 1 when N 0 f (N ) 2 when N 1 f ( N 1) f ( N 2) when N 2 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=1 F(2) i=1___i=2___ i=1___i=2___ i=1___i=2___ F(3) i=1____________i=2____________i=3____________ a[ ] 7 9 8 7 8 9 7 9 8 Output: 897, 987, 978, 798, 879, 789 i 1 i 1 2 i 1 3 2 Stack Complexity of recursion First Order Linear Recurrence h(0) n0 f ( n) g (n) f (n 1) h(n) n 0 n n i 0 j i 1 f (n) h(i) g ( j ) In particular, suppose a>1, c≠-1 f (n) f (n 1) (n c ) f (n) (n c 1 ) f (n) a f (n 1) b f (n) (a n ) Complexity of recursion _ Recurrence Iteration and Recursion Tree f (n) 2 f (n 1) 1 f(4) =1+2f(3) =1+2+4f(2) =1+2+4+8f(1) =1+2+4+8+f(0) Complexity of recursion _f (n) f (n 1) n 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 n _ f ( n) f ( ) n 2 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 ( n) 2 f ( n )n 2 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 T (n) aT (n / b) f (n) where a 1, b 1 logb a logb a T ( n ) ( n ) if f (n) (n ) for some 0 T ( n ) ( n logb a log n) if f (n) (n logb a ) T (n) ( f (n)) if f (n) (n logb a ) for some 0 _ _ _ In particular, suppose b>1, f ( n ) T ( n / b ) n f ( n ) ( n ) f (n) bT (n / b) n f (n) (n log n) 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 p/2 2 (B ) p B p / 2 2 ) B ( B Θ(log p) when B is even when B is odd 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 Conquer Break a problem into sub problem(s) 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. 0 when m 0 f (m, n) 0 when n 0 f (m 1, n 1) m n 1 otherwise Find the closed form of the function. Prove your answer algebraically. Challenge Problems Consider the following recursive function defined on triple of nonnegative numbers. a b f (a, b, c) c f (a 1, b 1, c 1) ab bc ac a b c 1 when a 0 when b 0 when c 0 otherwise 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/