Jordi Cortadella Department of Computer Science

Slides:



Advertisements
Similar presentations
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Advertisements

The Towers of Hanoi or Apocalypse When?.
More Recursion: Permutations and Towers of Hanoi COP 3502.
COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
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 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
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++
Reasoning with invariants Jordi Cortadella Department of Computer Science.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
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.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Upon completion, the world will end…
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
UNIT 17 Recursion: Towers of Hanoi.
Functions Jordi Cortadella Department of Computer Science.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion CSE 2320 – Algorithms and Data Structures
COMP 51 Week Fourteen Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
Reasoning with invariants
Recursion Chapter 12.
Chapter 15 Recursion.
For loops, nested loops and scopes
Recursion CSE 2320 – Algorithms and Data Structures
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
ECE 103 Engineering Programming
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion.
CSC 143 Recursion.
CS148 Introduction to Programming II
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Presentation transcript:

Jordi Cortadella Department of Computer Science Recursion Jordi Cortadella Department of Computer Science

Recursion Introduction to Programming © Dept. CS, UPC

Recursion Principle: Reduce a complex problem into a simpler instance of the same problem Recursion is a concept tightly related to mathematical induction (typically used for proofs on natural numbers): Proof a base case for the first natural number Inductive step: proof that validity for n also implies validity for n+1 (domino effect) Introduction to Programming © Dept. CS, UPC

Mathematical induction: example The sum of the first n odd numbers is n2: Informal proof: … 2n-1 n 9 7 5 3 1 Introduction to Programming © Dept. CS, UPC

Mathematical induction: example Base case: it works for n = 1  Inductive step: let us assume it works for n Introduction to Programming © Dept. CS, UPC

Factorial Definition of factorial: 𝑛!=𝑛∙ 𝑛−1 ∙ 𝑛−2 ⋯2∙1 // Pre: n  0 // Returns n! int factorial(int n) { // iterative solution int f = 1; for (int i = n; i > 0; --i) f = fi; return f; } Introduction to Programming © Dept. CS, UPC

Factorial Recursive definition: 𝑛!= 𝑛∙ 𝑛−1 !, 𝑛>0 &1, 𝑛=0 // Pre: n  0 // Returns n! int factorial(int n) { // recursive solution if (n == 0) return 1; else return nfactorial(n - 1); } Introduction to Programming © Dept. CS, UPC

Factorial: recursive solution factorial(n) factorial(n-1) 𝑛 𝑛-1 factorial(n-2) 𝑛-2 factorial(2) 2 factorial(1) 𝑛 𝑛! 1 Introduction to Programming © Dept. CS, UPC

Recursion Generally, recursive solutions are simpler than (or as simple as) iterative solutions. There are some problems in which one solution is much simpler than the other. Generally, recursive solutions are slightly less efficient than the iterative ones (if the compiler does not try to optimize the recursive calls). There are natural recursive solutions that can be extremely inefficient. Be careful ! Introduction to Programming © Dept. CS, UPC

Recursive design: 3 steps Identify the basic cases in which a simple non-recursive solution can be provided. Example: 0! = 1! = 1. Recursive cases: solve the complex cases in terms of simpler instances of the same problem (domino effect). Example: factorial(n) = nfactorial(n-1). Termination: guarantee that the parameters of the call move closer to the basic cases at each recursive call (the domino chain is not infinite). Example: In the case of a factorial, n-1 is closer to 0 than n. Therefore, we can guarantee that this function terminates. Introduction to Programming © Dept. CS, UPC

Recursive design: termination It is not clear whether the following function terminates: // Pre: n  1 // Returns the number of steps of the Collatz sequence // that starts with n. int Collatz(int n) { // recursive solution if (n == 1) return 0; else if (n%2 == 0) return 1 + Collatz(n/2); else return 1 + Collatz(3n + 1); } The reason is that 3n+1 is not closer to 1 than n Introduction to Programming © Dept. CS, UPC

Recursion: behind the scenes ... f = factorial(4); int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 4 4 4 3 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 3 3 3 2 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 2 2 2 1 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 1 1 1 Introduction to Programming © Dept. CS, UPC

Recursion: behind the scenes ... f = factorial(4); 24 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 4 3 24 24 6 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 3 2 6 6 2 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 2 1 2 2 1 int factorial(int n) if (n <= 1) return 1; else return n  factorial(n-1); 1 1 Introduction to Programming © Dept. CS, UPC

Recursion: behind the scenes Each time a function is called, a new instance of the function is created. Each time a function “returns”, its instance is destroyed. The creation of a new instance only requires the allocation of memory space for data (parameters and local variables). The instances of a function are destroyed in reverse order of their creation, i.e. the first instance to be created will be the last to be destroyed. Introduction to Programming © Dept. CS, UPC

Print a number n in base b Design a procedure that prints a number n in base b to cout. Examples: 1024 is 10000000000 in base 2 1101221 in base 3 2662 in base 7 1024 in base 10 Introduction to Programming © Dept. CS, UPC

