Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Tree Search Strategies

Similar presentations


Presentation on theme: "Lecture 4: Tree Search Strategies"— Presentation transcript:

1 Lecture 4: Tree Search Strategies
主講人:虞台文

2 Content Introduction Depth-First Search Breadth-First Search
Heuristic Searches Best-First Search/Hill Climbing A* algorithm Branch-and-Bound

3 Lecture 4: Tree Search Strategies
Introduction

4 Introduction The procedure of solving many problems may be represented by trees. Therefore the solving of these problems becomes a tree searching problem.

5 Example: n-Queen 1 2 3 4 5 6 7 8 x1 = 1 x2 = 5 x3 = 8 x4 = 6 x5 = 3

6 n = 4 Example: n-Queen x1 = 1 2 3 4 x2 = 2 3 4 1 3 4 1 2 4 1 2 3

7 n = 4 Example: n-Queen The size of search space is x!
2 3 4 How to search effectively? x2 = 2 3 4 1 3 4 1 2 4 1 2 3 x3 = 3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2 x4 = 4 3 4 2 3 2 4 3 4 1 1 4 4 1 2 1 3 2 3 1 2 1 3 2

8 Some Search Schemes Breadth-First Search Depth-First Search
Hill Climbing Best-First Search Branch-and-Bound Strategy for optimization problems Blind Search Guide with Heuristics

9 Nodes of Search Tree Live Node Dead Node Expanded Node
a node ready for being expanded Dead Node a node has been expanded or can not be expanded further Expanded Node a node that is currently selected for expansion

10 Example: Depth-First Search
2 3 4 x2 = 2 3 4 1 3 4 1 2 4 1 2 3 2 4 2 3 1 3 2 4 2 3 1 3 3 3 2 2

11 Example: Breadth-First Search
2 3 4 x2 = 2 3 4 1 3 4 1 2 4 1 2 3 2 4 2 3 1 3 2 4 2 3 1 3 3 3 2 2

12 Search Space S0: Initial State Solution Space Search Space
Solution Path Sg: Goal State

13 Major Components in Tree Search
Tree-Node Generating Mechanism List: Tree-Node Maintaining Mechanism Stack, Queue, Priority Queue Tree-Node Selection Mechanism Branching Answer Verification Mechanism Bounding Mechanism Pruning Cost Estimation Function Heuristic

14 Lecture 4: Tree Search Strategies
Depth-First Search

15 Depth-First Search Scheme
Step1: Form a stack consisting of the root node. Step2: Test to see if the top element in the stack is a goal node. If it is, stop. Otherwise, go to step 3. Step3: Remove the top element from the stack. Add the first element’s descendants, if any, to the top of the stack. Step4: If the stack is empty, then signal failure. Otherwise, go to Step 2.

16 Example: Sum-of-Subset Problem
Given a set of positive integers, say S={s1, …, sn}, and an positive integer, say M, we want to determine whether there exists a subset, say S’S, whose element sum equals to M. The problem is NP-Complete.

17 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem Solution Space (I) {1} {2} {3} {4} {1,2} {1,3} {1,4} {1,2,3} {1,2,4} {1,3,4} {1,2,3,4} {2,3} {2,4} {2,3,4} x1=1 x1=2 x1=3 x1=4 x2=2 x2=3 x2=4 x3=3 x3=4 x4=4

18 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 9 Solution Space (I) {1} x1=1 7 {1,2} x2=2 {1,3} x2=3 {1,4} x2=4 12 8 9 {1,3,4} x3=4 10

19 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 9 Solution Space (I) {1} {2} {3} {4} {1,2} {1,3} {1,4} {1,2,3} {1,2,4} {1,3,4} {1,2,3,4} {2,3} {2,4} {2,3,4} x1=1 x1=2 x1=3 x1=4 x2=2 x2=3 x2=4 x3=3 x3=4 x4=4

20 Example: Sum-of-Subset Problem
SumSubset_1(S[1..n], M) x[1]1; depth 1; // generate the first tree node loop if (depth =0) return false; // solution space has been exhausted // evaluate the current sum sum  0; for k  1 to depth do sum  sum + x[k]; if (sum = M) return true; // report success if (sum > M or x[depth] = n) // backtracking depth  depth1; if (depth <> 0) x[depth]  x[depth] + 1; else // dig into the tree x[depth+1]  x[depth]+ 1; depth  depth + 1; repeat

