CS 1110: Introduction to Computing Using Python Recursion

Similar documents
Revision: April 18, E Main Suite D Pullman, WA (509) Voice and Fax

Multiplication and Division

Lecture 22: Digital Transmission Fundamentals

t = 0 t = 8 (2 cycles)

Not for sale or distribution

The UK Linguistics Olympiad 2017

CSE231 Spring Updated 04/09/2019 Project 10: Basra - A Fishing Card Game

Problem A. Vera and Outfits

Reprint from IASTED International Conference on Signal and Image Processing (SIP 99). Nassau, Bahamas, October, 1999.

Performance Study on Multimedia Fingerprinting Employing Traceability Codes

CS 591 S1 Midterm Exam Solution

Series. Teacher. Numbers

Numbers and number relationships LO1

Midterm Examination. CSCI 561: Artificial Intelligence

Mathematics Success Grade 8

Girls Programming Network. Scissors Paper Rock!

N1 End-of-unit Test 2

Math 3012 Applied Combinatorics Lecture 2

CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

Objectives. Presentation Outline. Digital Modulation Lecture 04

A Zero-Error Source Coding Solution to the Russian Cards Problem

MICROWAVES Introduction to Laboratory Classes

EE140 Introduction to Communication Systems Lecture 7

Performance of Random Contention PRMA: A Protocol for Fixed Wireless Access

The Implement of Hydraulic Control System for Large- Scale Railway Maintenance Equipment Based on PLC

Wang, October 2016 Page 1 of 5. Math 150, Fall 2015 Exam 2 Form A Multiple Choice Sections 3A-5A

An Acquisition Method Using a Code-Orthogonalizing Filter in UWB-IR Multiple Access

EFFICIENT IIR NOTCH FILTER DESIGN VIA MULTIRATE FILTERING TARGETED AT HARMONIC DISTURBANCE REJECTION

In this project you will learn how to write a Python program telling people all about you. Type the following into the window that appears:

CHAPTER 3 BER EVALUATION OF IEEE COMPLIANT WSN

Windchimes, Hexagons, and Algebra

Thevenin Equivalent Circuits: (Material for exam - 3)

HW4: The Game of Pig Due date: Tuesday, Mar 15 th at 9pm. Late turn-in deadline is Thursday, Mar 17th at 9pm.

In this project, you ll learn how to create 2 random teams from a list of players. Start by adding a list of players to your program.

Notes on 4-coloring the 17 by 17 grid

HW4: The Game of Pig Due date: Thursday, Oct. 29 th at 9pm. Late turn-in deadline is Tuesday, Nov. 3 rd at 9pm.

Turbo-coded Multi-alphabet Binary CPM for Concatenated Continuous Phase Modulation

Version of 7. , using 30 points from 5 rad/s to 5 krad/s. Paste your plot below. Remember to label your plot.

In order for metogivebackyour midterms, please form. a line and sort yourselves in alphabetical order, from A

Spring 06 Assignment 2: Constraint Satisfaction Problems

EE (082) Chapter IV: Angle Modulation Lecture 21 Dr. Wajih Abu-Al-Saud

CMSC 201 Fall 2018 Project 3 Sudoku

CSE 231 Spring 2013 Programming Project 03

Resilience. Being resilient on the other hand, helps a person in many different ways. Such a person:

