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.

Slides:



Advertisements
Similar presentations
Traveling Salesperson Problem
Advertisements

1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
BackTracking Algorithms
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Branch & Bound Algorithms
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
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.
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?
Sum of Subsets and Knapsack
Backtracking.
MAE 552 – Heuristic Optimization Lecture 27 April 3, 2002
CHAPTER 4 Searching. Algorithm Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found,
24-Jun-15 Pruning. 2 Exponential growth How many leaves are there in a complete binary tree of depth N? This is easy to demonstrate: Count “going left”
Search.
Dr. Jouhaina Chaouachi Siala
CP Summer School Modelling for Constraint Programming Barbara Smith 1.Definitions, Viewpoints, Constraints 2.Implied Constraints, Optimization,
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
Bill Payment Optimization Algorithms. Purpose To find and/or construct algorithms that will optimize the decision process of paying bills from an account.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
CS 312: Algorithm Analysis Lecture #32: Intro. to State-Space Search This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
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.
Algorithmics - Lecture 131 LECTURE 13: Backtracking.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Chapter 5 Constraint Satisfaction Problems
Hard Problems Sanghyun Park Fall 2002 CSE, POSTECH.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
N-queens problem Original problem: How to place 8 queens on an 8x8 chessboard so that no two queens attack each other N-queen problem: Generalization for.
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
Constraint Propagation Artificial Intelligence CMSC January 22, 2002.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
Linear Programming Chap 2. The Geometry of LP  In the text, polyhedron is defined as P = { x  R n : Ax  b }. So some of our earlier results should.
1 Hojjat Ghaderi [Courtesy of Fahiem Bacchus], University of Toronto, Fall 2006 CSC384: Intro to Artificial Intelligence Backtracking Search I ● Announcements.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSG3F3/ Desain dan Analisis Algoritma
Problem Solving: Brute Force Approaches
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
CSCI 104 Backtracking Search
The minimum cost flow problem
Backtracking And Branch And Bound
Data Structures and Algorithms
BACKTRACKING- GENERAL METHOD
Instructor: Vincent Conitzer
Design and Analysis of Algorithm
Search
CS 188: Artificial Intelligence
Exact Solutions to NP-Complete Problems
Problem Solving: Brute Force Approaches
Back Tracking.
Analysis and design of algorithm
Branch and Bound.
Chapter 3: Finite Constraint Domains
A General Backtracking Algorithm
Exercise: Dice roll sum
Pruning 24-Feb-19.
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
ICS 353: Design and Analysis of Algorithms
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Lecture 4: Tree Search Strategies
Presentation transcript:

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 at an optimum or solution. A solution can be written in the form of a vector X = (x 1,x 2,..,x n ) and the search is directed by criterion P, defining feasible choices of X. Often P can also decide whether a partial solution X i =(x 1,..,x i ), i  n, is feasible, ie, there are extensions of X i to a feasible solution. We can either search for one solution, or we can search for all solutions.

Constraints Explicit constraints define the domain of the choice variables, eg x i >0, or x i =0 or 1, or l i <x i <u i. Implicit constraints specify that x 1.. x i satisfy P, ie relate the x i -s to each other. The explicit constraints define a possible solution space for the problem The implicit constraints narrow the solution space down to an answer space.

Search Spaces We concern us here with tree shaped spaces A prefix x 1.. x i is a path from the root (the empty vector) with edges labeled x 1 to x i Children of x i form solution vectors of the form x 1.. x i+1.

n Queens Given an nxn chessboard and n queens, find all placements of the queens such that none of them attack each other – none of the queens occupy the same row, column, or diagonal. As each queen occupies her own row, a (partial) choice vector X is a vector of integers from 1 to n, where x j is the column for the queen in row j. One solution to the 4-queens problem is (2,4,1,3)

Q 4 queens solution

Solution space for n-Queens root has n children, 1 for each column each of these has n-1 children, excluding its own column next has n-2 children etc. so the tree has n! nodes / states, many of these are infeasible – diagonal constraint n! is a gross overestimate of the number of states we need to check

Subset sum (SSS) Given a set of integers m 1 to m n and another integer M, find all subsets of m i -s such that their sum is M Eg for m ={1,2,3}, M=3 we have two solutions {1,2} and {3}.

Solution Vectors for SSS One option: the indices of the chosen elements then solution vectors have different length

Solution Vectors for SSS One option: the indices of the chosen elements then solution vectors have different length X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3

Alternative Another, more regular, solution space has nodes with up to n choice values c i = 0 if m i is not in the subset and c i = 1 if it is. In this case the leaves represent the subsets.

Alternative Another, more regular, solution space has nodes with up to n choice values c i = 0 if m i is not in the subset and c i = 1 if it is. In this case the leaves represent the subsets. C 1 = 0 C 1 = 1 C 2 = 1 C 2 = 0 C 3 = 0 C 3 = 1

Backtrack Backtrack walks depth first through the solution space and uses the criterion P (sometimes called bounding function B) to prune the solution space to create the answer space.