21 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem Solution Space (II) x1=1 x1=0 x2=1 x2=0 x3=1 x3=0 x4=1 x4=0

22 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 13 Solution Space (II) 7 12 18 22 16 13 17 11 5 15 9 6 10 4 x1=1 x1=0 x2=1 x2=0 x3=1 x3=0 x4=1 x4=0 x1=1 x1=0 x2=1 x2=0 x3=1 x3=0 x4=1 x4=0 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000

23 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 13 Solution Space (II) x1=1 x1=0 x2=1 x2=0 x3=1 x3=0 x4=1 x4=0 x1=1 x1=0 7 x2=1 x2=0 x2=1 x2=0 12 7 5 18 x3=1 12 16 x3=0 x4=1 1101 x3=1 x3=0 x3=1 x3=0 x3=1 x3=0 13 7 11 5 6 x4=1 x4=0 x4=0 x4=1 x4=0 x4=1 x4=0 x4=1 x4=0 x4=1 x4=0 x4=1 x4=0 x4=1 x4=0 22 18 12 17 13 11 7 15 11 9 5 10 6 4 1111 1110 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000

24 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 13 Solution Space (II) Lazy Version 7 x1=1 12 x2=1 18 x3=1 7 x2=0 13 x3=1 12 16 x3=0 x4=1

25 Example: Sum-of-Subset Problem
n = 4 Example: Sum-of-Subset Problem M = 13 Solution Space (II) Eager Version 7 x1=1 12 x2=1 7 x2=0 12 x3=0 13 x3=1

26 Example: Sum-of-Subset Problem
Lazy Version Example: Sum-of-Subset Problem SumSubset_2(S[1..n], M, X[1..n]) // return true or false. If true, subset is in X for i  1 to n do X[i] 0; // initialization k  1; sum = 0; // start from the root node loop while k  n and sum < M do // extend left as deep as possible x[k]  1; sum  sum + S[k]; k  k + 1; if(sum = M) return true; // answer found k  k  1; // overshooting sum  sum  S[k]; // withdraw the last element x[k]  0; // branch to right subtree if(k = n) // if no more right subtree, backtrack one level while(k > 0 and X[k] = 0) k  k  1; if (k = 0) return false; // tree has been exhausted else X[k]  0; k  k + 1; // search on subtree repeat

27 Example: Sum-of-Subset Problem
Eager Version Example: Sum-of-Subset Problem SumSubset_2(S[1..n], M, X[1..n]) // return true or false. If true, subset is in X for i  1 to n do X[i] 0; // initialization k  1; sum = 0; // start from the root node loop while k  n and sum + S[k]  M do // extend left as deep as possible x[k]  1; sum  sum + S[k]; k  k + 1; if(sum = M) return true; // answer found if(k > n) // if no more right subtree, backtrack one level while(k > 1 and X[k  1] = 0) k  k  1; if (k = 1) return false; // tree has been exhausted k  k + 1; repeat

28 Discussion Sum-of-Subset problem is a constraint satisfaction problem.
Tree pruning by considering feasibility Can we apply DFS scheme to solve optimization problem? Do we need to inspect the costs (or profits) of all feasible solutions to determine the optimal one? How to prune subtrees sure to with inferior feasible solutions only?

29 0-1 Knapsack Problem

30 0-1 Knapsack Problem Fractional Version
Fact: the profit for the optimal solution of a 0-1 knapsack problem cannot greater than that of its fractional version.

31 0-1 Knapsack Problem xk+1 = 1 xk+1 = 0
We can compute the following items for each feasible node: 1. current weight 2. current profit 3. upper bound xk+1 = 1 xk+1 = 0 Fact: a left child has the same upper bound as its root.

32 0-1 Knapsack Problem Weight/Profit Upper Bound xk+1 = 1 xk+1 = 0

33 Example: 0-1 Knapsack Problem
i 1 2 3 4 5 6 7 8 pi 11 21 31 33 43 53 55 65 wi 23 45 W = 110

