1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.

Slides:



Advertisements
Similar presentations
The Singleton Pattern II Recursive Linked Structures.
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers.
Backtracking.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer The Power of the Recursive Algorithms.
1 Teaching Programming with Sudoku Bill Sanders for Axel T. Schreiner Killer Examples Workshop at OOPSLA’09.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
Recursion Chapter 5.
Announcements.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
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
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Two Dimensional Arrays
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.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
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"?
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion Powerful Tool
Tirgul 7 Recursion.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Comments on the “Three Piles” Problem
CS100J Lecture 19 Previous Lecture This Lecture
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
8-Queens Puzzle.
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"?
CSE 143 Lecture 19 More Recursive Backtracking
Java Software Structures: John Lewis & Joseph Chase
Algorithm Design and Analysis (ADA)
Recursion Copyright (c) Pearson All rights reserved.
Chapter 12 Recursion (methods calling themselves)
מבוא למדעי המחשב, בן גוריון
Analysis and design of algorithm
The Power of Calling a Method from Itself
Recursion.
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
adapted from Recursive Backtracking by Mike Scott, UT Austin
Recursion James Brucker.
Algorithms: Design and Analysis
CSE 143 Lecture 18 More Recursive Backtracking
CSC 172 DATA STRUCTURES.
CSE 143 Lecture 18 More Recursive Backtracking
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
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"?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Principles of Object Oriented Programming
Presentation transcript:

1 Tirgul 11: Recursion & Backtracking

2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it. A simplifying step that gets us a bit closer to the base case.

3 Mutual Recursion A recursion can be more complex that just a function that calls itself. We can have several functions that call each other. Example: Method A calls method B Method B calls method A.

4 Odd or Even? public static boolean odd(int n){ if(n==1){ return true; } return even(n-1); } public static boolean even(int n){ if(n==1){ return false; } return odd(n-1); }

5 Backtracking

6 We can use recursion to go over many options, and do something for each case. Example: printing all subsets of the set {0, …,n-1}. Difficult to do with loops (but possible). Much simpler with recursion.

The basic idea The recursive decomposition: Print all subsets that an item, Then print all the subsets that do not contain it. Keep track of our current “state”. items that are in the set, items not in the set, items we did not decide about. 7