=, where f is focal length of a lens (positive for convex. Equations: Lens equation

DESIGN AND PERFORMANCE ANALYSIS OF BAND PASS IIR FILTER FOR SONAR APPLICATION

Clever Hangman. CompSci 101. April 16, 2013

Effect of Pulse Shaping on Autocorrelation Function of Barker and Frank Phase Codes

Power Budgeted Packet Scheduling for Wireless Multimedia

Homework: Please number questions as numbered on assignment, and turn in solution pages in order.

CPSC 217 Assignment 3

Beginning Game Programming, COMI 2040 Lab 6

LING 388: Computers and Language. Lecture 10

CPCS 222 Discrete Structures I Counting

Guide 1. User Guide. window. program.

Unit 1: Free time Focus on reading: skimming and scanning

Location Fingerprint Positioning Based on Interval-valued Data FCM Algorithm

Lecture 13 Register Allocation: Coalescing

CS 540-2: Introduction to Artificial Intelligence Homework Assignment #2. Assigned: Monday, February 6 Due: Saturday, February 18

CS 51 Homework Laboratory # 7

Make sure your name and FSUID are in a comment at the top of the file.

Spring 06 Assignment 2: Constraint Satisfaction Problems

INSTALLATION GUIDE. Perfect PANELLING c

RADAR TARGET RECOGNITION BASED ON PARAMETERIZED HIGH RESOLUTION RANGE PROFILES

Designing Information Devices and Systems I Fall 2018 Homework 10

4.2 Proving and Applying

Key-Words: - Software defined radio, Walsh Hadamard codes, Lattice filter, Matched filter, Autoregressive model, Gauss-Markov process.

CS123. Programming Your Personal Robot. Part 3: Reasoning Under Uncertainty

Writing Equations of Parallel and Perpendicular Lines 4.3. Essential Question How can you recognize lines that are parallel or perpendicular?

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

NAVAL POSTGRADUATE SCHOOL THESIS

Oracle Turing Machine. Kaixiang Wang

GIS Programming Practicuum

School Based Projects

UNIT 9B Randomness in Computa5on: Games with Random Numbers Principles of Compu5ng, Carnegie Mellon University - CORTINA

Layered Space-Time Codes for Wireless Communications Using Multiple Transmit Antennas

CS 3233 Discrete Mathematical Structure Midterm 2 Exam Solution Tuesday, April 17, :30 1:45 pm. Last Name: First Name: Student ID:

Multifunction Electric. Power Meter (LCD type J) USER S MANUAL

New Approach in Gate-Level Glitch Modelling *

A Robust Image Restoration by Using Dark channel Removal Method

Homework Assignment #2

Enumerative Combinatoric Algorithms. Gray code

CS 32 Puzzles, Games & Algorithms Fall 2013

. MA111: Contemporary mathematics. Jack Schmidt. November 9, 2012

The following may be of use in this test: 5! = Two rows of Pascal s triangle are:

CSE 231 Fall 2012 Programming Project 8

Game Artificial Intelligence ( CS 4731/7632 )

DSP First Lab 05: FM Synthesis for Musical Instruments - Bells and Clarinets

Freezing Simulates Non-Freezing Tile Automata

A focal nugget and accent drops of red coral are a bold splash of color. The background necklace features a more subtle palette of turquoise beads.

De Anza College Department of Engineering Engr 37-Intorduction to Circuit Analysis

IMAGE RECONSTRUCTION FROM OMNI-DIRECTIONAL CAMERA Kai Guo and Zhuang Li

Series. Teacher. Numbers

Due: Sunday 13 November by 10:59pm Worth: 8%

This assignment is worth 75 points and is due on the crashwhite.polytechnic.org server at 23:59:59 on the date given in class.

Texas Instruments Analog Design Contest

Time Reversal Synthetic Aperture Radar Imaging In Multipath

You Know More Than You Think ;) 3/6/18 Matni, CS8, Wi18 1

Extended Introduction to Computer Science CS1001.py

Research on Blanket Jamming to Beidou Navigation Signals Based on BOC Modulation

Transcription:

CS 1110: Introdution to Computing Using Python Leture 15 Reursion [Andersen, Gries, Lee, Marshner, Van Loan, White]

Announements: Prelim 1 Graded and released Mean: 81 out of 104 (78%) Can pik up your exam in homework handak room Need Cornell ID Suggest printing your netid on paper Do not disuss exam with people taking makeups. Regrade requests: we will send email to you 10/13/16 Reursion 2

Announements: Assignment 3 Released. Due: Thursday, Marh 30 th, 11:59pm Reommendation: follow milestone deadlines. You MUST aknowledge help from others We run software analyzers to detet similar programs Have had some aademi integrity violations so far Not a reursion assignment! 10/13/16 Reursion 3

Out. Not a reursion la! Announement: La 8 10/13/16 Reursion 4

Reursion Reursive Definition: A definition that is defined in terms of itself 10/13/16 Reursion 5

