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.

Slides:



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

Analysis of Algorithms II
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.
Introduction to Algorithms Quicksort
AE1APS Algorithmic Problem Solving John Drake
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Induction and recursion
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
Function Optimization Newton’s Method. Conjugate Gradients
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Recursion.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
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 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Induction and recursion
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.
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Advanced Counting Techniques
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
RMIT University; Taylor's College1 Lecture 6  To apply the Principle of Mathematical Induction  To solve the Towers of Hanoi puzzle  To define a recurrence.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Reading and Writing Mathematical Proofs
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
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.
MATH 224 – Discrete Mathematics
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
+ Simulation Design. + Types event-advance and unit-time advance. Both these designs are event-based but utilize different ways of advancing the time.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 10 By Herbert I. Gross and Richard A. Medeiros next.
In section 11.9, we were able to find power series representations for a certain restricted class of functions. Here, we investigate more general problems.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
The Art of Effective Reasoning Roland Backhouse Tallinn, Estonia, 19th November 2002.
Types and Programming Languages
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Copyright © Cengage Learning. All rights reserved. Sequences and Series.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
Chapter 5. Section 5.1 Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
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.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
COMP108 Algorithmic Foundations Divide and Conquer
Induction and recursion
Gray Code Can you find an ordering of all the n-bit strings in such a way that two consecutive n-bit strings differed by only one bit? This is called the.
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.
Induction and recursion
Lectures on Graph Algorithms: searching, testing and sorting
Copyright © Cengage Learning. All rights reserved.
Presentation transcript:

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 solving. You probably know the problem, but we will look at it slightly differently. Induction gives a systematic way of finding a solution. However this solution is undesirable. A better solution is obtained by observing the invariants.

Begin with the solution We begin with the solution (as you probably already know it) And you can see where we are going. Another reason is to understand why a correct solution has been found, If no information about the solution method is provided.

Problem specification There is a temple in Bramah, where there are 3 giant poles fixed in the ground. On the first pole, God placed 64 discs, each of different size in decreasing order of size. The Brahmin monks were given the task of moving the discs, one per day, from one pole to another. However, no disc must be above a smaller disc, when on the same pole.

Triangle representation. Almost every book (and webpage) I have see draws the 3 poles in a line. We can call the poles A, B, C. However there is a simple symmetry which is being ignored here. There is no difference between B and C (assuming all the discs start on pole A) But we draw them in different positions. A better way is to draw and equilateral triangle, And the symmetry is obvious. Now the moves of the discs can easily be described as clockwise or anti clockwise.

Iterative solution (assumptions) There is a very easy solution to the Towers of Hanoi problem. It is easy to remember and execute. We assume the problem is to move the discs from one pole to the next in the clockwise direction. We assume the days are numbered from 0, 1, 2,,,,, On day 0, the discs are placed in their initial position, And the monks begin moving the discs on day 1.

Iterative solution 1 On every alternate day, the smallest disk is moved. The smallest disc should cycle around the poles. The direction of rotation depends on the number of discs. It the total number is odd, it cycles clockwise, Otherwise, anticlockwise.

Iterative solution 2 On every other day, another disc is moved (other than the smallest disc). As no disc can be on a smaller disc, there is only one possible move for the disc. The algorithm terminates when no further moves are possible, That is, on a even numbered day when all the discs are on the same pole. Try executing this algorithm yourself on 4 discs.

WHY? Presenting the solution like this, provides us with no help in understanding how the solution was constructed. How would we give a formal mathematical verification of the correctness of the algorithm. By observing a number of invariants, we show how to derive the algorithm from the inductive solution.

Inductive solution Obviously, induction on the number of discs. Let us call the poles A B C Suppose the task is to move M discs from one pole to another specific pole. Base case. When there are 0 discs, no steps are needed to complete the task. For the inductive step, we assume we can move n discs from A to B, And the problem is to show how to move n+1 discs from A to B. We get stuck with this approach.

Stuck 1 First move n top discs from A to B. After doing this, all the discs are on B. We have no hypothesis about moving discs from this pole. (illustrate this on the board)

Stuck 2 Alternatively, we can move the smallest from pole A to pole C. We then move n discs from A to B. Once again we have exhausted all possibilities of using the inductive hypothesis, Because the n discs are on pole B, And we have no hypothesis about moving discs from this pole.

Too Specific We have been too specific about the inductive hypothesis. The way out of this to recovery, is to introduce some parameters, Which model the start and final positions of the discs.

Rotational Symmetry We make a crucial decision. Rather than name the poles A B C, We observer that the problem exhibits a rotational symmetry. This is obvious when the poles are arranged in a triangle, But obscured when the poles are placed in a line.