Iterative Backtrack We are searching for all possible answers. X will hold the (partial) solution vector (x 1,x 2,..,x i ). B is the bounding function. Next(X) is the set of valid next states x i+1 from state x i, and therefore B i+1 (X[1..(i+1)) is true for all states in Next(X) and false for all other states. We also need a function that decides whether a state is an answer state.

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set }

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=1 x=[] cap = 3

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=2 x=[1] feasible cap=2

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=3 x=[1,2] answer path cap=0 print X=[1,2]

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=3 x=[1,2,3] infeasible backtrack

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=2 X=[1,3] infeasible backtrack

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=1 X=[2] feasible

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=2 X=[2,3] infeasible backtrack

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=1 X=[3] feasible

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 k=2 X=[3] cap=0 path to answer node print X=[3]

IBackTrack(n){ k = 1 while (k>0) { if (there remains an x in Next(X[1..(k-1)]) and B k (X[1..k]) ) { if (X[1..k] is path to answer node) print X[1..k] // end if k++ // consider next set } else k-- // backtrack to previous set } X 1 =1 X 1 =3 X 2 =2 X 1 =2 X 2 =3 X 3 =3 paths to answer node X=[1,2] X=[3]

Recursive back track global variable X[1:n], initial call RBackTrack(1) RBackTrack(k) { for each X[k] with B k (X[1..k])==true if (X[1..k] is a path to an answer state) print(X[1..k] RBackTrack(k+1) }

n-queens X[1:k] is a partial solution with X[i] the column number (1..n) of a queen in row i 1:k. When we place queen k+1 we need to check that she does not occupy any of the already occupied columns (that's easy) and any of the already occupied diagonals: – two queens in (i,j) (row i, column j) and (p,q) occupy the same diagonal if (i-j) == (p-q) or (i+j)==(p+q), which is equivalent with |i-p|==|j- q|.

iterative n-queens with global partial solution X Place(k) for (i = 1 to k-1) if (X[i]==X[k] or // same columns | X[i]-X[k] | == |i-k| ) // same diagonal return(false); // end for return(true)

nQueens(n) { X[1] = 0 //place first queen "in front of" row 1 k=1 while (k>0) { X[k]++ // move queen k to next column while (X[k]<=n and not(Place(k)) { X[k]++ } if (X[k] <= n) // found valid spot for queen k if (k==n) print(X) else {k++; X[k]=0} // start next row else k-- // backtrack }

Sum of Subsets Two ways to represent the search space for subset sum. Here we consider the regular case with choices x i =0 (do not take object i) and x i =1 (take object i) at level i. A choice for the bounding function B k =

Simplifying / Strengthening assumption for SSS From hereon we assume that the objects are sorted in non-decreasing order, because this allows us to strengthen our observations.

Weight gathered so far In some state X[1..k] at level k the total weight gathered by objects 1 to k is either M, and we have found a solution and thus we don't need to go deeper in that state... or the weight gathered is less than M, and then X[1..k] cannot lead to an answer node if

Weight gathered so far In some state X[1..k] at level k the total weight gathered by objects 1 to k is either M, and we have found a solution and thus we don't need to go deeper in that state... or the weight gathered is less than M, and then X[1..k] cannot lead to an answer node if WHY?

A better bound Original bound plus previous observation leads to a better bound: not too little and not too much

Recursive SSS s is the weight gathered by the objects chosen so far, and thus needs to be initialized at 0 r is the total weight of the remaining objects, and thus needs to be initialized at the sum SumW of all the weights. k is the current 1-based level. If we take object k we can simplify the bound test because the sum of the weights of the chosen objects (now including k) plus remaining objects has not changed and is still >= M The initial call is SumOfSub(0,1,SumW).

No degenerate problems We assume that W[1] =M, otherwise we have a degenerate problem.

SumOfSub(s,k,r) { // two possible next states X[k] = 1 //1: take object k, we can: B k-1 = true if (s+W[k]==M) print X[1..k] // subset found else (if s+W[k]+W[k+1]<=M) // B k = true SumOfSub(s+W[k],k+1,r-W[k]) // do not take object k, // now we have to check whether the rest // of the objects still can reach M and // the lightest remaining object is not too heavy if (s + r - W[k] >= M // still can reach M and s+W[k+1] <=M // rest objects not too heavy ) SumOfSub(s,k+1,r-W[k]) }

SumOfSub(s,k,r) { // two possible next states X[k] = 1 //1: take object k, we can: B k-1 = true if (s+W[k]==M) print X[1..k] // subset found else (if s+W[k]+W[k+1]<=M) // B k = true SumOfSub(s+W[k],k+1,r-W[k]) // do not take object k, // now we have to check whether the rest // of the objects still can reach M and // the lightest remaining object is not too heavy if (s + r - W[k] >= M // still can reach M and s+W[k+1] <=M // rest objects not too heavy ) SumOfSub(s,k+1,r-W[k]) } Notice that the algorithm does not check k<=n. This is implicit in the fact that when SumOfSub is called s M, hence r>0 and thus k<=n.