Recursion COL 106.

Slides:



Advertisements
Similar presentations
Starting Out with Java: From Control Structures through Objects
Advertisements

Recursion.
Factorial Recursion stack Binary Search Towers of Hanoi
Programming Recursion.
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.
Recursion COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion Gordon College CPS212
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Data Structures Using C++ 2E Chapter 6 Recursion.
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
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
What is recursion? Imagine checking the dictionary for “apple”  a round fruit with a firm white flesh and a green, red or yellow skin  the usually sweet-tasting.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Programming Fundamentals1 Chapter 6 FUNCTIONS AND POINTERS.
1 CSC 211 Data Structures Lecture 17 Dr. Iftikhar Azim Niaz 1.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
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.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Math & Recursion COMP171 Fall Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition * Theorem.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 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.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
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! =
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.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Recursion: The Mirrors
Chapter 15 Recursion.
Recursion and Logarithms
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion.
Chapter 17 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Recursion COL 106

Ex. 1: The Handshake Problem There are n people in a room. If each person shakes hands once with every other person. What is the total number h(n) of handshakes? h(4) = h(3) + 3 h(3) = h(2) + 2 h(2) = 1 h(n) = h(n-1) + n-1 h(n): Sum of integer from 1 to n-1 = n(n-1) / 2

Recursion In some problems, it may be natural to define the problem in terms of the problem itself. Recursion is useful for problems that can be represented by a simpler version of the same problem. Example: the factorial function 6! = 6 * 5 * 4 * 3 * 2 * 1 We could write: 6! = 6 * 5!

Example 2: factorial function In general, we can express the factorial function as follows: n! = n * (n-1)! Is this correct? Well… almost. The factorial function is only defined for positive integers. So we should be a bit more precise: n! = 1 (if n is equal to 1) n! = n * (n-1)! (if n is larger than 1)

factorial function The C++ equivalent of this definition: int fac(int numb){ if(numb<=1) return 1; else return numb * fac(numb-1); } recursion means that a function calls itself

factorial function Assume the number typed is 3, that is, numb=3. 3 <= 1 ? No. fac(3) = 3 * fac(2) fac(2) : 2 <= 1 ? No. fac(2) = 2 * fac(1) fac(1) : 1 <= 1 ? Yes. return 1 int fac(int numb){ if(numb<=1) return 1; else return numb * fac(numb-1); } fac(2) = 2 * 1 = 2 return fac(2) fac(3) = 3 * 2 = 6 return fac(3) fac(3) has the value 6

