chapter 02 recursion compiled by: dr. mohammad omar alhawarat

Download CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat

Post on 12-Jan-2016

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

Slide 1

Chapter 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat1Recursive DefinitionsConsider the following list of numbers:24, 88, 40, 37Such a list can be defined recursively:A LIST is a:numberor a:number comma LISTThat is, a LIST can be a number, or a number followed by a comma followed by a LISTThe concept of a LIST is used to define itself2Tracing the recursive definition of a listLIST:numbercommaLIST 24 ,88, 40, 37

numbercommaLIST 88 ,40, 37

numbercommaLIST 40 , 37

number 373What Is Recursion?It is a problem-solving processBreaks a problem into identical but smaller problemsEventually you reach a smallest problemAnswer is obvious or trivialUsing that solution enables you to solve the previous problemsEventually the original problem is solved

4Recursive ThinkingRecursion is a programming technique in which a method can call itself in order to fulfill its purposeA recursive definition is one which uses the word or concept being defined in the definition itselfIn some situations, a recursive definition can be an appropriate way to express a conceptBefore applying recursion to programming, it is best to practice thinking recursively5Infinite RecursionAll recursive definitions must have a non-recursive partIf they don't, there is no way to terminate the recursive pathA definition without a non-recursive part causes infinite recursionThis problem is similar to an infinite loop -- with the definition itself causing the infinite loopingThe non-recursive part often is called the base case6Parts of a Recursive DefinitionEvery recursive definition contains two parts:a base case, which is non-recursive and, consequently, terminates the recursive application of the rule. a recursive case, which reapplies a rule. 7Direct vs. Indirect RecursionA method invoking itself is considered to be direct recursionA method could invoke another method, which invokes another, etc., until eventually the original method is invoked againFor example, method m1 could invoke m2, which invokes m3, which invokes m1 againThis is called indirect recursionIt is often more difficult to trace and debug

8Direct vs. Indirect Recursionm1m2m3m1m2m3m1m2m39Phases of RecursionForward Phase:Every recursion has a forward phase in which a call at every level, except the last, spins off a call to the next level, and waits for the latter call to return control it.

Backward Phase: Every recursion has a backtracking phase in which a call at every level, except the first, passes control back to the previous level, at which point the call waiting at the previous level wakes up and resumes its work. 10Examples: Simple problemsSummationFactorialFibonacci11Summation

12The sum of 1 to N, defined recursivelyint sum (int num){ int result;

if (num == 1) result = 1; else result = num + sum(num-1);

return result;}

Base caseRecursive case13Recursive calls to the sum methodmainsumsumsumsumresult = 4 + sum(3) sum(4)sum(3)sum(2)sum(1)result = 1result = 3 + sum(2)result = 2 + sum(1) 14FactorialMathematical formulas are often expressed recursivelyN!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusiveThis definition can be expressed recursively:1! = 1N! = N * (N-1)!A factorial is defined in terms of another factorial until the base case of 1! is reached15Factorial FunctionN is 1 x 2 x 3 x ... x N 1 x N

16Computing the FactorialA recursive program implements a recursive definition.

The main work in writing a recursive method to solve a specific problem is to define base cases.

17Computing the Factorial (Cont.)

18Fibonacci Sequence

fibn 1 + fibn 2, if n > 1 recursive casefibn =0, if n == 0 base case1, if n == 1 base case19Fibonacci SequenceDefinition of F(4) spins off two chains of recursion, one on F(3) and another on F(2).Every chain either ends in F(0) or F(1)

20Computing the Fibonacci Sequence

21Computing the Fibonacci Sequence (Cont.)Every call waits on two subsequent calls.These waits are not simultaneous.For every call there is a wait-wakeup-wait-wakeup cycle.Except the F(0) and F(1).There are two forward phases and two backtracking phases.

22Avoiding Recursion23int fibo(int n){

if (n < 2) return 1; else return fibo(n-1) + fibo(n-2);}

23Avoiding Recursion

2424Avoiding Recursion25int fibo(int n){ int f[n]; f[0]=0; f[1]=1;

for (int i=2; i

Recommended

View more >