CSS342: Recursion1 Professor: Munehiro Fukuda. CSS342: Recursion2 Topics Day 1: Lecture –Basic recursions 1.Sum 2.Fact 3.Print a number in any base 4.Binary.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Introduction to Recursion and Recursive Algorithms
Factorial Recursion stack Binary Search Towers of Hanoi
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Recursion. Binary search example postponed to end of lecture.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
COMPSCI 105 S Principles of Computer Science Recursion 3.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
1 Recursion. 2 Basic Recursions Break a problem into smaller identical problems –Each recursive call solves an identical but smaller problem. a base caseStop.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Chapter 8 Recursion Modified.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion Powerful Tool
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion Chapter 12.
Chapter 15 Recursion.
Java Software Structures: John Lewis & Joseph Chase
Chapter 12 Recursion (methods calling themselves)
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

CSS342: Recursion1 Professor: Munehiro Fukuda

CSS342: Recursion2 Topics Day 1: Lecture –Basic recursions 1.Sum 2.Fact 3.Print a number in any base 4.Binary search 5.Fibonacci sequence –Tracing recursions –Recursive applications Towers of Hanoi Divide & Conquer (Calculating the space of a given polynomial) Backtrack (8 queens) –Efficiency of recursion Day 2: Lab Work –Drawing Fractal Figures with Recursion Ruler Koch Curves Sierpinski Arrowhead Gosper or Hilbert Curves

CSS342: Recursion3 Basic Recursions Break a problem into smaller identical problems –Each recursive call solves an identical but smaller problem. a base caseStop the break-down process at a special case whose solution is obvious, (termed a base case) –Each recursive call tests the base case to eventually stop. –Otherwise, we fall into an infinite recursion. Basic Recursions

CSS342: Recursion4 Example 1: The Sum of the First N Positive Integers Definition: sum(n)= n + (n-1) + (n-2) + … + 1 for any integer n > 0 Recursive relation; sum(n)= n + [(n-1) + (n-2) + … + 1] = n + sum(n-1) Looks so nice, but how about n == 1? sum(1)= 1 + sum(0), but the argument to sum( ) must be positive Final recursive definition: sum(n) = 1 if n = 1 (Base case) = n + sum(n-1)if n > 1 (Recursive call) Basic Recursions

CSS342: Recursion5 Recursive Definition of sum(n) int sum(int n) { if (n == 1)// base case return 1; else return n + sum(n-1);// recursive call A } How should I test this function’s correctness? Use the box method: A box has each function’s local environmentex. cout << sum(3); 1. Arguments 2. Local variables 3. Place holder for the value returned by a called function 4. A return value n = 3 A: sum(n-1) = ? return ? Basic Recursions

CSS342: Recursion6 Box trace of sum(3) n = 3 A: sum(n-1)=? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 n = 3 A: sum(n-1)= ? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 Each box corresponds to a function’s activation record or stack. cout << sum(3); Basic Recursions

CSS342: Recursion7 Example 2: The Factorial of n Definition: factorial(n) = n * (n-1) * (n-2) * … * 1 for any integer n > 0 factorial(0) = 1 Recursive relation; factorial(n) = n * [(n-1) * (n-2) * … * 1] = n * factorial(n-1) Looks so nice, but how about n = 1? factorial(1)= 1 * factorial(0) but the argument to factorial( ) must be positive Final recursive definition: factorial(n) = 1 if n = 1 = n *factorial(n-1)if n > 1 Basic Recursions

CSS342: Recursion8 Recursive Definition of facotrial(n) int fact(int n) { if (n == 1)// base case return 1; else return n * fact(n-1);// recursive call A } Trace its execution using the box method: ex. cout << fact(3); n = 3 A: fact(n-1) = ? return ? Basic Recursions

CSS342: Recursion9 Box trace of fact(3) n = 3 A: fact(n-1)=? return ? n = 2 A: fact(n-1)=? return ? n = 1 return 1 n = 3 A: fact(n-1)= ? return ? n = 2 A: fact(n-1)=? return ? n = 1 return 1 cout << fact(3); Basic Recursions

CSS342: Recursion10 Precondition of fact(n) Precondition:n >= 1 If the precondition is violated, what happen? –Fact(0) calls fact(-1), which calls fact(-2), …. For robustness, given a negative number or 0, fact should stop a recursive call. Always correct to recursions int fact(int n) { if (n <= 1)// base case for n = 1 or bad args. return 1; else return n * fact(n-1);// recursive call A } Basic Recursions

CSS342: Recursion11 Example 3: Printing Numbers in Any Base How to convert a decimal to a hexadecimal Dividend Remainder 16) (10) ……..… 16) (10) ………6 16) (10) ………1 16) (10) …13(D) 16) 1883 (10) …12(C) 16) 117 (10) …11(B) 16) 7 (10) …… ) 0 (10) …… Divide by a base(16) 2. Print each reminder from the bottom: 75BCD16(16) Basic Recursions

