Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.

Similar presentations


Presentation on theme: "Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007."— Presentation transcript:

1 Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007

2 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.

3 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 Stack

4 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

5 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 Stack

6 Concept of recursion The concept of terminating condition(s) is/are needed. It is determined by the parameter(s).

7 Concept of recursion 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

8 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

9 Recurrence in mathematical functions Factorial __ Permutation __ Combination __ Pascal relationship __ Integer power __

10 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 ‘’.

11 Problem Discussion  Let f(N) be the number of solutions to S(N).

12 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.

13 Problem Discussion Suppose N=4

14 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)

15 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])

16 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____________ i i i Stack 9 2 3 a[ ] 8 7 1 98 79 8 7 21 1 Output: 897, 987, 978, 798, 879, 789

17 Complexity of recursion First Order Linear Recurrence  In particular, suppose a>1, c≠-1

18 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)

19 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)

20 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)

21 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))

22 Complexity of recursion Master Theorem  In particular, suppose b>1, _ _ _

23 Problem Discussion Compute B p mod M  B,P are integers in [0,2147483647]  M is an integer in [1,46340] f(p)=B*f(p-1)  B p = B*B p-1  Θ(p)

24 Problem Discussion Notice that Θ(log p)

25 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.

26 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.

27 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)

28 Problem Discussion You are given a 2 k * 2 k 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.

29 Problem Discussion Example

30 Problem Discussion Illustration

31 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

32 Challenge Problems Consider the following recursive function defined on pairs of nonnegative numbers. Find the closed form of the function. Prove your answer algebraically.

33 Challenge Problems Consider the following recursive function defined on triple of nonnegative numbers. Find the closed form of the function. Prove your answer geometrically.

34 Challenge Problems Find a closed form solution for the n th Fibonacci Number. Prove the Master Theorem.

35 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. q 2 ≤p RETURN F(F(x,q),q)*F(x,p-q 2 ) What is the purpose of F(x,p)? Prove your answer.

36 Challenge Problems Describe an efficient non recursive algorithm to compute B p mod M.  B,P are integers in [0,2147483647]  M is an integer in [1,46340] State the time and space complexity.

37 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.

38 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 i th largest disk is on Peg p i (t) after t moves. Denote h i (t) be the number of disks under the i th largest disk after t moves. Consider the algorithm that requires the minimum number of moves. Show that p i (t)+h i (t)+i is always odd.

39 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.

40 Reference http://en.wikipedia.org/ http://www.hkoi.org/


Download ppt "Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007."

Similar presentations


Ads by Google