8 PowerSet public class PowerSet { private boolean[] _set; public PowerSet(){} private void printCurrentSet() { System.out.print("{ "); for(int i=0; i<_set.length; i++){ if(_set[i]) System.out.print(i + " "); } System.out.println("}"); }... Holds the set we are currently building. A default constructor that does nothing. Prints the current set

9 PowerSet (cont.) public void printPowerSet(int n){ _set = new boolean[n]; powerSetHelper(0); _set = null; //free memory. } private void powerSetHelper(int index) { if(index==_set.length){ printCurrentSet(); return; } _set[index]=true; powerSetHelper(index+1); _set[index]=false; powerSetHelper(index+1); } This is the not the recursive method. It calls the recursive method that does the real work. Base case: we picked out all the items in the set – print it. Print all sets that include index Print all sets that exclude index

10 PowerSet and the stack ??? ??t ?tt?ft tttftttftfft index=0 index=1 index=2 {0,1,2} {0,1} {0,2}{0}

11 PowerSet and the stack ??? ??f??t ?tt?ft tttftttftfft ?tf?ff ttfftftfffff {0,1,2} {0,1} {0,2}{0} {1,2}{1} {2} {}

Basics of Backtracking A backtracking alg. can be used to find a solution (or all solutions) to a combinatorial problem. Solutions are constructed incrementally At if there are several options to advance incrementally, the algorithm will try one option, then backtrack and try more options. 12

13 N-Queens The problem: On an NxN board, place N queens so that no queen threatens the other (no other queen allowed in same row,col or diagonal). Print only one such board. Simplifying step: Place 1 queen somewhere in an available column then solve the problem of placing all other queens. Base case: All queens have been placed.

14 The N-Queen Problem public class NQueens { private boolean[][] _board; public NQueens(){} private boolean illegalPlacement(int row, int col){…} public void placeQueens(int boardSize){ _board = new boolean[boardSize][boardSize]; if(placeQueenAtCol(0)){ printBoard(); } else{ System.out.println("No Placement Found!"); } _board = null; }... This method uses a recursive helper method that really does the work to check if queen can be at row,col:

15 The N-Queen Problem private boolean placeQueenAtCol(int col) { if(col == _board[0].length){ return true; } for(int row=0; row<_board.length; row++){ if(illegalPlacement(row,col)){ continue; } _board[row][col]=true; if(placeQueenAtCol(col+1)){ return true; } _board[row][col]=false; } return false; } Base case: we have passed the last column. Iterate over rows until it is okay to place a queen. Place the queen Check if we can fill up the remaining columns If not, remove the queen and keep iterating. If no placement works, give up.

16 The N-Queen Problem private boolean illegalPlacement(int row, int col) { for(int delta = 1; delta<=col; delta++){ if(_board[row][col-delta] || (row-delta>=0 && _board[row-delta][col-delta]) || (row+delta<_board.length && _board[row+delta][col-delta])){ return true; } return false; } Note: it is enough to look for threatening queens in lower columns Check for queen in the same row Check for queen in upper diagonal Check for queen in lower diagonal

17 Output of N-Queens Q Q Q Q - Q Q Q Q

18 Traveling Knight Goal: on an NxN board, try to move a knight so it visits every square exactly once. Print all possible ways to manage this. Example of output for one path starting at top left corner:

19 Traveling Knight public class TravelingKnight { private static final int[][] KNIGHT_MOVES = {{1,2},{2,1},{-1,2},{-2,1}, {-1,-2},{-2,-1},{1,-2},{2,-1}}; private int[][] _board; public TravelingKnight(){} public void travel(int boardSize, int row, int col){ _board = new int[boardSize][boardSize]; travelHelper(row,col,1); }... All possible moves The board An interface method that calls the private recursive one.

20 Traveling Knight private void travelHelper(int row, int col, int stepNum){ _board[row][col] = stepNum; if(stepNum==_board.length*_board[0].length){ printBoard(); } else{ for(int i=0; i<KNIGHT_MOVES.length; i++){ int newRow = row+KNIGHT_MOVES[i][0]; int newCol = col+KNIGHT_MOVES[i][1]; if(canGo(newRow,newCol)) travelHelper(newRow, newCol,stepNum+1); } _board[row][col] = 0; } Do the move into this square Base case: This is the last move Simplifying step: try each possible move. Backtrack -- Cancel the move

21 Traveling Knight private boolean canGo(int row, int col) { return (row>=0 && row<_board.length && col>=0 && col<_board[row].length) && _board[row][col]==0; } Simply check if the square is in bounds and that we did not step in it already.

22 All Permutations Goal: Print all permutations of the numbers 1 … n Simplifying step? Base Case?

23 All Permutations private static void printArray(int[] array) {…} private static void swap(int[] array, int i, int j) {…} public static void allPermutations(int n){ int[] array = new int[n]; for(int i=0; i<n; i++){ array[i]=i+1; } permutationHelper(array,0); } An interface method. Inits the array and calls the recursive method.

24 All Permutations private static void permutationHelper(int[] array, int ind) { if(ind==array.length){ printArray(array); return; } for(int i=ind; i<array.length; i++){ swap(array,ind,i); permutationHelper(array, ind+1); swap(array,ind,i); } Base Case: we selected the order of all elements in the array. Print it. Try out all options for placement in current index. Note that when we backtrack we restore the array to its previous form

25 The output:

Latin Squares An NxN Latin square is a square that contains the number 1…N exactly once in each row and column. They are a primitive version of Sudoku puzzles. Example: Our task: to print all Latin squares of size N

27 public static void allLatinSquares(int size){ int[][] board = new int[size][size]; allSquaresHelper(board,0,0); } private static void allSquaresHelper(int[][] board, int row, int col){ if(row==board.length && col==board.length-1){ printBoard(board); return; } if(row == board.length){ row = 0; col++; } for(int symbol=1; symbol<=board.length; symbol++) if(canPut(board,row,col,symbol)){ board[row][col] = symbol; allSquaresHelper(board,row+1,col); } }

28 private static boolean canPut(int[][] board, int row, int col, int symbol){ for(int i=0; i<row; i++) if(board[i][col]==symbol) return false; for(int i=0; i<col; i++) if(board[row][i]==symbol) return false; return true; }