Print a number n in base b // Pre: n ≥ 0, 2  b  10 // Prints the representation of n in base b void print_base(int n, int b); Basic case: n < b  only one digit. Print it. General case: n  b Print the leading digits (n/b) Print the last digit (n%b) Introduction to Programming © Dept. CS, UPC

Write a number n in base b // Pre: n ≥ 0, 2  b  10 // Prints the representation of n in base b void print_base(int n, int b) { if (n < b) cout << n; else { print_base(n/b, b); cout << n%b; } } Introduction to Programming © Dept. CS, UPC

A simpler version // Pre: n ≥ 0, 2  b  10 // Prints the representation of n in base b void print_base(int n, int b) { if (n >= b) print_base(n/b, b); cout << n%b; } Introduction to Programming © Dept. CS, UPC

Tower of Hanoi The puzzle was invented by the French mathematician Édouard Lucas in 1883. There is a legend about an Indian temple that contains a large room with three time-worn posts in it, surrounded by 64 golden disks. To fulfil an ancient prophecy, Brahmin priests have been moving these disks, in accordance with the rules of the puzzle, since that time. The puzzle is therefore also known as the Tower of Brahma puzzle. According to the legend, when the last move in the puzzle is completed, the world will end. It is not clear whether Lucas invented this legend or was inspired by it. (from http://en.wikipedia.org/wiki/Tower_of_Hanoi) Rules of the puzzle: A complete tower of disks must be moved from one post to another. Only one disk can be moved at a time. No disk can be placed on top of a smaller disk. Not allowed ! Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Introduction to Programming © Dept. CS, UPC

Tower of Hanoi What rules determine the next move? How many moves do we need? There is no trivial iterative solution. Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Inductive reasoning: assume that we know how to solve Hanoi for n-1 disks Hanoi(n-1) from left to middle (safe: the largest disk is always at the bottom) Move the largest disk from the left to the right Hanoi(n-1) from the middle to the right (safe: the largest disk is always at the bottom) Introduction to Programming © Dept. CS, UPC

Tower of Hanoi // Pre: n is the number of disks (n≥0). // from, to and aux are the names of the pegs. // Post: solves the Tower of Hanoi by moving n disks // from peg from to peg to using peg aux void Hanoi(int n, char from, char to, char aux) { if (n > 0) { Hanoi(n - 1, from, aux, to); cout << “Move disk from “ << from << “ to “ << to << endl; Hanoi(n - 1, aux, to, from); } Introduction to Programming © Dept. CS, UPC

Tower of Hanoi // Main program to solve the Tower of Hanoi // for any number of disks int main() { int Ndisks; // Read the number of disks cin >> Ndisks; // Solve the puzzle Hanoi(Ndisks, ‘L’, ‘R’, ‘M’); } Introduction to Programming © Dept. CS, UPC

Tower of Hanoi > Hanoi 5 Move disk from L to R Move disk from L to M Move disk from R to M Move disk from L to R Move disk from M to L Move disk from M to R Move disk from L to R Move disk from L to M Move disk from R to M Move disk from R to L Move disk from M to L Move disk from R to M Move disk from L to R Move disk from L to M Move disk from R to M Move disk from L to R Move disk from M to L Move disk from M to R Move disk from L to R Move disk from M to L Move disk from R to M Move disk from R to L Move disk from M to L Move disk from M to R Move disk from L to R Move disk from L to M Move disk from R to M Move disk from L to R Move disk from M to L Move disk from M to R Move disk from L to R Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Hanoi(2,L,M,R) Hanoi(2,M,R,L) Hanoi(3,L,R,M) Hanoi(1,R,M,L) Hanoi(0,R,L,M) RM Hanoi(2,M,R,L) Hanoi(1,M,L,R) ML MR Hanoi(2,L,M,R) Hanoi(2,M,R,L) Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Hanoi(3,L,R,M) Hanoi(2,L,M,R) Hanoi(1,L,R,M) Hanoi(0,M,R,L) LM Hanoi(1,R,M,L) Hanoi(0,R,L,M) RM Hanoi(2,M,R,L) Hanoi(1,M,L,R) ML MR Introduction to Programming © Dept. CS, UPC

Tower of Hanoi How many moves do we need for n disks? Moves(n) = 1 + 2Moves(n-1) n Moves(n) 1 2 3 7 4 15 5 31 6 63 2n-1 Introduction to Programming © Dept. CS, UPC

Tower of Hanoi Let us assume that we can move one disk every second. How long would it take to move n disks? n time 1 1s 5 31s 10 17m 3s 15 9h 6m 7s 20 12d 3h 16m 15s 25 1y 23d 8h 40m 31s 30 > 34y 40 > 34,000y 50 > 35,000,000y 60 > 36,000,000,000y Introduction to Programming © Dept. CS, UPC