34 Example: 0-1 Knapsack Problem
i 1 2 3 4 5 6 7 8 pi 11 21 31 33 43 53 55 65 wi 23 45 W = 110 Example: 0-1 Knapsack Problem 164.88 1/11 x1=1 155.11 x1=0 12/32 x2=1 157.44 x2=0 33/63 x3=1 159.76 x3=0 56/96 x4=1 160.22 x4=0 65/35 x4=1 154.88 x4=0 89/139 x5=1 x5=0 162.44 66/106 x5=1 157.55 x5=0 68/108 157.11 164.66 x6=0 109/159 x6=1 159.79 x6=0 99/149 161.63 163.81 x7=0 162 105/151 160.18 158 157.63 A 139 x8=0 B 149 C 151 D 159

35 Example: 0-1 Knapsack Problem
i 1 2 3 4 5 6 7 8 pi 11 21 31 33 43 53 55 65 wi 23 45 W = 110 Example: 0-1 Knapsack Problem 164.88 Fact: the bounds can be integers. 1/11 x1=1 155.11 x1=0 12/32 x2=1 157.44 x2=0 33/63 x3=1 159.76 x3=0 56/96 x4=1 160.22 x4=0 65/35 x4=1 154.88 x4=0 89/139 x5=1 x5=0 162.44 66/106 x5=1 157.55 x5=0 68/108 157.11 164.66 x6=0 109/159 x6=1 159.79 x6=0 99/149 161.63 163.81 x7=0 162 105/151 160.18 158 157.63 A 139 x8=0 B 149 C 151 D 159

36 Example: 0-1 Knapsack Problem
i 1 2 3 4 5 6 7 8 pi 11 21 31 33 43 53 55 65 wi 23 45 W = 110 Example: 0-1 Knapsack Problem 164.88 Fact: the bounds can be integers. 1/11 x1=1 155.11 x1=0 12/32 x2=1 157.44 x2=0 33/63 x3=1 159.76 x3=0 56/96 x4=1 160.22 x4=0 89/139 x5=1 x5=0 162.44 66/106 x5=1 157.55 x5=0 164.66 x6=0 109/159 x6=1 159.79 x6=0 99/149 161.63 163.81 x7=0 162 105/151 160.18 A 139 x8=0 B 149 C 151 D 159

37 Lecture 4: Tree Search Strategies
Breadth-First Search

38 Breadth-First Search Scheme
Step1: Form a queue consisting of the root node. Step2: Test to see if the first element in the queue is a goal node. If it is, stop. Otherwise, go to step 3. Step3: Remove the first element from the queue. Add the first element’s descendants, if any, to the end of the queue. Step4: If the queue is empty, then signal failure. Otherwise, go to Step 2.

39 Example: 8-Puzzle 2 8 3 1 6 4 7 5 1 2 3 8 6 4 7 5 Start Goal

40 Example: 8-Puzzle Blank Cell Movement

41 Example: 8-Puzzle 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 1 7 6 4 5 2 8 3 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5

42 Exercise Accompanying with a bounding function to solve the instance of 0-1 knapsack problem describe previously using the breadth-first search. Give your result graphically and compare it with the depth-first one.

43 Lecture 4: Tree Search Strategies
Heuristic Searches

44 Cost of Subtrees c1 c2 cn c2 cn c1

45 n = 4 Example: n-Queen 4  4  4  4  4 2 x1 = 1 3 4 x2 = 2 1 x3 = 3

46 Assume that we are completely informative, then search is trivial.
Example: n-Queen Assume that we are completely informative, then search is trivial. 4 2 x1 = 1 3 4 x2 = 2 1 x3 = 3 x4 = 4 4 4 4 4

47 optimal path in the subtree
Cost Evaluation root c(x) g(x) current state (x) h(x) optimal path in the subtree goal

48 Cost Evaluation root g(x) current state (x) c(x)
Usually can be computed precisely. root c(x) g(x) current state (x) h(x) optimal path in the subtree Usually can be estimated only. goal

49 Admissible Heuristic a How to make a heuristic?
generally, drop or relax constraints from the original problem.

50 Heuristic Search Algorithm
Step1: Form a priority queue with root node only. Step2: Test to see if the first element in the queue is a goal node. If it is, stop. Otherwise, go to step 3. Step3: Remove the first element from the queue, and add its descendants, if any, to the queue orderly by referencing to their estimated cost. Step4: If the queue is empty, then signal failure. Otherwise, go to Step 2.

51 Heuristic Search Algorithm
Breadth-First Search Best-First Search/Hill Climbing A* algorithm

52 Example: 8-Puzzle 2 8 3 1 6 4 7 5 1 2 3 8 6 4 7 5 Start (x) Goal