A Mathematial Example: Fatorial Non-reursive definition: n! = n n-1 2 1 = n (n-1 2 1) Reursive definition: n! = n (n-1)! for n 0 0! = 1 Reursive ase Base ase What happens if there is no ase ase? 10/13/16 Reursion 6

Reursion Reursive Definition: A definition that is defined in terms of itself Reursive Funtion: A funtion that alls itself (diretly or indiretly) 10/13/16 Reursion 7

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1 n 3 3 return n*fatorial(n-1) Call: fatorial(3) 10/13/16 Reursion 8

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 3 return n*fatorial(n-1) Call: fatorial(3) 10/13/16 Reursion 9

Reursion 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 3 return n*fatorial(n-1) Call: fatorial(3) Now what? Eah all is a new frame. 10/13/16 Reursion 10

What happens next? fatorial 1, 3 n 3 A: fatorial 1, 3 n 3 fatorial 1 n 2 C: CORRECT ERASE FRAME fatorial 1 n 3 B: fatorial 1, 3, 1 n 3 2 D: fatorial 1, 3, 1 n 3 2 fatorial 1 n 2 10/13/16 Reursion 11

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1 n 2 3 return n*fatorial(n-1) Call: fatorial(3) 10/13/16 Reursion 12

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 3 return n*fatorial(n-1) Call: fatorial(3) 10/13/16 Reursion 13

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1 3 return n*fatorial(n-1) n 1 Call: fatorial(3) 10/13/16 Reursion 14

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) n 1 Call: fatorial(3) 10/13/16 Reursion 15

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) n 1 Call: fatorial(3) fatorial n 0 1 10/13/16 Reursion 16

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) n 1 Call: fatorial(3) fatorial n 0 1, 2 10/13/16 Reursion 17

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 18

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 19

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 20

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 21

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 RETURN 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 22

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 fatorial 1, 3 n 2 RETURN 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 23

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 RETURN 6 fatorial 1, 3 n 2 RETURN 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 24

Reursive Call Frames 1 2 def fatorial(n): """Returns: fatorial of n. Pre: n 0 an int""" if n == 0: return 1 fatorial 1, 3 n 3 RETURN 6 fatorial 1, 3 n 2 RETURN 2 fatorial 1, 3 3 return n*fatorial(n-1) Call: fatorial(3) n 1 RETURN 1 fatorial 1, 2 n 0 RETURN 1 10/13/16 Reursion 25

Example: Fionnai Sequene Sequene of numers: 1, 1, 2, 3, 5, 8, 13,... a 0 a 1 a 2 a 3 a 4 a 5 a 6 Get the next numer y adding previous two What is a 8? A: a 8 = 21 B: a 8 = 29 C: a 8 = 34 D: None of these. 10/13/16 Reursion 26

Example: Fionnai Sequene Sequene of numers: 1, 1, 2, 3, 5, 8, 13,... a 0 a 1 a 2 a 3 a 4 a 5 a 6 Get the next numer y adding previous two What is a 8? A: a 8 = 21 B: a 8 = 29 C: a 8 = 34 orret D: None of these. 10/13/16 Reursion 27

Example: Fionnai Sequene Sequene of numers: 1, 1, 2, 3, 5, 8, 13,... a 0 a 1 a 2 a 3 a 4 a 5 a 6 Get the next numer y adding previous two What is a 8? Reursive definition: a n = a n-1 + a n-2 a 0 = 1 a 1 = 1 Reursive Case Base Case (another) Base Case Why did we need two ase ases this time? 10/13/16 Reursion 28

Fionai as a Reursive Funtion def fionai(n): """Returns: Fionai no. a n Preondition: n 0 an int""" if n <= 1: return 1 Base ase(s) return (fionai(n-1)+ fionai(n-2)) Reursive ase Handles oth ase ases in one onditional. 10/13/16 Reursion 29

Fionai as a Reursive Funtion def fionai(n): """Returns: Fionai no. a n Preondition: n 0 an int""" if n <= 1: return 1 return (fionai(n-1)+ fionai(n-2)) fionai 3 n 5 fionai 1 fionai 1 n 4 n 3 10/13/16 Reursion 30

