Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

Recursion and Induction
CSC 205 Programming II Lecture 10 Towers of Hanoi.
More Recursion: Permutations and Towers of Hanoi COP 3502.
COSC 2006 Data Structures I Recursion III
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
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)
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Recursion Gordon College CPS212
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?
Review of Recursion. Definition: Recursion - The process of a function calling itself.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
CSC 212 Recursion By Dr. Waleed Alsalih. Definition A recursive function (method) is one that calls itself. A recursive method must have a basis part.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
How to win in ACM/ICPC? Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
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.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Recursion Jordi Cortadella Department of Computer Science.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Recursion (part 1) HKUCS Provinci Training Chan Ho Leung.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
ACM programming contest Introduction + Recursion.
Data Structures & Algorithms Recursion and Trees Richard Newman.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
1 Examples of Recursion Instructor: Mainak Chaudhuri
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
UNIT 17 Recursion: Towers of Hanoi.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Algorithm Analysis 1.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Chapter 4 Stacks
Recursion Chapter 12.
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
Jordi Cortadella Department of Computer Science
Data Structures & Algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion.
CSE 1342 Programming Concepts
When Will The World End “Temple of Brahma” legend from the Indian city of Benares Priests must move 64 gold disks from one end of the temple to the other.
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
CS1201: Programming Language 2
Chapter 17 Recursion.
Chapter 19: Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

Recursion

Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different sizes. The monks of Hanoi wants to move these disks from r 1 to r 3 such that: at each step, take the upper disk of one rod and place it to the top of another rod no bigger disk can be placed on top of a smaller disk

Input and output format Input: A single integer n in [1, 20] Output: The steps to move the disks from r 1 to r 3 ; in the format of r i -> r j 3 r1 -> r3 r1 -> r2 r3 -> r1 r1 -> r3 r2 -> r1 r2 -> r3 r1 -> r3

Recursion Can we solve the problem by solving some smaller problems? To move n disks from r 1 to r 3 1.move n-1 disks from r 1 to r 2 2.move 1 disks from r 1 to r 3 3.move n-1 disks from r 2 to r 3 r1r1 r2r2 r3r3

Implementation Let hanoi( int n, int i, int j, int z ) be a function that will move n disks from rod i to rod j using rod k as the middle rod. void hanoi( int n, int i, int j, int k ){ if( n==1 ){ cout r" << j << endl; return; } hanoi( n-1, i, k, j ); hanoi( 1, i, j, k ); hanoi( n-1, k, j, i ); } int main(){ int n; cin >> n; hanoi( n ); return 0; }

How many moves are there? Let T(n) be the number of steps to move n disks T(1) = 1 T(n)= 2 T(n-1) + 1 = 4 T(n-2) =... = 2 n-1 T(1)+ 2 n = 2 n - 1 The running time is also Θ(2 n ) = exponential time Very slow But it is optimal already (prove by M.I. on n)

Other recursion problems Look for easy problems first POJ 1920

POJ 2386 Lake counting

POJ 2227

Topological sort

Summary We have seen some examples on recursion. ◦ Hanoi tower ◦ Reverse of hanoi tower Flood fill is a recursive method to identify connected region. ◦ Running time = size of the region We can order a partial ordered set by topological sort ◦ Running time = number of edges Exercises: UVA 900, 254, 699, 10940, 527, 572, 657, 11110, 10305, 11686, 200