Algorithm Design and Analysis (ADA)

Slides:



Advertisements
Similar presentations
1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
Advertisements

Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
1 Programming Interest Group Tutorial Six Divide and Conquer and Backtracking.
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?
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
Backtracking.
L6: Searching Program in Java Breadth-first search vs depth-first search.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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"?
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Clue Paths. Memory Alias Review Remember that it’s possible for two unique variables to point to the same memory location myList MyClass mc 0x100 someFn.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
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 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Edit from old slide since 2000 P. Chongstitvatana 26 Nov 2010
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Depth-First Search N-Queens Problem Hamiltonian Circuits
CS100J Lecture 19 Previous Lecture This Lecture
Chapter 5 Ordered List.
CSCI 104 Backtracking Search
Data Structures and Algorithms
Chapter 5: Control Structures II
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
Recursion Copyright (c) Pearson All rights reserved.
Problem Solving: Brute Force Approaches
Chapter 5 Ordered List.
CSC 113 Tutorial QUIZ I.
Back Tracking.
Analysis and design of algorithm
An Introduction to Java – Part I, language basics
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Building Java Programs
adapted from Recursive Backtracking by Mike Scott, UT Austin
Building Java Programs
A General Backtracking Algorithm
Prabhas Chongstitvatana
Recursion James Brucker.
Algorithms: Design and Analysis
Algorithms: Design and Analysis
CSE 143 Lecture 18 More Recursive Backtracking
Building Java Programs
CSE 143 Lecture 18 More Recursive Backtracking
Recursive backtracking
Tree.
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"?
22C:21 Problem 2.3 Solution Outline.
Objects with ArrayLists as Attributes
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Backtracking.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Algorithm Design and Analysis (ADA) 242-535, Semester 1 2017-2018 3. Backtracking Objectives explain backtracking search, and give three examples (all subsets, permutations, and 8-queens)

1. Backtracking Features Search down a data structure to the 'end' e.g. search a binary tree The search involves choices about which path to take e.g. go to left or right child? let's go left! At 'end', go back to last choice point, and try a different choice. e.g. go back to parent node, let's go right! Stop when back at start, and no more choices.

Search Space The problem may have many answers, spread across a very large “search space” Brute force strategy: look at all the answers in the search space to find the best one(s) Today’s CPUs can handle search spaces containing billions of answers (about 109) In some problems, we can stop as soon as one answer is found – fast but answer may not be the best

Calculating the Search Space Size Search space size is related to the number of variables in the problem, and their values e.g., 20 variables, each one 0 or 1; there are 220 different solutions, or about 106 e.g., 12 variables, each one 0, 1, 2, …, or 9; there are1012 different solutions e.g., 12 variables, holding the permutation of 1, 2, 3, …, 12; there are 12! = 479,001,600 different solutions

Backtracking Pseudo-code in Words Assume the problem has n variables, stored in an array (v0, v1, …, vn-1) Pseudo-code for search(): Look at k th variable in (v0, v1, …, vk, …, vn-1): if k == n, then all the variables have been assigned, so check if the array is a good solution. The program can exit at this point or this search() call can return so backtracking can look for other good answers. else for each possible value of vk: (c0, c1, …, cj) Assign a value c to vk, and then search for k+1 th variable value in (v1, v2, …, (vk = c), vk+1, …, vn-1)

