Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Algorithms 1 Building a Ruler: drawRuler()

Similar presentations


Presentation on theme: "Recursive Algorithms 1 Building a Ruler: drawRuler()"— Presentation transcript:

1 Recursive Algorithms 1 Building a Ruler: drawRuler()
Main Index Contents Recursive Algorithms Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Partitioning and Merging of Sublists in mergeSort() Function calls in mergeSort() Quicksort Example (8 slides) Finding kth – Largest Element powerSet() Example Recursive calls for fib(5) Effect of fib(5) Using Dynamic Programming The 8-Queens Example (3 slides) Summary Slides (6 slides)

2 Recursion Recursive functions Are functions that calls themselves
Can only solve a base case If not base case, the function breaks the problem into a slightly smaller, slightly simpler, problem that resembles the original problem and Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case Makes a call to itself inside the return statement Eventually the base case gets solved and then that value works its way back up to solve the whole problem

3 Recursion Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1
Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… Base case (1! = 0! = 1)

4 Example Using Recursion: The Fibonacci Series
Each number sum of two previous ones Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2) C++ code for fibonacci function long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n – 2 ); }

5 Example Using Recursion: The Fibonacci Series
Diagram of Fibonnaci function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +

6 2.1 Call function fibonacci
1 // Fig. 3.15: fig03_15.cpp 2 // Recursive fibonacci function 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 unsigned long fibonacci( unsigned long ); 10 11 int main() 12 { unsigned long result, number; 14 cout << "Enter an integer: "; cin >> number; result = fibonacci( number ); cout << "Fibonacci(" << number << ") = " << result << endl; return 0; 20 } 21 22 // Recursive definition of function fibonacci 23 unsigned long fibonacci( unsigned long n ) 24 { 25 if ( n == 0 || n == 1 ) // base case return n; 27 else // recursive case return fibonacci( n - 1 ) + fibonacci( n - 2 ); 29 } 1. Function prototype 1.1 Initialize variables 2. Input an integer 2.1 Call function fibonacci 2.2 Output results. 3. Define fibonacci recursively Only the base cases return values. All other cases call the fibonacci function again.

7 Program Output Enter an integer: 0 Fibonacci(0) = 0
Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = Enter an integer: 35 Fibonacci(35) =

8 Recursion vs. Iteration
Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized Both can have infinite loops Balance between performance (iteration) and good software engineering (recursion)

9 Another example: the binary search
// Binary search of an array #include <iostream.h> #include <iomanip.h> int binarySearch( int [], int, int, int, int ); void printHeader( int ); void printRow( int [], int, int, int, int ); int main() { const int arraySize = 15; int a[ arraySize ], key, result; for ( int i = 0; i < arraySize; i++ ) a[ i ] = 2 * i; // place some data in array cout << "Enter a number between 0 and 28: "; cin >> key;

10 binary search continued
printHeader( arraySize ); result = binarySearch( a, key, 0, arraySize - 1, arraySize ); if ( result != -1 ) cout << '\n' << key << " found in array element " << result << endl; else cout << '\n' << key << " not found" << endl; return 0; }

