AE1APS Algorithmic Problem Solving John Drake

Slides:



Advertisements
Similar presentations
Copyright © Cengage Learning. All rights reserved.
Advertisements

3/25/2017 Chapter 16 Recursion.
Analysis of Algorithms II
Algorithm Design Techniques
Cognitive Computing 2013 Consciousness and Computations 7. THE SELF APPLICABILITY PROBLEM (SAP); PENROSE ON UNDERSTANDING UNDERSTANDING Mark Bishop.
Algebra Problems… Solutions
Discrete Math Recurrence Relations 1.
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.
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Week 6 - Wednesday CS322.
Introduction to Algorithms
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Chapter 7 Hypothesis Testing
Part 1 Module 1 Sets, elements, subsets In this course we cover a variety of topics, some of which may seem to be completely unrelated to others. Set mathematics.
The simplex algorithm The simplex algorithm is the classical method for solving linear programs. Its running time is not polynomial in the worst case.
AE1APS Algorithmic Problem Solving John Drake
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © Cengage Learning. All rights reserved.
AE1APS Algorithmic Problem Solving John Drake.  Invariants – Chapter 2  River Crossing – Chapter 3  Logic Puzzles – Chapter 5  Matchstick Games -
Factoring Polynomials
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
INTEGRALS The Substitution Rule In this section, we will learn: To substitute a new variable in place of an existing expression in a function,
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.
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.
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.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Survey of Mathematical Ideas Math 100 Chapter 1 John Rosson Tuesday January 23, 2007.
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
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.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
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.
MATH 224 – Discrete Mathematics
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
Extending the Definition of Exponents © Math As A Second Language All Rights Reserved next #10 Taking the Fear out of Math 2 -8.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
The Art of Effective Reasoning Roland Backhouse Tallinn, Estonia, 19th November 2002.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
CIE Centre A-level Further Pure Maths
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.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
COMP108 Algorithmic Foundations Divide and Conquer
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.
Copyright © Cengage Learning. All rights reserved.
12 Systems of Linear Equations and Inequalities.
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
Basic Concepts of Algorithm
Recursive Thinking.
Presentation transcript:

AE1APS Algorithmic Problem Solving John Drake Tower of Hanoi AE1APS Algorithmic Problem Solving John Drake

Outline of the course Invariants – Chapter 2 River Crossing – Chapter 3 Logic Puzzles – Chapter 5 Matchstick Games - Chapter 4 Sum Games – Chapter 4 Induction – Chapter 6 Tower of Hanoi – Chapter 8

A recap on induction Breaking larger problems down into sub- problems Using small instances of a problem to help us solve larger instances Find a trivial base case where size = 0 Show for an arbitrary number n how to solve n + 1 (induction step)

Tower of Hanoi - Introduction This problem is discussed in many maths texts, and is used in computer science and 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 of the inductive solution

Begin with the solution We will start at the end 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

Tower of Hanoi

Triangle representation Almost every book (and webpage) I have seen 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

Triangle representation 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 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 if the total is even, it cycles 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 

Iterative solution – 4 discs Day 0

Iterative solution – 4 discs Day 1

Iterative solution – 4 discs Day 2

Iterative solution – 4 discs Day 3

Iterative solution – 4 discs Day 4

Iterative solution – 4 discs Day 5

Iterative solution – 4 discs Day 6

Iterative solution – 4 discs Day 7

Iterative solution – 4 discs Day 8

Iterative solution – 4 discs Day 9

Iterative solution – 4 discs Day 10

Iterative solution – 4 discs Day 11

Iterative solution – 4 discs Day 12

Iterative solution – 4 discs Day 13

Iterative solution – 4 discs Day 14

Iterative solution – 4 discs Day 15

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 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 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 i.e. The information that we can move n discs from A to B does not help us in trying to move n+1 from A to B

Example with n = 3 B A C

Example with n = 3 B A C

Still stuck 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 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 observe 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.

An additional parameter One additional parameter needs to be introduced, the direction of movement We only need to say if a particular disc needs to be moved in a clockwise or anti- clockwise direction The generalisation 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 anti-clockwise)

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 arbitrarily over the poles, but no disc is on top of a disc smaller than itself This is a far more general definition

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. Assume the discs are numbered 1 upwards (with the smallest disc numbered 1)

Using the inductive hypothesis There is little choice in exploiting the inductive hypothesis we can begin by moving the n smallest discs in the direction d, or direction ¬d. The solution is to move the n discs in direction ¬d, then the n+1th disc can be moved in direction d We can then use the inductive hypothesis again to move the n smallest discs in the direction ¬d. This places the n discs above the n+1th disc, and all of the n+1 smallest discs have now been moved from their original position to a new pole in direction d

Example with n = 3

Example with n = 3

Example with n = 3

Example with n = 3

For n+1 (i.e. 3+1 = 4) We can use the previous solution to get a solution for n+1 using the inductive step

Example with n = 4

Example with n = 4

Example with n = 4

Example with n = 4

Psuedocode The following code summarises the inductive solution to the problem Hn(d) is the sequence of pairs <k,d’> 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 anti-clockwise The pair <k, d’> 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

Psuedocode Hn(d) H0(d) = [] – base case (i.e. 0 moves needed) Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d) – induction step (move n discs in a given direction, move the n+1th disc in the opposite direction then move n discs again in the same direction)

Psuedocode Notice that the procedure name, H, occurs on both sides of the equation for Hn+1(d) Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬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 size 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 anti-clockwise, rather than true and false to improve readability Calculate H2(cw) i.e. how to move 2 discs, one pole in a clockwise direction Equation (1) H0(d) = [] Equation (2) Hn+1(d) = Hn(¬d) ; [<n+1, d>] ; Hn(¬d)

Example with n = 2 <1,aw>

Example with n = 2 <2,cw>

Example with n = 2 <1,aw>

Example with n = 2

H3(cw)? Exercise – Can you do H3(aw)? Can you see a pattern in the number of moves required for n discs? i.e. H0, H1, H2, H3… Show this as a formula in terms of n? If this is easy, try exercise 8.3 a) (without cheating and looking at the answer first) 