Backtracking Technique Eg. Big Castle – Large Rooms & “Sleeping Beauty” • Systematic search - BFS, DFS • Many paths led to nothing but “dead-ends” • Can we avoid these unnecessary labor? (bounding function or promising function) • DFS with bounding function (promising function) • Worst-Case : Exhaustive Search
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Backtracking Technique
Eg. Big Castle – Large Rooms & “Sleeping Beauty”
• Systematic search - BFS, DFS• Many paths led to nothing but “dead-ends”
• Can we avoid these unnecessary labor? (bounding function or promising function)
• DFS with bounding function (promising function)• Worst-Case : Exhaustive Search
Backtracking Technique
• Useful because it is efficient for “many” large instances
• NP-Complete problems
Eg. 0-1 knapsack problem D.P. : O(min(2n, nW)) Backtracking : can be very efficient if good bounding function(promising function)
• Recursion
Outline of Backtracking Approach
• Backtracking : DFS of a tree except that nodes are visited if promising
• DFS(Depth First Search)
• Procedure d_f_s_tree(v: node)var u : node{ visit v; // some action // for each child u of v do d_f_s_tree(u);}
1
2
3 5 7 114
6
8 9 10
N-Queens Problem
N-Queens Problem
• nⅹn Chess board• n-Queens Eg. 8-Queens problem
, ,)(2
2
n
nn n ,! , nnn
QQ
QQ
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
N-Queens Problem: DFS
• State Space Tree for 4-Queens Problem
N-Queens Problem: DFS (Same Col/Row x)
• State Space Tree for 4-Queens Problem
X1=1
X2=2 3 4
43
4 3 4 2 3 2 3 2
2 4 32 3 4 1 4 1 3 2 4
1 3 4 1 2 4 321
2 3 4
(x1, x2, x3, x4)=(2, 4, 1, 3)
• #leaf nodes : n!=4!• DFS : “Backtrack” at dead ends – 4! leaf nodes (n!)Cf. Backtracking : “Backtrack” if non-promising
(pruning)
N-Queens Problem: Backtracking
• Promising Function(Bounding Function) (i) same row ⅹ (ii) same column ⅹ (col(i) ≠ col(k)) (iii) same diagonal ⅹ (|col(i)-col(k)|≠|i-k|)
Q (i,
col(i)) Q (k,
col(k))
Q (i,col(i))
Q (k,col(k))
N-Queens Problem: Backtracking
• State Space Tree for 4-Queens Problem
N-Queens Problem
• Better (faster) AlgorithmMonte Carlo Algorithm“place almost all queens randomly, then do remaining queens using backtracking.”
#Nodes CheckedDFS (nn)
#Nodes CheckedDFS
(same col/row X)(n!)
#Nodes CheckedBacktracking
34119,173,9619.73ⅹ1012
1.20ⅹ1016
2440,320
4.79ⅹ108
8.72ⅹ1010
6115,721
1.01ⅹ107
3.78ⅹ108
48
1214
n
Graph Coloring
• M-coloring problem:Color undirected graph with ≤m colors2 adjacent vertices : different colors
(Eg)V1 V2
V4 V3
2-coloring X
3-coloring O
Graph Coloring
• State-Space Tree : mn leaves• Promising function : check adjacent vertices for the same color
m
start
1 2 3
21 3
21 3
21 3
X
X
X
X X
Hamiltonian Circuits Problem
• TSP problem in chapter 3.– Brute-force: n!, (n-1)!– D.P. : – When n=20, B.F. 3,800 years D.P. 45 sec.– More efficient solution? See chapter 6
• Given an undirected or directed graph, find a tour(HC). (need not be S.P.)