 # Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.

## Presentation on theme: "Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution."— Presentation transcript:

Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution component by component, starting from null. –At each stage of the search, if an extension of the current partial solution is not possible, we “backtrack” to a shorter partial solution and try again with a different choice for extension. Branch-and Bound: A special case of backtrack with costs. Key point: Most problems have special structures, so we don’t end up trying all possible solutions.

Backtrack Set up a 1-1 correspondence between configurations and possible solution sequences (or partial solution vectors). Decision Tree: The root corresponds to the initial state of the problem (usually is null, means no decision is made), and each branch corresponds to a decision concerning one parameter.

Backtrack Example: 3-coloring with R/G/B 1 4 3 2 5 5R 1B,2G 3R 4B 4G4R 3B 4G A graph is legally colored with k colors if each vertex has a color (label) between 1 and k (inclusive), and no adjacent vertices have the same color. Example: colors are R, G, B. Question: Does the ordering of the vertices (1, 2, 3, 4, 5) matter? B G B G R

Backtrack Example: Non-Attacking Queens Put N queens on an N x N chessboard such that no queen attacks another queen A queen in chess can attack any square on the same row, column, or diagonal. Given an n x n chessboard, we seek to place n queens onto squares of the chessboard, such that no queen attacks another queen. The example shows a placement (red squares) of four mutually non- attacking queens.

Backtrack Example: Non-Attacking Queens 1 3441 432 4 1 12 32 2 3

“Domino” Phenomenon Suppose the solution is represented by a vector. Then a partial solution could be represented by a partial vector. Once we find a partial vector that does not satisfy the solution requirements, there is no point in extending the partial vector into a more complete solution Domino phenomenon: P k false  P k+1 false Note: Domino phenomenon must be true for backtrack to work.

Estimating Backtrack Efficiency Backtrack is usually exponential in the worst case but performs much better on average. One way to estimate: count #nodes in decision tree (unrealistic) –#nodes for the 4-queens problem is 1+ 4 + 4*4 + 4*4*4 + 4*4*4*4 = 341 Alternative way: only count #nodes of feasible choices. But, we don’t know the #feasible choices at each node in advance

Estimating Backtrack Efficiency: Monte Carlo To estimate the number of nodes in a decision tree, can use the Monte Carlo approach. Monte Carlo approach: –Traverse the decision tree, selecting among feasible choices randomly at each node –When a computation path is completed, assume that all the computation paths (those we did not travel) are exactly the same as the one path we chose randomly.

Monte Carlo Example: Case 1 To illustrate the Monte Carlo approach, again use the 4*4 chessboard. Case 1: Assume that X 1 is chosen randomly to be 1 from 4 possible values (1, 2, 3, 4), and X 2 is chosen randomly to be 4 from two possible values (3, 4), then X 3 is set to be 2 since it is the only choice. So, what will the decision tree look like according to the Monte Carlo approach?

Monte Carlo Example: Case 1 X 1 =1 X 2 =4 X 3 =2 Thus, we have 1 + 4 + 4*2 + 4*2 = 21 nodes

Monte Carlo Example: Case 2 Case 2: X 1 =1, X 2 =3 X 1 =1 X 2 =3 We would conclude that there are 13 nodes.

Monte Carlo Example: Case 3 Case 3: X 1 =2, X 2 =4, X 3 =1, X 4 =3 X 2 =4 X 1 =2 X 3 =1 X 4 =3 We could imagine the decision tree has 17 nodes.

Monte Carlo Example The real decision tree 1 3441 432 4 1 12 32 2 3

Monte Carlo Example The real decision tree has 17 nodes. Suppose we got Case 1 twice, Case 2 once, and Case 3 once. Then we could have estimated the #nodes in decision tree to be (2*21 + 1*13 + 1*17) / 4 = 18 This is actually close to the real answer of 17 nodes

Branch-and-Bound (B&B) Special variation of backtrack –Associate a cost with a partial solution, such that the cost of a parent is always less than or equal to the cost of its child in the decision tree –do not branch from an internal node whose cost is higher than the current bound = cost of the minimum- cost complete solution found so far –the bound is updated if a better solution is found Key points –Used for optimization problems –Cost-driven –Bounding prunes the decision tree, saves time

Branch-and-Bound Example: Game Tree In games (e.g., chess) can model the different stages of the game by a rooted tree –Don’t need to consider all possible situations of a game –Can predict the outcome of the game using the concept of branch-and-bound Questions –Who is the winner if both players play optimally ? –How much is the payoff? (Initially, we only know the payoff of the terminal nodes (end-states) of the game.)

Game Tree Example – “NIM” NIM: –Two players alternately take chips from a given pile –Each player must take at least one chip, and at most three chips in his turn –The winner is the player who empties the pile –The amount of payoff is the number of chips the winner takes in his last turn Way to think about it: payoff = amount the 1 st player receives at the end of game 1 st player wants to maximize payoff (Call her Max) 2 nd player wants to minimize payoff (Call her Min)

Game Tree Example Basic Idea Use both upper and lower cut-off values in a game tree TU  Q RS  If the value of a Max son is , then  is a lower cut-off value on the value of Max If Max has a lower cut-off value , then all Max’s children have lower cut-off value 

Game Tree Example If the value of a Min son is , then  is a upper cut-off value on the value of Min. If Min has a upper cut-off value , then all Min’s children have upper cut-off value .  Q RS            TU

Game Tree Example (before pruning) 2 -2 21876 -3 543 1 1 2 -4 4 -3 6 -2-5 14 -3 6 1 1 A B C DE F G H K J I M LN

Game Tree Example (after pruning) 2 -2 2131 112 -4-2 12 1 A B C DE F G H K J I M LN 11 11 11 11 11 11 11 11 11 11 11 11 -2 22

SPARE SLIDES

Backtrack example: 3-coloring Algorithm 3-coloring (G, var U); Input: G = (V,E) (an undirected graph). Let U = set of vertices that have already been colored together with their colors. U is initially empty) Output: An assignment of one of three colors to each vertex of G Begin if U = V then print “coloring is completed”; return; else pick a vertex v not in U; for C := 1 to 3 do if no neighbor of v is colored with color C then add v to U with color C; 3-coloring(G,U) end

Download ppt "Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution."

Similar presentations