School of EECS, Peking University

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?.
By Katie Dzerovych Jon Wood JB Henderson Mentor Professor Snapp.
The Tower Of Hanoi Edouard Lucas
More Recursion: Permutations and Towers of Hanoi COP 3502.
AE1APS Algorithmic Problem Solving John Drake
Analyzing Algorithms and Problems Prof. Sin-Min Lee Department of Computer Science.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
The Tower of Hanoi Ben Epstein Special Topics 2003.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND 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 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © Cengage Learning. All rights reserved.
Advanced Counting Techniques
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
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.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
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.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
RECURSION. Objectives: become familiar with the idea of recursion Examine examples to show the wide variety of situations to which recursion can be applied.
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.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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,
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
Upon completion, the world will end…
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
Recursion Colin Capham Recursion – an introduction to the concept.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
UNIT 17 Recursion: Towers of Hanoi.
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.
INTRO2CS Tirgul 6. Understanding the Traceback # in file t.py: def a(L): return b(L) def b(L): return L.len() #should have been len(L) # in the python.
Recursion (Continued)
Recursion.
Recursion COMP T1 #6.
Recursion.
Recursion Topic 5.
Introduction to Recurrence Relations
Constraint Satisfaction Problems vs. Finite State Problems
Uninformed Search Strategies
Recursion in Java December 12, 2005
Chapter 8: Recursion Java Software Solutions
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Copyright © Cengage Learning. All rights reserved.
ECE 103 Engineering Programming
Chapter 8: Recursion Java Software Solutions
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.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Copyright © Cengage Learning. All rights reserved.
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Problem Solving WEEK 3.
Java Software Solutions Foundations of Program Design Sixth Edition
Presentation transcript:

School of EECS, Peking University Lab#2: Recursion Hongfei Yan School of EECS, Peking University 3/25/2015

Run & Debug The turtledemo package includes a set of demo scripts. These scripts can be run and viewed using the supplied demo viewer as follows: python -m turtledemo Alternatively, you can run the demo scripts individually. For example, python -m turtledemo.bytedesign python -m pdb minimal_hanoi.py http://interactivepython.org/courselib/static/pythonds/Recursion/recursioncomplex.html

Debug

Complex Recursive Problems some problems that are really difficult to solve using an iterative programming style but are very elegant and easy to solve using recursion. http://interactivepython.org/courselib/static/pythonds/Recursion/recursioncomplex.html

Tower of Hanoi invented by the French mathematician Edouard Lucas in 1883. inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. At the beginning of time, the priests were given three poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. They could only move one disk at a time, and they could never place a larger disk on top of a smaller one. The priests worked very efficiently, day and night, moving one disk every second. When they finished their work, the legend said, the temple would crumble into dust and the world would vanish.

An Example Arrangement of Disks for the Tower of Hanoi Although the legend is interesting, you need not worry about the world ending any time soon. The number of moves required to correctly move a tower of 64 disks is 2^64−1. At a rate of one move per second, that is 584,942,417,355 years! Clearly there is more to this puzzle than meets the eye.

a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole: Move a tower of height-1 to an intermediate pole, using the final pole. Move the remaining disk to the final pole. Move the tower of height-1 from the intermediate pole to the final pole using the original pole.

Solving Tower of Hanoi Recursively (hanoi)

Towers of Hanoi – Turtles as Hanoi-Discs https://docs.python.org/3/library/turtle.html The turtledemo package includes a set of demo scripts. These scripts can be run and viewed using the supplied demo viewer as follows: python -m turtledemo Alternatively, you can run the demo scripts individually. For example, python -m turtledemo.bytedesign

Run & Debug

Exploring a Maze

Exploring a Maze The problem we want to solve is to help our turtle find its way out of a virtual maze. The maze problem has roots as deep as the Greek myth about Theseus who was sent into a maze to kill the minotaur. Theseus used a ball of thread to help him find his way back out again once he had finished off the beast. In our problem we will assume that our turtle is dropped down somewhere into the middle of the maze and must find its way out.  http://interactivepython.org/courselib/static/pythonds/Recursion/recursioncomplex.html

a systematic procedure to find its way out of the maze From our starting position we will first try going North one square and then recursively try our procedure from there. If we are not successful by trying a Northern path as the first step then we will take a step to the South and recursively repeat our procedure. If South does not work then we will try a step to the West as our first step and recursively apply our procedure. If North, South, and West have not been successful then apply the procedure recursively from a position one step to our East. If none of these directions works then there is no way to get out of the maze and we fail.

in an infinite loop If we apply the recursive procedure from there we will just go back one step to the North and be in an infinite loop. So, we must have a strategy to remember where we have been. In this case we will assume that we have a bag of bread crumbs we can drop along our way. If we take a step in a certain direction and find that there is a bread crumb already on that square, we know that we should immediately back up and try the next direction in our procedure.

Four base cases The turtle has run into a wall. Since the square is occupied by a wall no further exploration can take place. The turtle has found a square that has already been explored. We do not want to continue exploring from this position or we will get into a loop. We have found an outside edge, not occupied by a wall. In other words we have found an exit from the maze. We have explored a square unsuccessfully in all four directions.

use the turtle module to draw and explore our maze __init__ Reads in a data file representing a maze, initializes the internal representation of the maze, and finds the starting position for the turtle. drawMaze Draws the maze in a window on the screen. updatePosition Updates the internal representation of the maze and changes the position of the turtle in the window. isExit Checks to see if the current position is an exit from the maze. The Maze class also overloads the index operator [] so that our algorithm can easily access the status of any particular square.

Maze Data File

the internal representation for the data file