CSS342: Recursion12 Recursive Definition of printNum(n,base) void printNum(int n, int base) { static string DIGIT_TABLE = “ abcdef”; if (n >= base)// recursive call A printNum( n / base, base ); // else n < basebase case cout << DIGIT_TALBE[ n % base ]; } Trace its execution using the box method: ex. cout << print( , 16); n = , base = 16 A: fact(n-1) returns nothing Print: n % base = ? return nothing Basic Recursions

CSS342: Recursion13 Box trace of printNum( , 16) Trace by yourself (This type of tracing problems will be given in your mid term.) Basic Recursions

CSS342: Recursion14 Example 4: Binary Search The search divides a list into two small sub-lists till a sub- list is no more divisible. Each step needs the top/tail indexes and a target item. First half An entire sorted list First halfSecond half Can we call binarySearch( ) recursively? Basic Recursions

CSS342: Recursion15 Binary Search (Recursive Version) int binarySearch( const int array[], int top, int tail, int target ) { int mid = (top + tail)/2; if ( target == array[mid] ) return mid; else if ( target < array[mid] ) return binarySearch( array, top, mid-1, target ); else if ( target > array[mid] ) return binarySearch( array, mid+1, tail, target ); } Question: if it does not find the target, how can we stop this program? Basic Recursions

CSS342: Recursion16 Binary Search Revisited In the next recursive call, top = i + 1; tail = i; thus top > tail!! Base cases: (1) if (target == array[mid]) return mid; (2) if top > tail return –1; 3547 toptail ii+1 mid = (top + tail)/2 = (i + i+1)/2 = i top = mid + 1 = i + 1 Basic Recursions

CSS342: Recursion17 Binary Search (Correct Recursive Version) int binarySearch( const int array[], int top, int tail, int target ) { int mid = (top + tail)/2; if ( target == array[mid] ) return mid;// find it else if ( top > tail ) return -1;// cannot find it else if ( target < array[mid] ) return binarySearch( array, top, mid-1, target ); else if ( target > array[mid] ) return binarySearch( array, mid+1, tail, target ); } Basic Recursions

CSS342: Recursion18 Babies Example 5: Multiplying Mice Quite difficult to keep track of the population explosion. Month 1 Month 2 Month 4 Month 5 Month 3 Month 6 Month 7 Recursive relation: #babies in Month 7= #mice in Month 5 #adults in Month 7 = #mice in Month 6 Basic Recursions

CSS342: Recursion19 The Fibonacci Sequence mice(n) = mice(n-1) + mice(n-2) Then, what are base cases? Recursive definition: mice(1) = mice(0) + mice(-1) can’t happen. Thus, mice(1) = 1; mice(2) = mice(1) + mice(0) can’t happen, either. Thus, mice(2) = 1; mice(n) = 1if n = 1 or 2 mice(n-1) + mice(n-2)if n > 2 Basic Recursions

CSS342: Recursion20 Tracing mice(n) m(6) Return m(5)+m(4) m(5) Return m(4)+m(3) m(4) Return m(3)+m(2) m(3) Return m(2)+m(1) m(2) Return 1 m(1) Return 1 m(3) Return m(2)+m(1) m(1) Return 1 m(2) Return 1 m(1) Return 1 m(3) Return m(2)+m(1) m(2) Return 1 m(4) Return m(3)+m(2) 11 m(2) Return 1 m(2) Return Basic Recursions

CSS342: Recursion21 Recursive Applications Discrete Mathematics (Combinatorics, Puzzles, Coding Theory) –Tower of Hanoi and Gray Code Divide and Conquer –Mergesort, Convex Hall, and Fast Fourier Transform Backtrack –8 Queens, Maze and Classic Chess Program Fractal Figures –Koch, Sierpinski Allowhead, Gosper, Hilbert, and Dragon curves Recursive Applications

CSS342: Recursion22 Towers of Hanoi A BC Find how to move dishes from A to B using C. Restrictions: Recursive Applications

CSS342: Recursion23 Solution A BC A BC A BC A BC if we could move n-1 dishes from A to C via B, we could move a dish, (i.e., the last one) from A to B (via C), and thereafter, we would move n-1 Dishes from C to B via A! move(n, A, B, C) move(n-1, A, C, B) move(1, A, B, C) move(n-1, C, B, A) In order to move n dishes from A to B via C, Recursive Applications

CSS342: Recursion24 Example Code #include using namespace std; void move( int n, char orig, char dest, char temp ) { if ( n == 1 ) cout << "move disk 1 from " << orig << " to " << dest << endl; else { move( n - 1, orig, temp, dest ); cout << "move disk " << n << " from " << orig << " to " << dest << endl; move( n - 1, temp, dest, orig ); } int main( int argc, char* argv[] ) { int nDishes = atoi( argv[1] ); move( nDishes, 'A', 'B', 'C' ); cout << "completed" << endl; return 0; }

CSS342: Recursion25 Tracing move(n, A, B, C) move(3, A, B, C) move(2, A, C, B)move(1, A, B, C)move(2, C, B, A) move(1, A, B, C) move(1, A, C, B) move(1, B, C, A) move(1, C, A, B) move(1, C, B, A) move(1, A, B, C) Locally, A is A, C is B, B is C.Locally C is A, B is B, A is C Recursive Applications

CSS342: Recursion26 Divide-and-Conquer Algorithms Computational Geometry – Finding a Convex Hall Divide: –Divide a problem into two sub-problems recursively so that the sub-problems are calculated by themselves. Conquer: –Form a new solution from the solutions answered by the sub-problems Recursive Applications The convex hull problem Trivial algorithm: O(n2) Divide & conquer: O(nlogn)

CSS342: Recursion27 Backtrack Trace every combination until encountering a solution The eight queens problem –Place 8 queens on a 8 * 8 chessboard so that no queen can attack any other queen. –Place a queen from the column 0 to 7 as checking if the queens placed on the former columns can attack the current queen. Recursive Applications Q Q Q Q Q Q Q Q

CSS342: Recursion28 Backtrack The Eight Queen Problem A Naïve Solution Recursive Applications Q Q Q Q ?? Q Q for ( int col = 0; col < 7; col++ ) { for ( int row = 0; row < 7; row++ ) { if ( safeLocation( table, row, col ) ) { table[row][col] = true; break; } // oops! No safe row to place a new queen // I got to go back to the previous column! } Then, how can I go back to the previous column? Just col--?

CSS342: Recursion29 Backtrack The Eight Queen Problem A Recursive Solution: Recursive Applications Q Q Q Q ?? Q Q [0,0] -> [0,1] NO [1,1] NO [2,1] -> [0,2] NO [1,2] NO [2,2] NO [3,2] NO [4,2] -> [0,3] NO [1,3] -> [0,4] NO [1,4] NO [2,4] NO [3,4] -> [0,5] NO [1,5] NO [2,5] NO [3,5] NO [4,5] NO [5,5] NO [6,5] NO [7,5] NO [4,4] NO [5,4] NO [6,4] NO [7,4] -> [0,5]

CSS342: Recursion30 Backtrack The Eight Queen Problem Recursive Applications bool addQueen( bool t[SIZE][SIZE], int col ) { if ( col >= SIZE ) return true; // all cols have been examined for ( int row = 0; row < SIZE; row++ ) { if ( safeLocation( t, row, col ) ) { // this row may be a candidate t[row][col] = true; // place a new queen; if ( addQueen( t, col + 1 ) ) return true; // all the following cols were filled else t[row][col] = false; // A wrong position. Try the next row } return false; // all rows examined, but no candidates } int main( ) { bool table[SIZE][SIZE]; // there are no matrix templates in STL init( table ); // all table[i][j] = false; if ( addQueen( table, 0 ) ) print( table ); else cout << "no solution" << endl; }

CSS342: Recursion31 Fractal Curves Drawing a Ruler Recursive Applications // java code to draw a ruler void drawRuler( Graphics g, int left, int right, int level ) { if ( level < 1 ) return; int mid = ( left + right ) / 2; g.drawLine( mid, 80, mid, 80 – level * 5 ); drawRuler( g, left, mid – 1, level – 1 ); drawRuler( g, mid + 1, right, level – 1 ); } mid at level 3 mid at level 2 mid at level 1 Initial left Initial right

CSS342: Recursion32 Fractal Curves Drawing a Koch Curve using a Turtle Recursive Applications class Turtle { public: Turtle( float initX=0.0, float initY=0.0, float initAngle=0.0 ) { ~Turtle( ); void draw( float d );// draw a line from the current position for distance d void move( float d );// simply move from the current position for distance d void turn( float a );// turn left by angle a private: float angle;// presented in degree but not in radian oftream out; }; int main( ) { Turtle t; t.draw( 10 ); t.turn( 45 ); t.move( 10 ); t.draw( 10 ); }

CSS342: Recursion33 Fractal Curves Drawing a Koch Curve using a Turtle Recursive Applications class Koch : Turtle { public: Koch( float initX=0.0, float initY=0.0, float initAngle=0.0) : Turtle( initX, initY, initAngle ) { ) ~Koch( ); curve( int level, float length ) { if ( level > 1 ) { curve( level – 1, length ); turn( 60 ); curve( level – 1, length ); turn( -120 ); curve( level – 1, length ); turn( 60 ); curve( level – 1, length ); } draw( length ); } }; int main( ) { Koch k(100, 100, 0); k.curve( 3, 10 ); } Draw the level-3 Koch curve here

CSS342: Recursion34 Recursion and Efficiency Programmability –Capable of breaking down a complex problem –Based on mathematical induction Efficiency –The overhead associated with function calls (Bookkeeping the current stack and creating a new one) –The inherent inefficiency of some algorithm (Repeating the same computation) Efficiency

CSS342: Recursion35 Efficiency of mice(n) m(6) Return m(5)+m(4) m(5) Return m(4)+m(3) m(4) Return m(3)+m(2) m(3) Return m(2)+m(1) m(2) Return 1 m(1) Return 1 m(3) Return m(2)+m(1) m(1) Return 1 m(2) Return 1 m(1) Return 1 m(3) Return m(2)+m(1) m(2) Return 1 m(4) Return m(3)+m(2) m(2) Return 1 m(2) Return 1 O(2 n ) Efficiency

CSS342: Recursion36 Iterative Solution of mice(n) int iterativeMice(int n) { int previous = 1; int current = 1; int next = 1; for (int i = 3; i <= n; ++i) { next = current + previous; previous = current; current = next; } return next; } Efficiency of mice(n): O(n-2)=O(n) Efficiency

CSS342: Recursion37 Recursion versus Iteration Discover a recursive solution if it is easier. Convert it to iteration if an iterative solution is more efficient. –When a recursion incurs more iterations –When each iteration includes a few computation ProblemsRecursionIteration Fibonacci sequenceO(2 n )O(n) Sum/Fact/printNumO(n) O(n) function call overhead Binary searchO(log n)O(log n) Tower of HanoiO(2 n ) Fractal figures (Koch)O(4 n ) Can you program it? N queensO(N n ) Efficiency