Download presentation
Presentation is loading. Please wait.
Published byCollin Moody Modified over 8 years ago
1
Recursion by Ender Ozcan
2
Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming defines a function in terms of itself. Advantage: Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program. Advantage: Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.
3
Example: the natural numbers The canonical example of a recursively defined set is given by the natural numbers: The canonical example of a recursively defined set is given by the natural numbers: 0 is in N 0 is in N if n is in N, then n + 1 is in N if n is in N, then n + 1 is in N The set of natural numbers is the smallest set satisfying the previous two properties. The set of natural numbers is the smallest set satisfying the previous two properties. Here's an alternative recursive definition of N: Here's an alternative recursive definition of N: 0, 1 are in N; 0, 1 are in N; if n and n + 1 are in N, then n + 2 is in N; if n and n + 1 are in N, then n + 2 is in N; N is the smallest set satisfying the previous two properties. N is the smallest set satisfying the previous two properties.
4
Example: Factorial Computing 4!, requires a call to Fact(4) Computing 4!, requires a call to Fact(4) Fact(4) returns Fact(3)*46*4=24 Fact(4) returns Fact(3)*46*4=24 Fact(3) returns Fact(2)*32*3=6 Fact(3) returns Fact(2)*32*3=6 Fact(2) returns Fact(1)*21*2=2 Fact(2) returns Fact(1)*21*2=2 Fact(1) returns 1 Fact(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; }
5
Basic 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.
6
Example – Sum of n numbers Suppose we have a list of n numbers, and we want to sum them. Suppose we have a list of n numbers, and we 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.
7
Linked List datanext 3 datanext 159 datanext 78 headtail Linked list is a basic data structure, used to hold a sequence of items in memory
8
C implementation using linked lists int sum_list(struct list_node *l) { if(l == NULL) return 0; return l.data + sum_list(l.next); }
9
Comparing loops with recursive functions PropertiesLoops Recursive functions Repetition Execute the same block of code repeatedly to obtain the result; signal their intent to repeat by either finishing the block of code or issuing a continue command. Execute the same block of code repeatedly to obtain the result; signal their intent to repeat by repeat by calling themselves. Terminating conditions In order to guarantee that it will terminate, a loop must have one or more conditions that cause it to terminate and it must be guaranteed at some point to hit one of these conditions. In order to guarantee that it will terminate, a recursive function requires a base case that causes the function to stop recursing. State Current state is updated as the loop progresses. Current state is passed as parameters.
10
To Loop or Not To Loop Rule of thumb for programmers If you can solve a problem using iteration, avoid recursion If you can solve a problem using iteration, avoid recursion
11
Fibonacci Sequence The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances (assuming they don’t die). The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances (assuming they don’t die).
12
n th Fibonacci Number fib(n) = fib(n – 1) + fib (n – 2) for n>1, and fib(0)=0, fib(1)=1. fib(n) = fib(n – 1) + fib (n – 2) for n>1, and fib(0)=0, fib(1)=1. C program computing n th fibonacci number: C program computing n th fibonacci number: int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); }
13
n th Fibonacci Number-Revisited fib(5) fib(4)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
14
Recursive 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
15
Tower of Hanoi Puzzle Puzzle was invented by the French mathematician Edouard Lucas in 1883. Puzzle was invented by the French mathematician Edouard Lucas in 1883. We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller. We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller.
16
Recursive Solution Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). SrcAuxDst n disks
17
Recursive Solution – Step 1 Move the top n-1 disks from Src to Aux (using Dst as an intermediary peg) Move the top n-1 disks from Src to Aux (using Dst as an intermediary peg) n-1 disks SrcAuxDst
18
Recursive Solution – Step 2 Move the bottom disks from Src to Dst Move the bottom disks from Src to Dst SrcAuxDst
19
Recursive Solution – Step 3 Move n-1 disks from Aux to Dst (using Src as an intermediary peg) Move n-1 disks from Aux to Dst (using Src as an intermediary peg) SrcAuxDst
20
Pseudocode 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)
21
Analysis of the Algorithm I Assume than T(n) is the number of steps required to move n disks from Src to Dst Assume than T(n) is the number of steps required to move n disks from Src to Dst SrcAuxDst n disks
22
Analysis of the Algorithm II Moving the top n-1 disks from Src to Aux will take T(n-1) steps Moving the top n-1 disks from Src to Aux will take T(n-1) steps n-1 disks SrcAuxDst
23
Analysis of the Algorithm III Moving the bottom disk from Src to Dst will take a single step Moving the bottom disk from Src to Dst will take a single step SrcAuxDst
24
Analysis of the Algorithm IV Moving n-1 disks from Aux to Dst will take T(n-1) steps Moving n-1 disks from Aux to Dst will take T(n-1) steps SrcAuxDst
25
Overall Complexity T(n) = 2T(n-1) + 1 {T(n-1) = 2T(n-2) + 1 } T(n) = 2T(n-1) + 1 {T(n-1) = 2T(n-2) + 1 } T(n) = 2 2 T(n-2) + 2 + 1 substitute T(n-1) T(n) = 2 2 T(n-2) + 2 + 1 substitute T(n-1) T(n) = 2 3 T(n-3)+ 2 2 + 2 + 1 T(n) = 2 3 T(n-3)+ 2 2 + 2 + 1 … T(n) = 2 n-1 T( n-(n-1)) +…+ 2 3 +2 2 +2+1 T(n) = 2 n-1 T( n-(n-1)) +…+ 2 3 +2 2 +2+1T(1)=1 T(n) = 2 n-1 +…+ 2 3 +2 2 +2+1 (geometric series) T(n) = 2 n-1 +…+ 2 3 +2 2 +2+1 (geometric series) T(n) = 2 n - 1 T(n) = 2 n - 1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.