Additional Parameter One additional parameter needs to be introduced. This is the direction of movement. i.e. we only need to say if a particular disc needs to be moved in a clockwise or counter clockwise direction. Also, the generalization of the problem becomes, How to move n discs from one pole to the next in the direction d (where d is either clockwise or anticlockwise)

Inductive hypothesis The inductive hypothesis we use is that it is possible to move the n smallest discs, From one pole to another in direction d, Beginning from any valid starting point. (that is, a starting position in which the discs are distributed arbitrarly over the poles, But no disc is on top of a disc smaller than itself).

Base and inductive case. For n = 0, the sequence of moves is empty. In the case of n+1 discs, we assume we have a method of moving the n smallest discs from one pole to one of the other two poles. We must show how to move n+1 discs from one pole to another pole in direction d, Where d is either clockwise or anticlockwise. Lets assume the discs are numbered 1 upwards (the smallest disc being numbered 1)

Using the inductive hypothesis There is little choice in exploiting the inductive hypothesis. We begin by moving the n smallest discs in the direction d, or direction (not)d. The solution is to move the n discs in direction (not)d, Then the n+1 th disc can be moved in direction d. Finally we can use the inductive hypothesis again for a second time, To move the n smallest discs in the direction (not) d. This places the n discs above the n+1 th disc, And all of the n+1 smallest discs have now been moved from there original position to a new pole in direction d. (draw a picture)

Code The following code summarises the inductive solution to the problem. Hn.d is the sequence of pairs Where n in the number of discs, K is a disc number and d and d’ are directions. Directions are Boolean values, true representing clockwise, and false representing counter clockwise. The pair means, move disc number k from its current position in the direction d’. The pair is therefore an action. We use the ; to separate movements.

Hn.d Hn.d means the sequence to move the n smallest discs in direction d. Taking the pairs in order from left to right, the complete sequence Hn.d, Prescribes how to move the n smallest discs, one by one, from one pole to another in direction d Write out the rules

Hn.d H0.d = [] Hn+1.d = Hn.(not)d ; [ ] ; Hn.(not)d Notice that the procedure name, H, occurs on both sides of the equation for Hn+1.d For this reason we call this a recursive solution (i.e. the procedure calls itself, think of Java).

How to use This inductive procedure gives us a way to generate the solution to the problem, for any n. We simply use the rules as rewrite rules, until all occurrences of H have been eliminated. i.e. we are left with just a sequence of actions (e.g. move disc 3 clockwise)

H2,cw Let us use cw and aw to mean clockwise and anticlockwise, Rather than true false, to improve readability. Show working on board p112 Exercise – you do H3.aw Can you see a pattern in the number of moves required.

Recursion vs Iteration The recursive solution is not the solution the monks were using. There solution was iteratively (repeatedly) executing a simple procedure depenent only on the current state. Conversely, The implementation of the inductive solution involves maintaining a stack of the sequence of moves yet to be executed. In java this can lead to “out of memory” runtime errors. There are interesting memory and speed issues here, Which you can address in a future course on Complexity.

The iterative solution Remember that the iterative solution had two main elements. First, that the smallest disc cycles around the poles (i.e. keeps going around in one direction) Second, that the disc to be moved alternates between the smallest disc and some other disc. We now show how these two properties can be derived from the inductive solution.

Cyclic moves In this section we show that the smallest disc cycles around the poles. In fact we can show that all discs cycle around the poles !!! And we can show which direction it is

The key - invariant For all pairs in the sequence Hn+1.d, The Boolean value even.k=d’ is invariant. Applying the formula, Hn+1.d, The parameter “n+1” is replaced by “n” And “d” is replaced by “not d” Since even.(n+1) = not.even.n The value of even(n+1) = d remains constant.

Initial conditions Even.k = d’ is true or false, And this depends on the initial values of n and d. Suppose these are N and D, then Even.k = d = even.n = D

In words If we require to move an even number of discs in a clockwise direction, All even numbered discs should cycle in a clockwise direction, And all odd numbers discs cycle in an anticlockwise direction. The converse is true. In particular, the smallest disc (number 1), Cycles in a direction opposite to D, If N is even, And in the same direction as D if N is odd.

Alternate discs The second fact was that the disc to be moved alternates between The smallest disc and some other disc. This is not difficult to see this. Two consecutive moves of the smallest disc can be done in one move. Two consecutive moves of any other disc do not change the state of the puzzle. We want to give a formal proof that the sequence Hn.d has this property.

Alternating Let us call a sequence of numbers alternating if it has two properties. First, consecutive elements alternate between 1 and a value greater than one. Second, when the sequence is not empty, it begins and ends with the value 1. We write alt.ks if the sequence ks has these properties.

Formally discn.k is the sequence of discs moved on successive days. Obtained by taking the first component of pairs in Hn.d, And ignoring the second directional component. By definition of H we get, disc0.d = [] discn+1.d = discn.not.d ; [n+1] ; discn.not.d