11 binary search continued
int binarySearch( int b[], int searchKey, int low, int high, int size ) { int middle; while ( low <= high ) { middle = ( low + high ) / 2; printRow( b, low, middle, high, size ); if ( searchKey == b[ middle ] ) // match return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array else low = middle + 1; // search high end of array } return -1; // searchKey not found

12 binary search continued
// Print a header for the output void printHeader( int size ) { cout << "\nSubscripts:\n"; for ( int i = 0; i < size; i++ ) cout << setw( 3 ) << i << ' '; cout << '\n'; for ( i = 1; i <= 4 * size; i++ ) cout << '-'; cout << endl; }

13 binary search continued
// Print one row of output showing the current // part of the array being processed. void printRow( int b[], int low, int mid, int high, int size ) { for ( int i = 0; i < size; i++ ) if ( i < low || i > high ) cout << " "; else if ( i == mid ) // mark middle value cout << setw( 3 ) << b[ i ] << '*'; else cout << setw( 3 ) << b[ i ] << ' '; cout << endl; }

14 binary search continued
Enter a number between 0 and 28: 15 Subscripts: * * 16 18* 20 16* 15 not found

15 Building a Ruler: drawRuler()
15 Main Index Contents 15 Main Index Contents Building a Ruler: drawRuler() Problem: create a program that draws marks at regular intervals on a line. The sizes of the marks differ, depending on the specific interval. - The recursive function drawRuler() assumes the existence of the function drawMark( ), which takes a point x and an integer value h as arguments and draws a mark at point x with size proportional to h.

16 The Merge Algorithm Example
The merge algorithm takes a sequence of elements in a vector v having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid.

17 The Merge Algorithm… (Cont…)
17 Main Index Contents The Merge Algorithm… (Cont…)

18 The Merge Algorithm… (Cont…)
18 Main Index Contents The Merge Algorithm… (Cont…)

19 The Merge Algorithm… (Cont…)

20 Partitioning and Merging of Sublists in mergeSort()
20 Main Index Contents Partitioning and Merging of Sublists in mergeSort()

21 Function calls in mergeSort()

22 Quicksort Example The quicksort algorithm uses a series of recursive calls to partition a list into smaller and smaller sublists about a value called the pivot. Example: Let v be a vector containing 10 integer values: v = {800, 150, 300, 650, 550, 500, 400, 350, 450, 900}

23 Quicksort Example (Cont…)

24 Quicksort Example (Cont…)
24 Main Index Contents Quicksort Example (Cont…)

25 Quicksort Example (Cont…)

26 Quicksort Example (Cont…)

27 Quicksort Example (Cont…)

28 Quicksort Example (Cont…)

29 Quicksort Example (Cont…)
29 Main Index Contents Quicksort Example (Cont…)

30 Finding Kth – Largest Element
To locate the position of the kth-largest value (kLargest) in the list, partition the elements into two disjoint sublists. The lower sublist must contain k elements that are less than or equal to kLargest and the upper sublist must contain elements that are greater than or equal to kLargest. The elements in the lower sublist do not need to be ordered but only to have values that are less than or equal to kLargest. The opposite condition applies to the upper sublist.

31 31 Main Index Contents powerSet() Example

32 Recursive calls for fib(5)

33 Affect of fib(5) Using Dynamic Programming
33 Main Index Contents Affect of fib(5) Using Dynamic Programming

34 The 8-Queens Example

35 The 8-Queens Example (Cont…)
35 Main Index Contents The 8-Queens Example (Cont…)

36 The 8-Queens Example (Cont…)
36 Main Index Contents The 8-Queens Example (Cont…)

37 §- Divide-and-Conquer Algorithms
37 Main Index Contents Summary Slide 1 §- Divide-and-Conquer Algorithms - splits a problem into subproblems and works on each part separately - Two type of divide-and-conquer strategy: 1) mergesort algorithm - Split the range of elements to be sorted in half, sort each half, and then merge the sorted sublists together. - running time: O(n log2n), requires the use of an auxiliary vector to perform the merge steps.

38 - running time: O(n log2n)
38 Main Index Contents Summary Slide 2 2) quicksort algorithm - uses a partitioning strategy that finds the final location of a pivot element within an interval [first,last). - The pivot splits the interval into two parts, [first, pivotIndex), [pivotIndex, last). All elements in the lower interval have values  pivot and all elements in the upper interval have values  pivot. - running time: O(n log2n) - worst case: of O(n2), highly unlikely to occur

39 §- Recursion in Combinatorics
39 Main Index Contents Summary Slide 3 §- Recursion in Combinatorics - A set of n elements has 2n subsets, and the set of those subsets is called the power set. By using a divide and conquer strategy that finds the power set after removing an element from the set and then adds the element back into each subset, we implement a function that computes the power set. The section also uses recursion to list all the n! permutations of the integers from 1 through n. The success of this algorithm depends on the passing of a vector by value to the recursive function.

40 §- Dynamic Programming
40 Main Index Contents Summary Slide 4 §- Dynamic Programming - Two type of dynamic programming: 1) top-down dynamic programming - uses a vector to store Fibonacci numbers as a recursive function computes them - avoids costly redundant recursive calls and leads to an O(n) algorithm that computes the nth Fibonacci number. - recursive function that does not apply dynamic programming has exponential running time. - improve the recursive computation for C(n,k), the combinations of n things taken k at a time.

41 Summary Slide 5 2) bottom-up dynamic programming
41 Main Index Contents Summary Slide 5 2) bottom-up dynamic programming - evaluates a function by computing all the function values in order, starting at the lowest level and using previously computed values at each step to compute the current value. - 0/1 knapsack problem

42 §- Backtracking Algorithm
42 Main Index Contents Summary Slide 6 §- Backtracking Algorithm - finds a consistent partial solution to a problem and then tries to extend the partial solution to a complete solution by executing a recursive step. If the recursive step fails to find a solution, it returns to the previous state and the algorithm tries again from a new consistent partial solution. - takes "1 step forward and n steps backward". - solving advanced problems in graph theory and operations research. - Example: The 8-Queens, developing a series of free functions and the class chessboard.


Download ppt "Recursive Algorithms 1 Building a Ruler: drawRuler()"

Similar presentations


Ads by Google