Backtracking Pseudo-code private static void search(Data[] vars, int k) { if (vars.length == k) // looked at all vars? reportSoln(vars); // returning after the report will continue the search else { // process kth var Data[] candidates = getCandidates(vars, k); for (Data c : candidates) { // for all values of kth var vars[k] = c; search(vars, k+1); } } // end of search()

Search Space as a Tree v0 v1 v2 . . . . . . . . . . . . . . . E.g., vars = { v0, v1, v2 } can have values 0, 1, or 2 search one solution == one path v0 1 2 choice point v1 1 2 1 2 1 2 v2 . . . . . . . . . . . . . . . 1 2 1 2 1 2 exit or use backtracking to try alternative paths

2. Constructing All Subsets Given a set with n items, we have 2n subsets. How to output all the subsets? E.g., the subsets of {1, 2, 3 } are { 1 2 3 } { 1 2 } { 1 3 } { 1 } { 2 3 } { 2 } { 3 } { } Use three variables, v0, v1, v2. Each of them can be either true or false. vi is true means that i+1 is in the subset.

Search Space as a Tree v0 v1 v2 == {3} == {1,2,3} Use three variables, v0, v1, v2. Each of them can be either true or false. vi is true means that i+1 is in the subset. search one possible solution v0 == {1,2,3} F T == {3} v1 T F T F v2 T F T F T F T F use backtracking to try alternative paths

Constructing All Subsets see AllSubsets.java /* output all the subsets of {1, 2, 3} */ public static void main(String[] args) { boolean[] vars = new boolean[3]; search(vars, 0); } // end of main() > java AllSubsets { 1 2 3 } { 1 2 } { 1 3 } { 1 } { 2 3 } { 2 } { 3 } { } vars[0] represents "1", vars[1] represents 2", etc. vars[i] == true value means i+1 is in the set

private static void search(boolean[] vars, int k) { if (vars private static void search(boolean[] vars, int k) { if (vars.length == k) // looked at all vars? reportSoln(vars); else { // process kth var boolean[] candidates = getCandidates(vars, k); for (boolean c : candidates) { vars[k] = c; search(vars, k+1); } } // end of search() Contest Algorithms: 4. Backtracking

private static boolean[] getCandidates(boolean vars[], int k) { return new boolean[]{ true, false}; } private static void reportSoln(boolean vars[]) { System.out.print(" {"); for(int i = 0; i < vars.length; i++) { if (vars[i]) System.out.print(" " + (i+1)); } System.out.println(" }"); } // end of reportSoln() vars[0] represents "1", vars[1] represents 2", etc. vars[i] == true value means print i+1

3. Constructing All Permutations Permutation of {0, 1, 2} are 012 021 102 120 201 210 Use three variables, v0, v1, v2. Each of them can be assigned 0, 1, or 2 But can only use 0, 1, or 2 once per path

Search Space as a Tree v0 v1 v2 Use three variables, v0, v1, v2. Each of them can be assigned 0, 1, or 2 But can only use 0, 1, or 2 once per path search one solution == one path v0 1 2 v1 1 2 2 1 v2 2 1 2 1 use backtracking to try alternative paths

Constructing All Permutations see Permutations.java public static void main(String[] args) { int[] vars = {-1, -1, -1}; // -1 means 'no value' search(vars, 0); } // end of main() > java Permutations 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 vars[0] represents "0", vars[1] represents 1", etc.

private static void search(int[] vars, int k) { if (vars private static void search(int[] vars, int k) { if (vars.length == k) // looked at all vars? reportSoln(vars); else { // process kth var int[] candidates = getCandidates(vars, k); for (int c : candidates) { vars[k] = c; search(vars, k+1); } } // end of search()

private static boolean[] getCandidates(int vars[], int k) { boolean[] inPerms = new boolean[vars.length]; for(int i = 0; i < vars.length; i++) inPerms[i] = false; for(int i = 0; i < k; i++) if (vars[i] != -1) inPerms[vars[i]] = true; // using vars[i] int n = 0; int[] c = new int[vars.length - k]; if (!inPerms[i]) /* candidates must be */ c[n++] = i; /* different from existing */ /* elements */ return c; } // end of getCandidates()

private static void reportSoln(boolean vars[]) { for(int v: vars) System.out.print(" " + v); System.out.println(); } // end of reportSoln() Contest Algorithms: 4. Backtracking

4. Eight-Queens Problem Put 8 queens on an 8×8 chessboard such that none of them is able to affect any of the others. A solution requires that no two queens share the same row, column, or diagonal. How many solutions? (92)

Eight-Queens Problem If a queen can be placed at any square, the search space is 648. Maybe too large! (648 == 26*8 == 248 ≈ 1012 * 28 = 2.56 x1014) Position constraints help to reduce the size of search space The queen in the first column has 8 choices. Next, the queen in the second column has 7 choices. Next, the queen in the third column has 6 choices. Etc. Using these constraints, the space size is 8! = 40320 only. We can also add diagonal constraints to reduce the size more

Search Space  this path fails     X X     X X X X    X X X this path succeeds  this path fails     X X     X X X X    X X X X  

Eight-Queens Problem see Queens8.java private static int solnsCount = 0; // there are 92 solutions public static void main(String[] args) { int[] qs = new int[8]; search(qs, 0); System.out.println("No. of solutions: " + solnsCount); } // end of main()

private static void search(int[] qs, int k) { if (qs.length == k) // looked at all qs? reportSoln(qs); else { // process kth var for (int i = 0; i < qs.length; i++) { qs[k] = i; if (isConsistent(qs, k)) search(qs, k+1); } } // end of search() replace getCandidates() by loop and isConsistent()

: _ _ _ _ _ _ _ Q _ _ Q _ _ _ _ _ Q _ _ _ _ _ _ _ _ _ _ _ _ Q _ _ _ Q _ _ _ _ _ _ _ _ _ _ Q _ _ _ _ _ _ _ _ _ Q _ _ _ _ Q _ _ _ _ No. of solutions: 92 private static void reportSoln(int qs[]) { for (int i = 0; i < qs.length; i++) { for (int j = 0; j < qs.length; j++) { if (qs[i] == j) System.out.print("Q "); else System.out.print("_ "); } System.out.println(); solnsCount++; } // end of reportSoln()

private static boolean isConsistent(int[] q, int k) /* Return true if queen placement qs[k] does not conflict with other queens qs[0] through qs[k-1] */ { for (int i = 0; i < k; i++) { if (q[i] == q[k]) // same row return false; if ((q[i] - q[k]) == (k - i)) // same major diagonal if ((q[k] - q[i]) == (k - i)) // same minor diagonal } return true; } // end of isConsistent() three position constraints