factorial function For certain problems (such as the factorial function), a recursive solution often leads to short and elegant code. Compare the recursive solution with the iterative solution: Iterative solution int fac(int numb){ int product=1; while(numb>1){ product *= numb; numb--; } return product; Recursive solution int fac(int numb){ if(numb<=1) return 1; else return numb*fac(numb-1); }

Recursion We have to pay a price for recursion: calling a function consumes more time and memory than adjusting a loop counter. high performance applications (graphic action games, simulations of nuclear explosions) hardly ever use recursion. In less demanding applications recursion is an attractive alternative for iteration (for the right problems!)

Recursion If we use iteration, we must be careful not to create an infinite loop by accident: for(int incr=1; incr!=10;incr+=2) ... int result = 1; while(result >0){ result++; } Oops! Oops!

No termination condition Recursion Similarly, if we use recursion we must be careful not to create an infinite chain of function calls: int fac(int numb){ return numb * fac(numb-1); } Or: if (numb<=1) return 1; else return numb * fac(numb+1); Oops! No termination condition Oops!

Recursion We must always make sure that the recursion bottoms out: A recursive function must contain at least one non-recursive branch. The recursive calls must eventually lead to a non-recursive branch.

Recursion Recursion is one way to decompose a task into smaller subtasks. At least one of the subtasks is a smaller example of the same task. The smallest example of the same task has a non-recursive solution. Example: The factorial function n! = n * (n-1)! and 1! = 1

How many pairs of rabbits can be produced from a single pair in a year's time? Assumptions: Each pair of rabbits produces a new pair of offspring every month; each new pair becomes fertile at the age of one month; none of the rabbits dies in that year. Example: After 1 month there will be 2 pairs of rabbits; after 2 months, there will be 3 pairs; after 3 months, there will be 5 pairs (since the following month the original pair and the pair born during the first month will both produce a new pair and there will be 5 in all).

Population Growth in Nature Leonardo Pisano (Leonardo Fibonacci = Leonardo, son of Bonaccio) proposed the sequence in 1202 in The Book of the Abacus. Fibonacci numbers are believed to model nature to a certain extent, such as Kepler's observation of leaves and flowers in 1611.

The golden ratio Dividing two adjacent Fibonacci numbers produces increasingly precise approximation of the “golden ratio”. Read http://en.wikipedia.org/wiki/Golden_ratio

Direct Computation Method Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... where each number is the sum of the preceding two. Recursive definition: F(0) = 0; F(1) = 1; F(number) = F(number-1)+ F(number-2);

Example 3: Fibonacci numbers //Calculate Fibonacci numbers using recursive function. //A very inefficient way, but illustrates recursion well int fib(int number) { if (number == 0) return 0; if (number == 1) return 1; return (fib(number-1) + fib(number-2)); } int main(){ // driver function int inp_number; cout << "Please enter an integer: "; cin >> inp_number; cout << "The Fibonacci number for "<< inp_number << " is "<< fib(inp_number)<<endl; return 0;

Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

Trace a Fibonacci Number int fib(int num) { if (num == 0) return 0; if (num == 1) return 1; return (fib(num-1)+fib(num-2)); } Assume the input number is 4, that is, num=4: fib(4): 4 == 0 ? No; 4 == 1? No. fib(4) = fib(3) + fib(2) fib(3): 3 == 0 ? No; 3 == 1? No. fib(3) = fib(2) + fib(1) fib(2): 2 == 0? No; 2==1? No. fib(2) = fib(1)+fib(0) fib(1): 1== 0 ? No; 1 == 1? Yes. fib(1) = 1; return fib(1);

Trace a Fibonacci Number 0 == 0 ? Yes. fib(0) = 0; return fib(0); fib(2) = 1 + 0 = 1; return fib(2); fib(3) = 1 + fib(1) fib(1): 1 == 0 ? No; 1 == 1? Yes fib(1) = 1; return fib(1); fib(3) = 1 + 1 = 2; return fib(3)

Trace a Fibonacci Number fib(2): 2 == 0 ? No; 2 == 1? No. fib(2) = fib(1) + fib(0) fib(1): 1== 0 ? No; 1 == 1? Yes. fib(1) = 1; return fib(1); fib(0): 0 == 0 ? Yes. fib(0) = 0; return fib(0); fib(2) = 1 + 0 = 1; return fib(2); fib(4) = fib(3) + fib(2) = 2 + 1 = 3; return fib(4);

Example 4: Fibonacci number w/o recursion //Calculate Fibonacci numbers iteratively //much more efficient than recursive solution int fib(int n) { int f[100]; f[0] = 0; f[1] = 1; for (int i=2; i<= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }

Fibonacci Numbers Fibonacci numbers can also be represented by the following formula.

Example 5: Binary Search Search for an element in an array Sequential search Binary search Compare the search element with the middle element of the array If not equal, then apply binary search to half of the array (if not empty) where the search element would be.

Binary Search with Recursion // Searches an ordered array of integers using recursion int bsearchr(const int data[], // input: array int first, // input: lower bound int last, // input: upper bound int value // input: value to find )// output: index if found, otherwise return –1 { //cout << "bsearch(data, "<<first<< ", last "<< ", "<<value << "); "<<endl; int middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) return bsearchr(data, first, middle-1, value); else return bsearchr(data, middle+1, last, value); }

Binary Search int main() { const int array_size = 8; int list[array_size]={1, 2, 3, 5, 7, 10, 14, 17}; int search_value; cout << "Enter search value: "; cin >> search_value; cout << bsearchr(list,0,array_size-1,search_value) << endl; return 0; }

Binary Search w/o recursion // Searches an ordered array of integers int bsearch(const int data[], // input: array int size, // input: array size int value // input: value to find ){ // output: if found,return // index; otherwise, return -1 int first, last, upper; first = 0; last = size - 1; while (true) { middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) last = middle - 1; else first = middle + 1; }

Recursion General Form How to write recursively? int recur_fn(parameters){ if(stopping condition) return stopping value; // other stopping conditions if needed return function of recur_fn(revised parameters) }

Example 6: exponential func How to write exp(int numb, int power) recursively? int exp(int numb, int power){ if(power ==0) return 1; return numb * exp(numb, power -1); }

Example 6: number of zero Write a recursive function that counts the number of zero digits in an integer zeros(10200) returns 3. int zeros(int numb){ if(numb==0) // 1 digit (zero/non-zero): return 1; // bottom out. else if(numb < 10 && numb > -10) return 0; else // > 1 digits: recursion return zeros(numb/10) + zeros(numb%10); } zeros(10200) zeros(1020) + zeros(0) zeros(102) + zeros(0) + zeros(0) zeros(10) + zeros(2) + zeros(0) + zeros(0) zeros(1) + zeros(0) + zeros(2) + zeros(0) + zeros(0)

Example 7: n choose k The Binomial coefficients arise in several applications: Combinatorics/Probability (n choose k): C (n,k) = the number of different groups of size k from an initial group of size n Algebra: C (n,k) = coefficient of k th term in the expansion of the n th binomial power (x + y )n Commonly used notation: A closed formula for C(n,k) is given by: C(n,k) = n!/( k! (n-k)! ) L16

n choose k and Pascal’s Triangle Typically the fastest way to compute all C (n,k) up to a certain n is via Pascal’s triangle. In Pascal’s triangle, a 1 is put up top (initialization) and every consequent element is recursively defined to be the sum of the numbers to the right and left of it in the previous row. If a number is missing, it is considered to be 0. L16

n choose k and Pascal’s Triangle 1 L16

n choose k and Pascal’s Triangle 1 1 1 L16

n choose k and Pascal’s Triangle 1 1 1 1 2 1 L16

n choose k and Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 L16

n choose k and Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 L16

n choose k and Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 L16

n choose k and Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 L16

n choose k and Pascal’s Triangle n = row 1 2 3 4 5 6 1 2 3 4 5 6 k = diagonal col 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 Q: Find C (6,4) L16

n choose k and Pascal’s Triangle n = row 1 2 3 4 5 6 1 2 3 4 5 6 k = diagonal col 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 A:C (6,4)=15. Q: Rec. form. for C (n,k)? L16

n choose k and Pascal’s Triangle A: Use Pascal’s triangle. INITIALIZATION: Top of triangle is 1. Therefore, C (0,0) = 1. If a number is missing, it is considered to be 0. This gives rise to C (n,k) = 0 if k < 0, or k > n. RECURSION: Next element is sum of the numbers to the right and left of it in the previous row: C (n,k) = C (n -1,k) + C (n -1,k -1) L16

n choose k and Pascal’s Triangle A standard way of expressing recursive formulas applied to the above: Q: Find a recursive definition for the gcd function. Hint : Euclid’s algorithm. L16

Problem Solving Using Recursion Let us consider a simple problem of printing a message for n times. You can break the problem into two subproblems: one is to print the message one time and the other is to print the message for n-1 times. The second problem is the same as the original problem with a smaller size. The base case for the problem is n==0. You can solve this problem using recursion as follows: void nPrintln(char * message, int times) { if (times >= 1) { cout << message << endl; nPrintln(message, times - 1); } // The base case is n == 0 }

Think Recursively Many of the problems can be solved easily using recursion if you think recursively. For example, the palindrome problem can be solved recursively as follows: bool isPalindrome(const char * const s) { if (strlen(s) <= 1) // Base case return true; else if (s[0] != s[strlen(s) - 1]) // Base case return false; else return isPalindrome(substring(s, 1, strlen(s) - 2)); }

Example 8: Towers of Hanoi Only one disc could be moved at a time A larger disc must never be stacked above a smaller one One and only one extra needle could be used for intermediate storage of discs see applet

Towers of Hanoi void hanoi(int from, int to, int num) { int temp = 6 - from - to; //find the temporary //storage column if (num == 1){ cout << "move disc 1 from " << from << " to " << to << endl; } else { hanoi(from, temp, num - 1); cout << "move disc " << num << " from " << from hanoi(temp, to, num - 1);

Towers of Hanoi int main() { int num_disc; //number of discs cout << "Please enter a positive number (0 to quit)"; cin >> num_disc; while (num_disc > 0){ hanoi(1, 3, num_disc); cout << "Please enter a positive number "; } return 0;

Eight Queens Place eight queens on the chessboard such that no queen attacks any other one.

bool empty(int t[], int row, int col) { for( int j = 0; j < row; j++) { if (t[j] == col) //same column return false; if (abs(t[j] - col) == (row - j)) //on cross line } return true; bool queens(int t[], int row, int col) { if (row == SIZE) // found one answer for (col = 0; col <SIZE; col++) { t[row] = col; if (empty(t, row, col) && queens(t, row +1, 0)) return true;

void print(int t[]){ // print solution for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if (j == t[i]) cout << " Q "; else cout << " _ "; } cout << endl; int main() { int t[SIZE]={0}; for (int i= 0; i <SIZE; i++){ t[0] = i; //on first row, Queen on different column cout << endl << endl <<"i is: " << i << endl; if (queens(t, 1,0)) print(t);

In Summary Recursion is a powerful way to think about problems and understand what’s really going on Often not efficient in implementation. Iterative implementation is better Want a challenge? Think of how to solve a sudoku puzzle using recursion.