Lecture 03
Lecture 03
Chapter Objectives
Learn about recursive definitions n Explore the base case and the general case of a recursive definition n Explore how to use recursive functions to implement recursive algorithms n Complexity analysis of recursive functions n Recursion and backtracking
n
Recursive Definitions
n
Recursion
Process of solving a problem by reducing it to smaller versions of itself n It can be either
n n
Recursive Definitions
Recursive algorithm n Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself n Has one or more base cases n Implemented using recursive functions n Recursive function n A function that calls itself is called a recursive function
n
Recursive Definitions
Direct Recursion : a function that calls itself n Indirect Recursion : a function that calls another function and eventually results in the original function call n Receive(buffer)Decode(buffer) Store(b uffer)Receivi(buffer). n Nested Recursion: the argument of the function is a smaller recursive call of the same function n F(n) = F( F(n-1) + 2);
n
Infinite Recursion : the case where every recursive call results in another recursive call
Recursive Definitions
Anchor, ground, or base case: n Case in recursive definition in which the solution is obtained directly n Stops the recursion n General case: n Case in recursive definition in which a smaller version of itself is called n Must eventually be reduced to a base case
n
Recursion: Example
Classic linear recursion example--the factorial function: n n! = 1 2 3 (n-1) n n Recursive definition:
n
In this example, factorial(0) is called the base case (there should be at least one) and factorial(n) for n > 0 is called recursive case.
Do
Understand problem requirements n Identify base cases n Provide direct solution to each base case n Identify general case(s) n Provide solutions to general cases in terms of smaller versions of the problem itself
n
Recursive Method
n
Binary recursion occurs whenever there are two recursive calls for each non-base case.
Algorithm BinaryRecSum(A, i, n): Input: An array A and integers i and n Output: The sum of the n integers in A starting at index i BinaryRecSum(A, 0, 5) = 59 if n = 1 then return A[i] return BinaryRecSum(A, i, Ceil(n/ 2)) + BinaryRecSum(A, i + ceil(n/ 2), floor(n/ 2))
n Provide direct solution to each base case n Identify general case(s) n Provide solutions to general cases in terms of smaller versions of itself
Unary recursion occurs whenever there is one recursive call for each non-base case.
Algorithm LinearRecSum(A, i, n): Input: An array A and integers i and n Output: The sum of the n integers in A starting at index i LinearSum(A, 0, 5) = 59 if n = 1 then return A[i ] Else return A[i]+ LinearRecSum(A, i+1, n-1)
Space Complexity
What is the size OS stack required to execute LinearRecSum and BinaryRecSum, given that the size of the array is n?? (Hint: use the size of the activation block)
A =
base case and for the general case. (typically it is a recurrence function for the general case)
Recurrence Relationship for the Number of Basic Operations vs. the Original Recursive Function n The recurrence function for the number of basic operations is different from the original recursive function n Factorial
F(n) := 1 if n = 1 n F(n) := F(n-1) * n if n >1
n n
Ex 1 n C(n) = C(n-1) +4
n n n n n n n
C(1) = 2
C(n-1) = C(n-2) + 4 C(n-2) = C(n-3) + 4 : : C(1) = 2 C(n) = (c(1) + 4)+4)+4+4) + 4 C(n) = ((((( 2) + 4)+4)+4+4) + 4 //with n-
1 4s n C(n) = (n-1) * 4 + 2 = 4n -2
Ex. Printing a list in reverse order ReversePrint (A,n) if n=1 then print A[n-1] // the last element is at index n1 else ReversePrint(A,n-1); print last element in A, A[n-1]; print Next Element
Output: The sum of the n integers in A starting at index i LinearSum(A, 0, 5) = 59 if n = 1 then return A[i ] Else return A[i]+ LinearRecSum(A, i+1, n-1) Solution
A recursive call of the function reduce the size of the problem by one, and performs b*n basic operations.
C(n) = C(n-1) + bn C(1) = d Solution: C(n) = [n(n+1)/2 1] b + d quadratic
RecSelectionSort(A,n)
if n>1 then
e = FindLargestElement(A,n); A[0]e; RecSelectionSort(A,n-1);
Approximating the Order of Growth of the Recurrence Relation: Master Method Apply to Divide-and-Conquer Cases
If a problem of size n is solved recursively by diving the problem into a sub-problems, each of size n/b, and if the amount of work required to divided the problem and to combine the solutions is f(n) then we can say that:
C(n) = aC(n/b) + f (n)
n If a < bk then n If a = bk then n If a > bk then
n C(n) = 2C(n/2) + n n a=2, b=2, k=1, a = bk => C(n) is O(nlg n) n C(n) = 3 C(n/2) + n n a=3, b=2, k=1, a > bk => C(n) is O(nlog 3)
2
C(n) is O(nlog
Fibonacci Numbers
n n
Another example:
C(n) = C(n-1) 2C(n-2) C(0) = 1 C (1) = 3
Measuring the execution time sometime may help make a decision, especially when using as part of a real-time system n Recursion should be avoided if some part of the work is unnecessarily repeated to compute an answer, like in the case of Fibonacci
n
Backtracking Algorithm
In solving a problem, you might come a cross-point where you have to choose between different paths to find a solution. n Some/all of the paths might lead to no
n
solution n Backtracking attempts to find solutions to a problem by constructing partial solutions n Backtracking makes sure that any partial solution does not violate the problem requirements n Backtracking tries to extend partial solution towards completion n Recursion serve as a natural implementation of backtracking.
Backtracking Algorithm
n
dome which marks the centre of the World, rests a brass plate in which are placed 3 diamond needles, each a cubit high and as thick as the body of a bee. On one of these needles, at the creation, god placed 64 discs of pure gold, the largest disc resting on the brass plate and the others getting smaller and smaller up to the top one. This is the tower of Brahma. Day and night unceasingly the priests transfer the discs from one diamond needle to another according to the fixed and immutable laws of Brahma, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. When the 64 discs shall have been thus transferred from the needle on which at the creation god placed them to one of the other needles, tower, temple and Brahmans alike will crumble into dust and with a thunder clap the world will vanish.
if the priests were able to move disks at a rate of one disk per second, using the smallest number of moves, it will take them 2641 seconds or roughly 584.542 billion years o move 64 disks.
n n
See it at http://www.cut-theknot.org/recurrence/hanoi.shtml
Play it at http://www.mazeworks.com/hanoi/
Provide direct solution to each base case n Identify general case(s) n Provide solutions to general cases in terms of smaller versions of itself
n
} }
C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls).
n Solve the recurrence to obtain a closed form or estimate
the order of magnitude of the solution. You can use by backward substitutions or another method.
Recurrence function
C(n) = C(n-1) + 1 + C(n-1) = 2 C(n-1) + 1 n C(n) = 2 ( 2C(n-2) +1) + 1 = 22C(n-2) + 2 +1 : : n C(n) = = 2iC(n-i) + 2i-1 + 2i-2+1 n We arrive at the base case when i = n-1
n
0 i n
2i = 20 + 21 ++ 2n
n n
Questions
Questions
Questions
Questions
Questions
Questions