24
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8-queens). 4. Backtracking 1 Contest Algorithms: 4. Backtracking

Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Embed Size (px)

DESCRIPTION

3 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 2 20 different solutions, or about 10 6  brute force should find an answer within the ACM contest time  e.g., 12 variables, each one 0, 1, 2, …, or 9; there are different solutions  maybe too many for the ACM contest, but try using long  e.g., 12 variables, holding the permutation of 1, 2, 3, …, 12; there are 12! = 479,001,600 different solutions  brute force may be fast enough

Citation preview

Page 1: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

1

Contest AlgorithmsJanuary 2016

Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8-queens).

4. Backtracking

Contest Algorithms: 4. Backtracking

Page 2: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

2

1. Backtracking The problem has a large number of different possible

answers, called a “search space”

Brute force strategy: check all possible answers to find the best one(s)

Today’s CPUs can handle problems with search spaces containing billions of answers (about 109).

Page 3: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

3

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

brute force should find an answer within the ACM contest time

e.g., 12 variables, each one 0, 1, 2, …, or 9; there are1012 different solutions

maybe too many for the ACM contest, but try using long

e.g., 12 variables, holding the permutation of 1, 2, 3, …, 12; there are 12! = 479,001,600 different solutions

brute force may be fast enough

Page 4: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

4

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)

Page 5: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

5

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()

Page 6: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

6

E.g., vars = { v0, v1, v2 } can have values 0, 1, or 2Search Space as a Tree

v0 0 1 2

0 1 2 0 1 2 0 1 2

search

. . . . . . . . . . . . . . .

v1

v2 0 1 2 0 1 2 0 1 2

one solution == one path

choice point

exit or use backtracking to try alternative paths

Page 7: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

7

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.

Page 8: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

8

Search Space as a Tree

v0 T

T F

search

v1

v2 T F T F

one possible solution

use backtracking to try alternative paths

F

T F

T F T F

== {1,2,3}

== {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.

Page 9: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

9

Constructing All Subsets /* 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()

vars[0] represents "1", vars[1] represents 2", etc.

vars[i] == true value means i+1 is in the set

> java AllSubsets { 1 2 3 } { 1 2 } { 1 3 } { 1 } { 2 3 } { 2 } { 3 } { }

see AllSubsets.java

Page 10: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

10

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()

Page 11: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

11

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

Page 12: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

12

3. Constructing All Permutations Permutation of {0, 1, 2} are

012021102120201210

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

Page 13: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

13

Search Space as a Tree

v0 0 1 2

1 2 0 2 0 1

search

v1

v2 2 1

one solution == one path

use backtracking to try alternative paths

2 10 0

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

Page 14: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

14

Constructing All Permutations public static void main(String[] args) { int[] vars = {-1, -1, -1}; // -1 means 'no value' search(vars, 0); } // end of main()

vars[0] represents "0", vars[1] represents 1", etc.

> java Permutations 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0

see Permutations.java

Page 15: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

15

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()

Page 16: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

16

private static boolean[] getCandidates(boolean 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]; for(int i = 0; i < vars.length; i++) if (!inPerms[i]) /* candidates must be */ c[n++] = i; /* different from existing */ /* elements */ return c; } // end of getCandidates()

Page 17: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

17

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

Page 18: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

18

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)

Page 19: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

19

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

Page 20: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

20

Search Space

XX

X X X X

X X X X

this path fails

this path succeeds

Page 21: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

21

Eight-Queens Problem 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()

see Queens8.java

Page 22: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

22

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()

Page 23: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

23

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(); } System.out.println(); solnsCount++; } // end of reportSoln()

:_ _ _ _ _ _ _ Q_ _ Q _ _ _ _ _Q _ _ _ _ _ _ __ _ _ _ _ Q _ __ Q _ _ _ _ _ __ _ _ _ Q _ _ __ _ _ _ _ _ Q __ _ _ Q _ _ _ _

_ _ _ _ _ _ _ Q_ _ _ Q _ _ _ _Q _ _ _ _ _ _ __ _ Q _ _ _ _ __ _ _ _ _ Q _ __ Q _ _ _ _ _ __ _ _ _ _ _ Q __ _ _ _ Q _ _ _

No. of solutions: 92

Page 24: Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest

Contest Algorithms: 4. Backtracking

24

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 column return false; if ((q[i] - q[k]) == (k - i)) // same major diagonal return false; if ((q[k] - q[i]) == (k - i)) // same minor diagonal return false; } return true; } // end of isConsistent()

threepositionconstraints