Reursion vs Iteration Reursion is provaly equivalent to iteration Iteration inludes for-loop and while-loop (later) Anything an do in one, an do in the other But some things are easier with reursion And some things are easier with iteration Will not teah you when to hoose reursion We just want you to understand the tehnique 10/13/16 Reursion 31

Reursion is est for Divide and Conquer Goal: Solve prolem P on a piee of data data 10/13/16 Reursion 32

Reursion is est for Divide and Conquer Goal: Solve prolem P on a piee of data data Idea: Split data into two parts and solve prolem data 1 data 2 Solve Prolem P Solve Prolem P 10/13/16 Reursion 33

Reursion is est for Divide and Conquer Goal: Solve prolem P on a piee of data data Idea: Split data into two parts and solve prolem data 1 data 2 Solve Prolem P Solve Prolem P Comine Answer! 10/13/16 Reursion 34

Divide and Conquer Example Count the numer of 'e's in a string: p e n n e Two 'e's p e n n e One 'e' One 'e' 10/13/16 Reursion 35

Divide and Conquer Example Count the numer of 'e's in a string: p e n n e Two 'e's p e n n e Zero 'e's Two 'e's 10/13/16 Reursion 36

Three Steps for Divide and Conquer 1. Deide what to do on small data Some data annot e roken up Have to ompute this answer diretly 2. Deide how to reak up your data Both halves should e smaller than whole Often no wrong way to do this (next leture) 3. Deide how to omine your answers Assume the smaller answers are orret Comining them should give igger answer 10/13/16 Reursion 37

Divide and Conquer Example def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Comine the result return left+right Short-ut for s[0] if s[0] == 'e : return 1 else: return 0 p e n n 0 + 2 s[1:] e 10/13/16 Reursion 38

Divide and Conquer Example def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Comine the result return left+right Short-ut for s[0] if s[0] == 'e : return 1 else: return 0 p e n n 0 + 2 s[1:] e 10/13/16 Reursion 39

Divide and Conquer Example def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Comine the result return left+right Short-ut for s[0] if s[0] == 'e : return 1 else: return 0 p e n n 0 + 2 s[1:] e 10/13/16 Reursion 40

Divide and Conquer Example def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Comine the result return left+right Short-ut for s[0] if s[0] == 'e : return 1 else: return 0 p e n n 0 + 2 s[1:] e 10/13/16 Reursion 41

Divide and Conquer Example def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 Base Case # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Comine the result return left+right Reursive Case 10/13/16 Reursion 42

Exerise: Remove Blanks from a String def (s): """Returns: s ut with its lanks removed""" 1. Deide what to do on small data If it is the empty string, nothing to do if s == '': return s If it is a single harater, delete it if a lank if s == ' ': # There is a spae here return '' # Empty string else: return s 10/13/16 Reursion 43

Exerise: Remove Blanks from a String def (s): """Returns: s ut with its lanks removed""" 2. Deide how to reak it up left = (s[0]) # A string with no lanks right = (s[1:]) # A string with no lanks 3. Deide how to omine the answer return left+right # String onatenation 10/13/16 Reursion 44

Putting it All Together def (s): """Returns: s w/o lanks""" if s == '': return s elif len(s) == 1: return '' if s[0] == ' ' else s left = (s[0]) right = (s[1:]) return left+right Handle small data Break up the data Comine answers 10/13/16 Reursion 45

Putting it All Together def (s): """Returns: s w/o lanks""" if s == '': return s elif len(s) == 1: return '' if s[0] == ' ' else s left = (s[0]) right = (s[1:]) return left+right Base Case Reursive Case 10/13/16 Reursion 46

Following the Reursion a 10/13/16 Reursion 47

Following the Reursion a a stops (ase ase) a stops (ase ase) 10/13/16 Reursion 48

Following the Reursion a a 10/13/16 Reursion 49

Following the Reursion a a a 10/13/16 Reursion 50

Following the Reursion a a a 10/13/16 Reursion 51

Following the Reursion a a a 10/13/16 Reursion 52

Following the Reursion a a a 10/13/16 Reursion 53

Following the Reursion a a a

Following the Reursion a a a

Following the Reursion a a a

Following the Reursion a a a

Following the Reursion a a a

Following the Reursion a a a a

Following the Reursion a a a a a

