MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
AE1APS Algorithmic Problem Solving John Drake
PROLOG 8 QUEENS PROBLEM.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Chapter 15 Recursive Algorithms.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
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.
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.
Recursion Chapter 5.
Announcements.
Lesson 13-1: Matrices & Systems Objective: Students will: State the dimensions of a matrix Solve systems using matrices.
COSC2007 Data Structures II
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Two Dimensional Arrays
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
EXAMPLES OF RECURSION Towers of Hanoi Writing Linked Lists Backwards Recursive Insert 8 Queens Recognizing Simple Languages Prefix Expressions Conversion.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
Data Structures Arrays and Lists Part 2 More List Operations.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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 – some examples. Sum of Squares Write Vertical.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Stacks
Section 4.4 Counting Blobs
Recursion (Part 2).
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Java Lecture Recursion 29th feb 2005
Java Software Structures: John Lewis & Joseph Chase
CSCE 3110 Data Structures & Algorithm Analysis
Algorithm Design and Analysis (ADA)
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 12 Recursion (methods calling themselves)
The Hanoi Tower Problem
Ch. 6 Recursion as a Problem Solving Technique
Recursion When performing a repetitive task either: a loop recursion
Exercise: Dice roll sum
CSE 143 Lecture 18 More Recursive Backtracking
Recursion as a Problem-Solving Technique
CSE 143 Lecture 18 More Recursive Backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Presentation transcript:

MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Some more examples in recursion The eight queens problems Eight queens are to be placed on a chess board so that they do not check each other. The program should print out the first solution it finds.

Eight queens problem So we would have a class like this: public class EightQueen {.... public void main(String st[]) {..... } }

Eight queens problem No two queens can be put into the same column, or the same row, or the same diagonal line. Since the chess board has 8 rows, 8 columns and 15 diagonal lines from top right to bottom left and 15 diagonal lines from top left to bottom right, so there should only be one queen in each row, one queen in each column and at most one queen in one diagonal line.

Eight queens problem Assume that we are putting a queen in one row from top to bottom. So we do not need to do any book keeping for queens in rows. But, we need to store information regarding which columns have already occupied so that the next move should not be put in those columns. Similarly, we also need to store information regarding which diagonal lines have been occupied.

Eight queens problem Let x, y represents the row and columns of a position in the chess board so that they are both from 0 to 7. So a particular value of x represents a row, a particular value of y represents a column, a particular value of x+y represents a diagonal line running the from top left to bottom right, a particular value of x-y represents a diagonal line running from top right to bottom left.

Eight queens problem As both x, y are from 0 to 7, so x+y can range from 0 to 14 and x-y can range from -7 to 7. So it is easy to use an array of boolean to represents whether a particular x+y type of diagonal line is occupied because x+y has values starting from 0 which is exactly what Java needs.

Eight queens problem However, for the x-y style of diagonal lines, the value ranges from -7 to 7. So the value is not suitable to be used directly as index of an array. So we need to add 7 to the value so that the value becomes from 0 to 14 as well.

Eight queens problem So we have 3 arrays of boolean that represents whether a column, an x+y style of diagonal line and an x-y style of diagonal line is occupied. Just to remain you that we do not need such an array to store information regarding whether a row is occupied or not because we are putting a queen on each row one by one.

Eight queen problem public class EightQueens { boolean column[]=new boolean[8]; boolean xaddy[]=new boolean[15]; //x+y style diagonal line boolean xsuby[]=new boolean[15]; //x-y style diagonal line public EightQueens() { for (int i=0;i<8;i++) { column[i]=false; } for (int i=0;i<15;i++) { xaddy[i]=xsuby[i]=false; }

Eight queens problem Now assume that we have a method to put the n-th move and the method should return true if this move will finally give a valid result. So in this method, we would try all the positions in the row to see if the we can put a queen in the positions. If the answer is yes, then we need to check whether this move is the last move. If it is, then we have successfully get a valid result.

Eight queens problem If the move is not the last move, then we need to recursively call the method again to make the next move. After returning from that method, if the result is false, this means that the last move is invalid, so we undo the move and try next position. If there is no more position left in the row, then this means that there is no hope to get a valid result and we just return false.

public boolean move(int y) { for (int x=0;x<8;x++) { if (column[x] || xaddy[x+y] || xsuby[7+x-y]) { continue; // the position is not valid, try another } column[x]=xaddy[x+y]=xsuby[7+x-y]=true; //try this position if (y==7) { System.out.println("A valid result is found"); System.out.println(x+" "+y); return true; //a valid result is found } else { if (move(y+1)) { System.out.println(x+" "+y); //print the result return true; } else { column[x]=xaddy[x+y]=xsuby[7+x-y]=false; //undo } return false; }

Eight Queens problem Now, we want to change the program so that it prints out all possible solutions.

Tower of hanoi There are three poles and a number of discs so that all the discs are of different sizes. Each disc has a hole so that they can be put onto the poles. Originally, all the discs are in one pole so that the discs increasing in size from bottom to top.

Tower of Hanoi Now, we want to move the discs from the first pole to the second so that when we move the discs, no disc is allowed to put on a disc with smaller size.

Tower of Hanoi public class Pole { int id; public Pole(int i) {id=i;} public void moveADiscFrom(Pole p) { System.out.println("move a disc from "+p.id+" to "+id); } public move(int noDisc, Pole p2, Pole p3) { //move top noDisc from this pole //to pole p2 with p3 as intermediate pole }

Tower of Hanoi If noDisc is 0, then the method would do nothing. move top noDisc-1 discs to pole3, then move the bottom disc to pole2, then move the noDisc-1 discs from pole3 to pole2.

Tower of Hanoi public void move(int disc, Pole p2, Pole p3) { if (disc==0) { return; } else { move(disc-1, p3,p2); System.out.println("move a disc from pole "+id+" to pole "+p2.id); p3.move(disc-1,p2,this ); }

Eight queens problem