Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | price-walters |
View: | 24 times |
Download: | 0 times |
RecursionRecursion
byby
Ender OzcanEnder Ozcan
Recursion in computingRecursion in computing
Recursion in computer programming Recursion in computer programming defines a function in terms of itself. defines a function in terms of itself.
AdvantageAdvantage: Infinite set of possible : Infinite set of possible sentences, designs or other data can be sentences, designs or other data can be defined, parsed or produced by a finite defined, parsed or produced by a finite computer program. computer program.
Example: the natural numbersExample: the natural numbers The canonical example of a recursively defined set is The canonical example of a recursively defined set is
given by the natural numbers:given by the natural numbers: 0 is in 0 is in NN if if nn is in is in NN, then , then nn + 1 is in + 1 is in NN The set of natural numbers is the smallest set The set of natural numbers is the smallest set
satisfying the previous two properties. satisfying the previous two properties. Here's an alternative recursive definition of Here's an alternative recursive definition of NN::
0, 1 are in 0, 1 are in NN; ; if if nn and and nn + 1 are in + 1 are in NN, then , then nn + 2 is in + 2 is in NN; ; NN is the smallest set satisfying the previous two is the smallest set satisfying the previous two
properties. properties.
Example: FactorialExample: Factorial
Computing 4!, requires a call to Fact(4)Computing 4!, requires a call to Fact(4) Fact(4) Fact(4) returns returns Fact(3)Fact(3)*4*4 66*4=*4=2424
Fact(3) Fact(3) returns returns Fact(2)Fact(2)*3*3 22*3=*3=66 Fact(2) Fact(2) returns returns Fact(1)Fact(1)*2*2 11*2=*2=22
Fact(1) Fact(1) returns 1 returns 1
integer Fact ( integer X ) {
if X < 0 then return -1; // invalid arg
if X = 1 then return 1;
return Fact(X-1) * X;
}
Basic steps of recursive programsBasic steps of recursive programs
Initialize the algorithm. Termination Criteria – check to see whether the
current value(s) being processed match the base case. If so, process and return the value.
Redefine the answer in terms of a smaller or simpler sub-problem or sub-problems.
Run the algorithm on the sub-problem. Combine the results in the formulation of the answer. Return the results.
Example – Sum of Example – Sum of nn numbers numbers
Suppose we have a list of Suppose we have a list of nn numbers, and we numbers, and we want to sum them. want to sum them.
1. Initialize the algorithm. This algorithm's seed value is the first number to process and is passed as a parameter to the function.
2. Check for the base case. The program needs to check and see if the list is empty. If so, we return zero because the sum of all members of an empty list is zero.
3. Redefine the answer in terms of a simpler sub-problem. We can define the answer as the sum of the rest of the list plus the contents of the current number. To determine the sum of the rest of the list, we call this function again with the next number.
4. Combine the results. After the recursive call completes, we add the value of the current node to the results of the recursive call.
Linked ListLinked List
data next
3
data next
159
data next
78
<3, 159, 78>
head tail
Linked list is a basic data structure, used to hold a sequence of items in memory
C implementation using linked listsC implementation using linked lists
int sum_list(struct list_node *l) int sum_list(struct list_node *l) { {
if(l == NULL) return 0; if(l == NULL) return 0; return l.data + sum_list(l.next); return l.data + sum_list(l.next);
} }
Comparing loops with recursive functionsComparing loops with recursive functions
PropertiesProperties LoopsLoops Recursive Recursive functionsfunctions
Repetition Repetition Execute the same block of code Execute the same block of code repeatedly to obtain the result; repeatedly to obtain the result; signal their intent to repeat by signal their intent to repeat by either finishing the block of either finishing the block of code or issuing a continue code or issuing a continue command. command.
Execute the same block Execute the same block of code repeatedly to of code repeatedly to obtain the result; signal obtain the result; signal their intent to repeat by their intent to repeat by repeat by calling repeat by calling themselves. themselves.
Terminating Terminating conditions conditions
In order to guarantee that it will In order to guarantee that it will terminate, a loop must have one terminate, a loop must have one or more conditions that cause it or more conditions that cause it to terminate and it must be to terminate and it must be guaranteed at some point to hit guaranteed at some point to hit one of these conditions. one of these conditions.
In order to guarantee that In order to guarantee that it will terminate, a it will terminate, a recursive function recursive function requires a base case that requires a base case that causes the function to causes the function to stop recursing. stop recursing.
State State Current state is updated as the Current state is updated as the loop progresses. loop progresses.
Current state is passed as Current state is passed as parameters. parameters.
To Loop or Not To LoopTo Loop or Not To Loop
Rule of thumb for programmersRule of thumb for programmers
If you can solve a problem using If you can solve a problem using iteration, avoid recursioniteration, avoid recursion
Fibonacci SequenceFibonacci Sequence
<0, 1, 1, 2, 3, 5, 8, 13, 21, <0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 34, 55, 89, 144, 233, 377, 610, 987, …>610, 987, …>
The original problem that The original problem that Fibonacci investigated (in Fibonacci investigated (in the year 1202) was about the year 1202) was about how fast rabbits could how fast rabbits could breed in ideal breed in ideal circumstances (assuming circumstances (assuming they don’t die). they don’t die).
nnthth Fibonacci Number Fibonacci Number
fibfib((nn) = ) = fibfib((n n – 1) + – 1) + fibfib ( (n n – 2) for n>1, and – 2) for n>1, and fibfib(0)=0, (0)=0, fibfib(1)=1.(1)=1.
C program computing nC program computing nthth fibonacci fibonacci number:number:
int fib(int n) int fib(int n) { {
if (n == 0) return 0; if (n == 0) return 0; if (n == 1) return 1;if (n == 1) return 1;return fib(n-1) + fib(n-2); return fib(n-1) + fib(n-2);
} }
nnthth Fibonacci Number-Revisited Fibonacci Number-Revisited
fib(5)
fib(4) fib(3)
fib(3) fib(2) fib(1) fib(2)
fib(1) fib(0)fib(1) fib(0)fib(1) fib(2)
fib(1) fib(0)Try to avoid solving overlapping sub-problem instances
Recursive vs. Iterative SolutionRecursive vs. Iterative Solution
int fib(int n) {
if (n == 0) return 0; if (n == 1) return 1;return fib(n-1) + fib(n-2);
}
int fib(int n) {
int i, *fib=new int[n];fib[0]=0;fib[1]=1;
for (i=2; i<n; i++)fib[i]=fib[i-1]+fib[i-
2];return fib[n];
} Running time is ~ a(n+n)
Running time is ~ cn
Tower of Hanoi PuzzleTower of Hanoi Puzzle
Puzzle was invented by the French Puzzle was invented by the French mathematician Edouard Lucas in 1883.mathematician Edouard Lucas in 1883.
We are given a tower of 8 disks, initially We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to The objective is to transfer the entire tower to one of the other pegs, moving only one disk at one of the other pegs, moving only one disk at a time and never a larger one onto a smaller. a time and never a larger one onto a smaller.
Recursive Solution Recursive Solution
Call the three pegs Src (Source), Aux Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). (Auxiliary) and Dst (Destination).
Src Aux Dst
n disks
Recursive Solution – Step 1Recursive Solution – Step 1
Move the top Move the top nn-1 disks from Src to Aux (using -1 disks from Src to Aux (using Dst as an intermediary peg)Dst as an intermediary peg)
n-1 disks
Src Aux Dst
Recursive Solution – Step 2Recursive Solution – Step 2
Move the bottom disks from Src to Dst Move the bottom disks from Src to Dst
Src Aux Dst
Recursive Solution – Step 3Recursive Solution – Step 3
Move Move nn-1 disks from Aux to Dst (using Src as -1 disks from Aux to Dst (using Src as an intermediary peg) an intermediary peg)
Src Aux Dst
PseudocodePseudocode
Solve(n, Src, Aux, Dst) Solve(n, Src, Aux, Dst) if n is 0 return if n is 0 return Solve(n-1, Src, Dst, Aux)Solve(n-1, Src, Dst, Aux) Move from Src to Dst Move from Src to Dst Solve(n-1, Aux, Src, Dst) Solve(n-1, Aux, Src, Dst)
Analysis of the Algorithm IAnalysis of the Algorithm I
Assume than T(Assume than T(nn) is the number of steps ) is the number of steps required to move required to move nn disks from Src to Dst disks from Src to Dst
Src Aux Dst
n disks
Analysis of the Algorithm IIAnalysis of the Algorithm II
Moving the top Moving the top nn-1 disks from Src to Aux will -1 disks from Src to Aux will take T(take T(nn-1) steps-1) steps
n-1 disks
Src Aux Dst
Analysis of the Algorithm IIIAnalysis of the Algorithm III
Moving the bottom disk from Src to Dst will Moving the bottom disk from Src to Dst will take a single steptake a single step
Src Aux Dst
Analysis of the Algorithm IVAnalysis of the Algorithm IV
Moving Moving nn-1 disks from Aux to Dst will take -1 disks from Aux to Dst will take T(T(nn-1) steps-1) steps
Src Aux Dst
Overall ComplexityOverall Complexity
T(T(nn) = 2T() = 2T(nn-1) + 1 {T(-1) + 1 {T(nn-1) = 2T(-1) = 2T(nn-2) + 1 }-2) + 1 } T(T(nn) = 2) = 222T(T(nn-2) + 2 + 1-2) + 2 + 1 substitute T(n-1) substitute T(n-1) T(n) = 2T(n) = 233T(T(nn-3)+ 2-3)+ 22 2 + 2 + 1+ 2 + 1 …… T(T(nn) = 2) = 2nn-1 -1 T( T( nn-(-(nn-1))-1)) +…+ 2 +…+ 23 3 +2+222+2+1+2+1
T(1)=1T(1)=1 T(T(nn) = 2) = 2nn-1 -1 +…+ 2+…+ 23 3 +2+222+2+1 (geometric series)+2+1 (geometric series)
T(T(nn) = 2) = 2nn - 1- 1