UNIT 17 Recursion: Towers of Hanoi.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

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.
The Towers of Hanoi or Apocalypse When?.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
CS1010: Programming Methodology
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.
Chapter Recurrence Relations
More Recursion: Permutations and Towers of Hanoi COP 3502.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 20 Recursion.
COSC 2006 Data Structures I Recursion III
Representational Choices The Towers of Hanoi Problem.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
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.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
25-2 Recursive Functions Related Chapter: ABC 5.14, 5.15.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS Final Project 2010 Rohan Paramesh. The Programs  Implementing 3 programs each in C# and Python  Student Scheduler  Assigns courses and students,
Recursion Jordi Cortadella Department of Computer Science.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
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.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
CS1010: Programming Methodology
Upon completion, the world will end…
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
UNIT 11 Random Numbers.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
WEEK 10 Class Activities Lecturer’s slides.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Aumm-e-hani munir Maria umer Rakhshanda batool Faiza.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
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.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Review of Recursion What is a Recursive Method?
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursion.
Recursion.
Pramada Ramachandran, Assistant Professor, Department of Mathematics,
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Jordi Cortadella Department of Computer Science
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
2.3 Recursion do automated demo of towers of hanoi before class
ECE 103 Engineering Programming
Review of Recursion What is a Recursive Method?
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
Recursion.
Review of Recursion What is a Recursive Method?
Tower of Hanoi problem: Move the pile of rings from one peg to another
Tower of Hanoi Problem Laboratory Experiment #1 Eltayeb Abuelyaman
Review of Recursion What is a Recursive Method?
Presentation transcript:

UNIT 17 Recursion: Towers of Hanoi

Unit 17: Recursion: Towers of Hanoi CS1010 (AY2014/5 Semester 1)Unit17 - 2© NUS Objectives:  Understand that many problems are more naturally solved with recursion, which can provide elegant solutions.  Taste the classic example of recursion: Towers of Hanoi.

Tower Of Hanoi (1/17) CS1010 (AY2014/5 Semester 1)Unit17 - 3© NUS  This classical “Towers of Hanoi” puzzle has attracted the attention of computer scientists more than any other puzzles.  Invented by Edouard Lucas, a French mathematician, in1883.  There are 3 pegs (A, B and C) and a tower of n disks on the first peg A, with the smallest disk on the top and the biggest at the bottom. The purpose of the puzzle is to move the whole tower from peg A to peg C, with the following simple rules:  Only one disk (the one at the top) can be moved at a time.  A bigger disk must not rest on a smaller disk.

Tower Of Hanoi (2/17) CS1010 (AY2014/5 Semester 1)Unit17 - 4© NUS  Demo: Tower of HanoiTower of Hanoi  We attempt to write a program to produce instructions on how to move the disks from peg A to peg C to complete the puzzle.  Example: A tower with 3 disks.  Output produced by program: Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C

Tower Of Hanoi (3/17) CS1010 (AY2014/5 Semester 1)Unit17 - 5© NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (4/17) CS1010 (AY2014/5 Semester 1)Unit17 - 6© NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (5/17) CS1010 (AY2014/5 Semester 1)Unit17 - 7© NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (6/17) CS1010 (AY2014/5 Semester 1)Unit17 - 8© NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (7/17) CS1010 (AY2014/5 Semester 1)Unit17 - 9© NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (8/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (9/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

Tower Of Hanoi (10/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Voilà!

Tower Of Hanoi (11/17) CS1010 (AY2014/5 Semester 1)Unit © NUS Can be interpreted as: 1. move four disks from peg A to peg B 2. move disk 5 from peg A to peg C 3. move four disks from peg B to peg C

Tower Of Hanoi (12/17) CS1010 (AY2014/5 Semester 1)Unit © NUS But how do we execute step 1? Or step 3? Step 3 can be interpreted as: 3.1 move three disks from peg B to peg A 3.2 move disk 4 from peg B to peg C 3.3 move three disks from peg A to peg C Towers after steps 1 and 2:

Tower Of Hanoi (13/17) CS1010 (AY2014/5 Semester 1)Unit © NUS Can you start to visualise how to solve this using recursion? Towers after steps 1, 2, 3.1 and 3.2:

Tower Of Hanoi (14/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Algorithm: if (n > 0) move n – 1 disks from the source peg to the temp peg using the dest peg move disk n from the source peg to the dest peg move n – 1 disks from the temp peg to the dest peg using the source peg

Tower Of Hanoi (15/17) CS1010 (AY2014/5 Semester 1)Unit © NUS #include void tower(char, char, char, int); int main(void) { int disks; printf("Number of disks: "); scanf("%d", &disks); tower('A','B','C', disks); return 0; } // Display instructions for moving n disk from source to dest // using temp as an auxiliary. Disks are numbered 1 to n // (smallest to largest). void tower(char source, char temp, char dest, int n) { if (n > 0) { tower(source, dest, temp, n-1); printf("Move disk %d from peg %c to peg %c\n", n, source, dest); tower(temp, source, dest, n-1); } Unit17_TowersOfHanoi.c

Tower Of Hanoi (16/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Tracing tower('A', 'B', 'C', 3); tower('A', 'B', 'C', 3); source is 'A' temp is 'B' dest is 'C' n is 3 tower ('A', 'C', 'B', 2) move 3 from A to C tower ('B', 'A', 'C', 2) source is 'A' temp is 'C' dest is 'B' n is 2 tower ('A', 'B', 'C', 1) move 2 from A to B tower ('C', 'A', 'B', 1) source is 'B' temp is 'A' dest is 'C' n is 2 tower ('B', 'C', 'A', 1) move 2 from B to C tower ('A', 'B', 'C', 1) source is 'A' temp is 'B' dest is 'C' n is 1 tower ('A', 'C', 'B', 0) move 1 from A to C tower ('B', 'A', 'C', 0) source is 'C' temp is 'A' dest is 'B' n is 1 tower ('C', 'B', 'A', 0) move 1 from C to B tower ('A', 'C', 'B', 0) source is 'B' temp is 'C' dest is 'A' n is 1 tower ('B', 'A', 'C', 0) move 1 from B to A tower ('C', 'B', 'A', 0) source is 'A' temp is 'B' dest is 'C' n is 1 tower ('A', 'C', 'B', 0) move 1 from A to C tower ('B', 'A', 'C', 0)

Tower Of Hanoi (17/17) CS1010 (AY2014/5 Semester 1)Unit © NUS  Output generated by tower('A', 'B', 'C', 3); Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C

End of File CS1010 (AY2014/5 Semester 1)Unit © NUS