53 Example: 8-Puzzle 4 1+5 1+3 1+5 2+3 2+3 2+4 3+3 3+4 3+2 3+4 4+1 5+0 2
6 4 7 5 4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 1+5 1+3 1+5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2+3 2+3 2+4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 3+3 3+4 3+2 3+4 2 8 3 1 6 4 7 5 4+1 2 8 3 1 6 4 7 5 5+0

54 Best-First Search Best-first search is a search algorithm which optimizes depth-first search by expanding the most promising node chosen according to some rule.

55 Example: 8-Puzzle 2 8 3 1 6 4 7 5 4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 5 3 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 3 3 4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 3 4 2 4 2 8 3 1 6 4 7 5 1 2 8 3 1 6 4 7 5

56 Hill Climbing A simplification of Best-First Search The idea 
You don't keep the big list of states around, i.e., you just keep track of the one state you are considering, and the path that got you there from the initial state. At every state you choose the state that leads you closer to the goal and continue from there. In fact, this is a greedy algorithm Local minima/maxima

57 A* Algorithm Admissible

58 A* Algorithm 4 1+5 1+3 1+5 2+3 2+3 2+4 3+3 3+4 3+2 3+4 4+1 5+0 2 8 3 1
6 4 7 5 4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 1+5 1+3 1+5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2+3 2+3 2+4 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 2 8 3 1 6 4 7 5 3+3 3+4 3+2 3+4 2 8 3 1 6 4 7 5 4+1 2 8 3 1 6 4 7 5 5+0

59 Optimality of A* Algorithm
If A* returns a solution, that solution is guaranteed to be optimal as long as the cost estimation function is admissible.

60 Exercise Use BFS, DFS, Hill-Climbing, Best-First Search and A* schemes to solve the following 8-puzzle problem with the evaluation function being the number of misplaced tiles. 2 3 1 8 4 7 6 5 1 2 3 7 6 4 8 5 Start Goal

61 Branch-and-Bound Algorithm
Two main tools: Branching A Mechanism to jump to subtrees Bounding A Mechanism to prune subtrees

62 Example: Shortest Paths on a Multi-stage Graph
1 2 3 5 4 7

63 Example: Shortest Paths on a Multi-stage Graph
1 2 3 5 4 7 Search tree

64 Example: Shortest Paths on a Multi-stage Graph
Solved by B&B 1 3 2 Upper Bound 1 3 2 5 3 4 3 2 7 5 6 1 4 7 6 1 4 9 5 5

65 Example: Asymmetric TSP
1 2 3 4

66 Example: Asymmetric TSP
n = 4 Example: Asymmetric TSP Search Tree (Static) 1 2 4 3 5 6 7 8 9 10 11 12 13 14 15 16 x1=2 x1=3 x1=4 x2=3 x2=4 x2=2 x3=4 x3=3 x3=2

67 Example: Asymmetric TSP
How to determine the lower-bound of each tree node? n = 5

68 Example: Asymmetric TSP
How to determine the lower-bound of root node? Example: Asymmetric TSP 10 2 2 3 4 21 n = 5 Row Reduced (21)

69 Example: Asymmetric TSP
How to determine the lower-bound of root node? 25 Example: Asymmetric TSP 1 3 Reduced Cost Matrix (21 4 = 25) Row Reduced (21)

70 Example: Asymmetric TSP
25 1 2 4 3 x1=2 x1=3 x1=4 5 x1=5 How to determine the lower-bound of the children? What are their cost reduced matrices?

71 Example: Asymmetric TSP
(i, j) Given the cost reduced matrix of node, say R, how to obtain the cost reduced matrix of its child node, say S, generated by selecting edge (i, j)?

72 Example: Asymmetric TSP
1 2 i j n R S (i, j) Reduced Cost Matrix of R Non-leaf

73 Example: Asymmetric TSP
To obtain the reduced cost matrix of S, set the surrounded entries to , then reduce it. Example: Asymmetric TSP 1 2 i j n R S (i, j) Reduced Cost Matrix of R Non-leaf

74 Example: Asymmetric TSP
Upper Bound 1 2 4 3 x1=2 x1=3 x1=4 5 x1=5 25 10 35 2 Reduced

75 Example: Asymmetric TSP
Upper Bound 1 2 4 3 x1=2 x1=3 x1=4 5 x1=5 25 10 17 35 53 3 11