Following the Reursion a a a a a a

Tower of Hanoi Three towers: left, middle, and right n disks of unique sizes on left Goal: move all disks from left to right Cannot put a larger disk on top of a smaller disk 1 2 3 4 left middle right 10/13/16 Reursion 62

1 Dis 1. Move from left to right 1 left middle right 10/13/16 Reursion 63

1 Dis 1. Move from left to right left middle right 1 10/13/16 Reursion 64

2 Diss 1. Move from left to middle 1 2 left middle right 10/13/16 Reursion 65

2 Diss 1. Move from left to middle 2. Move from left to right 2 1 left middle right 10/13/16 Reursion 66

2 Diss 1. Move from left to middle 2. Move from left to right 3. Move from middle to right 1 left middle right 2 10/13/16 Reursion 67

2 Diss 1. Move from left to middle 2. Move from left to right 3. Move from middle to right 1 2 left middle right 10/13/16 Reursion 68

3 Diss 1. Move from left to right 1 2 3 left middle right 10/13/16 Reursion 70

3 Diss 1. Move from left to right 2. Move from left to middle 2 3 1 left middle right 10/13/16 Reursion 71

3 Diss 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 3 2 1 left middle right 10/13/16 Reursion 72

1 3 2 left middle right 3 Diss 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 10/13/16 Reursion 73

3 Diss 1 2 left middle right 3 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 10/13/16 Reursion 74

3 Diss 1 2 3 left middle right 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 6. Move from middle to right 10/13/16 Reursion 75

3 Diss 2 1 3 left middle right 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 6. Move from middle to right 7. Move from left to right 10/13/16 Reursion 76

3 Diss 1 2 left middle right 3 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 6. Move from middle to right 7. Move from left to right 10/13/16 Reursion 77

4 Diss: High-level Idea 1 2 3 4 left middle right 10/13/16 Reursion 78

4 Diss: High-level Idea Plan: move top three disks from left to middle 4 1 2 3 left middle right 10/13/16 Reursion 79

4 Diss: High-level Idea 1 2 3 4 left middle right Plan: move top three disks from left to middle Move: largest disk from left to right 10/13/16 Reursion 80

4 Diss: High-level Idea 1 2 3 4 Plan: move top three disks from left to middle Move: largest disk from left to right Plan: move top three disks from middle to right left middle right 10/13/16 Reursion 81

4 Diss: High-level Idea Plan: move disks 1, 2, and 3 from left to middle 1 2 3 4 left middle right 10/13/16 Reursion 82

4 Diss: High-level Idea Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right 3 4 1 2 left middle right 10/13/16 Reursion 83

4 4 Diss: High-level Idea 1 3 2 left middle right Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right 10/13/16 Reursion 84

4 Diss: High-level Idea 4 1 2 3 left middle right Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle 10/13/16 Reursion 85

4 Diss: High-level Idea 4 1 2 3 left middle right Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle 10/13/16 Reursion 86

4 Diss: High-level Idea 1 2 3 4 left middle right Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle Move: disk 4 from left to right 10/13/16 Reursion 87

4 Diss: High-level Idea 1 2 3 4 left middle right Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle Move: disk 4 from left to right Plan: move disks 1, 2, and 3 from middle to right 10/13/16 88

4 Diss: High-level Idea Plan: move disks 1, 2, and 3 from left to middle 1 2 3 4 Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle left middle right Move: disk 4 from left to right Plan: move disks 1, 2, and 3 from middle to right 10/13/16 89

Oservation: Plans within a Plan High-level plan Low-level plan Plan: move disks 1, 2, and 3 from left to middle Plan: move disks 1 and 2 from left to right Move: disk 3 from left to right Plan: move disks 1 and 2 from right to middle Move: disk 4 from left to right Plan: move disks 1, 2, and 3 from middle to right 10/13/16 90

General Pattern To move n disks from soure to target: 1 2 3 disks 1,, n-1 4 disk n soure other disks? other target (soure, other, and target an e any permutation of left, middle and right) 1. Plan: move disks 1,, n-1 from soure to other 2. Move: disk n to from soure to target 3. Plan: move disks 1,, n-1 from other to target 10/13/16 Reursion 91