Recursive Algorithms 1 Building a Ruler: drawRuler()

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
Advertisements

Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 19 Recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 Storage Classes, Scope, and Recursion Lecture 6.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Program Development and Design Using C++, Third Edition
Searching Arrays Linear search Binary search small arrays
Recursion Powerful Tool
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 16: Searching, Sorting, and the vector Type
Chapter 19: Recursion.
Topic 6 Recursion.
Chapter 15 Recursion.
5.13 Recursion Recursive functions Functions that call themselves
RECURSION.
Chapter 15 Recursion.
Andy Wang Object Oriented Programming in C++ COP 3330
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Functions.
Announcements Final Exam on August 17th Wednesday at 16:00.
Formatted and Unformatted Input/Output Functions
Algorithm Design Methods
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 14: Recursion Starting Out with C++ Early Objects
Using Arrays in Abstract Data Types
Algorithm design and Analysis
Chapter 14: Recursion Starting Out with C++ Early Objects
CISC181 Introduction to Computer Science Dr
Functions Recursion CSCI 230
Basics of Recursion Programming with Recursion
Chapter 17 Recursion.
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

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)

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

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)

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 ); }  

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

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 { 13 unsigned long result, number; 14 15 cout << "Enter an integer: "; 16 cin >> number; 17 result = fibonacci( number ); 18 cout << "Fibonacci(" << number << ") = " << result << endl; 19 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 26 return n; 27 else // recursive case 28 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.

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) = 832040   Enter an integer: 35 Fibonacci(35) = 9227465

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)

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;

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; }

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

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; }

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; }

binary search continued Enter a number between 0 and 28: 15 Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ------------------------------------------------------------ 0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28 16 18 20 22* 24 26 28 16 18* 20 16* 15 not found

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.

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.

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

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

The Merge Algorithm… (Cont…)

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

Function calls in mergeSort()

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}

Quicksort Example (Cont…)

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

Quicksort Example (Cont…)

Quicksort Example (Cont…)

Quicksort Example (Cont…)

Quicksort Example (Cont…)

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

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 Main Index Contents powerSet() Example

Recursive calls for fib(5)

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

The 8-Queens Example

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

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

§- 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.

- 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

§- 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.

§- 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.

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

§- 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.