76 Example: Asymmetric TSP
Upper Bound 1 2 4 3 x1=2 x1=3 x1=4 5 x1=5 25 35 53 3 25 31 6 8 7 x2=2 x2=3 x2=5 28 50 36 9 10 x3=3 x3=5 32 28 11 x4=3 28

77 Example: Asymmetric TSP
Upper Bound 1 2 4 3 x1=2 x1=3 x1=4 5 x1=5 25 28 35 53 3 25 31 6 8 7 x2=2 x2=3 x2=5 28 50 36 9 10 x3=3 x3=5 32 28 11 x4=3 28

78 Example: Asymmetric TSP
Search Tree (Dynamic) Tours include (i, j) Tours exclude (i, j)

79 Example: Asymmetric TSP
Given the cost reduced matrix of node, say r, and how to obtain the cost reduced matrices of its child nodes, say x,and y, and and by considering (i, j)? r Tours include (i, j) Tours exclude (i, j) x y

80 Example: Asymmetric TSP
Obtain the cost reduced matrix of the right child by set (i, j)th entry to  and then reduce it. The low-bound of the right child can be obtained by accumulation. Example: Asymmetric TSP r Tours include (i, j) Tours exclude (i, j) x y Cost Reduced Matrix of r.

81 Example: Asymmetric TSP
Obtain the cost reduced matrix of the left child by set the entries to  and then reduce it. The low-bound of the left child can be obtained by accumulation. Example: Asymmetric TSP r Tours include (i, j) Tours exclude (i, j) x y Cost Reduced Matrix of r.

82 Example: Asymmetric TSP
At each stage, the edge to be considered is determined based on heuristic. Possible heuristics: Choose (i, j) such that as large as possible. r Tours include (i, j) Tours exclude (i, j) x y or

83 Example: Asymmetric TSP
We will use the first one in the following example. At each stage, the edge to be considered is determined based on heuristic. Possible heuristics: Choose (i, j) such that as large as possible. Tours include (i, j) Tours exclude (i, j) x y or

84 Example: Asymmetric TSP
How to determine the lower-bound of each tree node? n = 5

85 Example: Asymmetric TSP
How to determine the lower-bound of root node? Example: Asymmetric TSP 10 2 2 3 4 21 n = 5 Row Reduced (21)

86 Example: Asymmetric TSP
How to determine the lower-bound of root node? 25 Example: Asymmetric TSP 1 3 Reduced Cost Matrix (21 4 = 25) Row Reduced (21)

87 Example: Asymmetric TSP
25 1 2 3 ? You should choose one of zero entries that reduces most cost for the right child. Why?

88 Example: Asymmetric TSP
25 1 2 3 0 ? 1 1 You should choose one of zero entries that reduces most cost for the right child.

89 Example: Asymmetric TSP
25 1 2 3 0 ? 1 2 You should choose one of zero entries that reduces most cost for the right child. 2

90 Example: Asymmetric TSP
25 1 2 3 ? 11 1 You should choose one of zero entries that reduces most cost for the right child. 2 0 11

91 Example: Asymmetric TSP
25 1 2 3 0 ? 1 You should choose one of zero entries that reduces most cost for the right child. 2 0 11

92 Example: Asymmetric TSP
25 1 2 3 0 ? 1 You should choose one of zero entries that reduces most cost for the right child. 2 11 3 3

93 Example: Asymmetric TSP
25 1 2 3 3 ? 1 You should choose one of zero entries that reduces most cost for the right child. 2 11 3 0 3

94 Example: Asymmetric TSP
25 1 2 3 11 ? 1 You should choose one of zero entries that reduces most cost for the right child. 2 11 3 0 3 11

95 Example: Asymmetric TSP
25 1 2 3 11 ? 1 You should choose one of zero entries that reduces most cost for the right child. 2 11 3 0 3 11

96 Example: Asymmetric TSP
25 1 2 3 ? 25 36 already reduced

97 Example: Asymmetric TSP
1 3 2 4 5 25 1 Optimal TSP Tour 2 3 ? 25 36 4 5 28 36 6 7 28 37

98 Exercise Using B&B algorithm to solve the TSP instance given in the above table. Using static search tree Using dynamic search tree.


Download ppt "Lecture 4: Tree Search Strategies"

Similar presentations


Ads by Google