Algorithms Chapter 5: The Tree Searching Strategy - Examples 1 / 11 Chapter 5: The Tree Searching Strategy 1. Ex 5.1Determine the satisfiability of the following Boolean formulas by depth-first search tree searching technique. (depth-first search) (tree searching technique) (Boolean formulas) (satisfiability) (a) ( X1 X 2 X ) ( X1 X ) ( X 2 ) (b) ( X1 X 2 X ) ( X1 X 2 ) ( X 2 X ) ( X ) (c) ( X1 X 2 X ) ( X1 X 2 X ) (d) ( X1 X 2 ) ( X 2 X ) ( X ) We apply the depth-first search. (a) satisfiable (b) unsatisfiable satisfying assignment: (X 1, X 2 ) X 1 =T X 1 =F X 2 =T X 2 =F X 2 =T X 2 =F X =T X =F X =T X =F X =T X =F X =T X =F (4) (c) satisfiable () (4) (1) (4) () (4) (2)
Algorithms Chapter 5: The Tree Searching Strategy - Examples 2 / 11 satisfying assignment: (X 1, X 2, -X ) (d) satisfiable satisfying assignment: (X 1, -X 2, -X ) 2. Ex 5.2Consider the following graph. Find a Hamiltonian cycle by some kind of tree searching technique. (tree searching technique) (Hamiltonian cycle) 7 5 2 6 4 1 The searching tree with respect to the graph, starting from node 1 is as follows. It can be seen from the searching tree that there are two Hamiltonian cycles for this graph. One is 1--2-7-5-4-6-1 and the other is 1-6-4-5-7-2--1. They are exactly the same.
Algorithms Chapter 5: The Tree Searching Strategy - Examples / 11. Ex 5.Solve the following sum of subset problem. S = {7, 1, 4, 6, 14, 25, 5, 8} and M = 18. Find a sum whose elements add up to M by using the depth-first search strategy. We adopt the depth-first search in the branch-and-bound strategy. The searching tree is as follows. A branch is valid only when the accumulated sum of the selected elements is less than or equal to 18. This accumulated sum is annotated inside each node. We find a subset of S, that is, {7, 1, 4, 6}, whose sum is exactly 18 so that the searching stops. 4. Ex 5.4 Solve the 8-puzzle problem which tests with the following initial state.
Algorithms Chapter 5: The Tree Searching Strategy - Examples 4 / 11 2 8 1 7 5 4 6 Note that our final goal is 1 2 8 7 6 4 5 Note that (1) the evaluation function f(n) be defined as f(n) = w(n), where w(n) is the number of misplaced tiles in node n, and (2) you must expand at least 4 levels. Let the evaluation function f(n) be defined as f(n) = w(n), where w(n) is the number of misplaced tiles in node n. We apply the hill climbing method to expand the searching tree. The partially expanded searching tree is shown in the following. Note that the value of f(n) is annotated beside node n. 2 8 1 4 (5) 2 8 1 4 2 4 (4) 8 1 (6) 2 8 1 4 2 1 () 8 4 (4) 8 2 1 4 (4)
Algorithms Chapter 5: The Tree Searching Strategy - Examples 5 / 11 5. Ex 5.5Find the shortest path from v 0 to v 8 in the following graph by the branch-and-bound strategy. (branch-and-bound strategy) v 0 v 8 (shortest path) v 0 1 5 7 4 v 1 v 2 v 10 2 2 1 v 5 v 6 9 2 5 v 8 v 4 v 7 We apply the depth-first search in the branch-and-bound strategy and we illustrate the searching tree as follows. We indicate the branching sequence of each node at its left side. It is seen that at the th branching node we obtain a path from v 0 to v 8 with weight 6 based upon which branching nodes 4, 6, 7, 8, and 10 can be bounded. Since all branches have been expanded or bounded we have a shortest path from v 0 to v 8 with weight 6. 10 2 2 >6 bound 2 4 6 7 10 v 5 v 6 v 5 v 7 v 7 2 v 8 goal weight=6 v 0 v 1 v 2 v >6 bound 1 >6 bound 0 5 7 1 5 8 9 >6 bound 4 v 4 >6 bound 6. Ex 5.6Given the following distance (cost) matrix, please solve the traveling salesperson problem on it by the branch-and-bound strategy. (You must expand at least 2 levels) a b c d a 9 7
Algorithms Chapter 5: The Tree Searching Strategy - Examples 6 / 11 b 6 5 c 5 6 6 d 9 7 4 The represented directed graph: 9 a 7 d 7 9 5 5 6 4 b 6 c 6 The reduced cost matrix: (cost matrix 1 0) a b c d a 9 7 b 6 5 c 5 6 6 5 d 9 7 4 4 1 a b c d a 0 6 b 0 1 c 0 1 0 d 5 0 lower bound = (++5+4)+1 = 16 ( cost, i.e., solution 16) 1st row C 12 or -1: C 12 reduced cost matrix cost matrix: a b c d a 6 b 0 1 c 0 1 0
Algorithms Chapter 5: The Tree Searching Strategy - Examples 7 / 11 d 5 0 1 a b c d a 0 b 0 1 c 0 0 0 d 5 2 0 lower bound = 16+(+1) = 20 ( cost, i.e., solution 20) -2: C 12 edge (2, 1) Hamiltonian cycle C 21 = 1 2 reduced cost matrix cost matrix: a c d b 1 1 c 0 0 d 5 0 a c d b 2 0 c 0 0 d 5 0 lower bound = 16+(1) = 17 ( cost, i.e., solution 17) binary decision tree(branch-and-bound strategy) (OPT) binary decision tree:
Algorithms Chapter 5: The Tree Searching Strategy - Examples 8 / 11 solutions LB = 17 C 12 LB = 20 C 12 LB = 17 C 24 C 24 LB = 19 LB = 17 C 4 C 4 No solution LB = 17 C 1 No solution C 1 (1) Branch = LB nodeexpanding (2) An upper bound 17 is obtained. (optimal solution ) () nodelb (17) expand( optimal) (4) optimal solution = 17: a b d c a 7. Ex 5.7The following is an instance of 0/1 Knapsack problem, where p i is the profit when putting object i into knapsack, w i is the weight of object i, and M = 15 is the capacity of knapsack for 1 i 4. Please use the branch-and-bound strategy to solve this problem. i 1 2 4 p i 10 10 12 18 w i 2 4 6 9 We first transfer the original problem to the following: minimize 1 i 4 p i x i, subject to 1 i 4 w i x i M for x i = 0 or 1. Initially, a lower bound and an upper bound are found. -1: lower bound (LB, optimal solution ) use the greedy strategy to find it (see Sec..7) i 1 2 4 p i /w i 5 2.5 2 2 x i 1 1 1 /9 A lower bound = (p 1 +p 2 +p +/9*p 4 ) = (2+6) = 8-2: upper bound (UB, solution> optimal) use the greedy strategy to find it s = 0; for i = 1 to 4 do if s+w i M = 15 then
Algorithms Chapter 5: The Tree Searching Strategy - Examples 9 / 11 s = s+w i ; x i = 1; else x i = 0; By the above procedure, we obtain a feasible solution (x 1, x 2, x, x 4 ) = (1, 1, 1, 0). Then, an upper bound (p 1 +p 2 +p ) = (10+10+12) = 2 is obtained. Branch-and-bound strategy: Branch strategy: the best LB ( ) and then the lowest UB ( ) of node is expanded. UB = 2 LB = 8 UB = 2 0 LB = 8 x 1 =1 x 1 =0 UB = 2 UB: (x 1 2 1, x 2, x, x 4 ) = (0, 1, 1, 0) = 22 LB = 8 LB: (x 1, x 2, x, x 4 ) = (0, 1, 1, 5/9) = 2 x 2 =1 x 2 =0 LB > node 7 UB = 2 UB: (x 1, x 2, x, x 4 ) = (1, 0, 1, 0) = 2 4 UB, LB = 8 LB: (x 1, x 2, x, x 4 ) = (1, 0, 1, 7/9) = 6 x =1 x =0 expand UB: (x 1, x 2, x, x 4 ) = (1, 1, 0, 1) = 8 5 6 LB: (x 1, x 2, x, x 4 ) = (1, 1, 0, 1) = 8 x 4 =1 x 4 =0 UB = 8 UB: (x 1, x 2, x, x 4 ) = (1, 1, 0, 0) = 20 LB = 8 7 8 LB: (x 1, x 2, x, x 4 ) = (1, 1, 0, 0) = 20 The optimal solution is 8, where (x 1, x 2, x, x 4 ) = (1, 1, 0, 1). Then, the original solution is 8.
Algorithms Chapter 5: The Tree Searching Strategy - Examples 10 / 11 Chapter 6: The Prune-and-Search Strategy 1. Ex 6.1Given a set S of n elements and an integer k, the selection problem is to find the k-th smallest element of S. Please design an O(n) time algorithm to solve the selection problem. Note that you must design an algorithm and then analyze its time complexity. (a) The algorithm Prune-and-Search Algorithm Input: A set S of n elements. Output: The k-th smallest element of S. Method. Step 1: Divide S into n/5 subsets. Each subset contains five elements. Add some dummy elements to the last subset if n is not a net multiple of S. Step 2: Sort each subset of elements. Step : Find the element p which is the median of the medians of the n/5 subsets. Step 4: Partition S into S 1, S 2 and S, which contain the elements less than, equal to, and greater than p, respectively. Step 5: If S 1 k, then discard S 2 and S and solve the problem that selects the k-th smallest element from S 1 during the next iteration; else if S 1 + S 2 k, then p is the k-th smallest element of S; otherwise, let k = k S 1 S 2, solve the problem that selects the k -th smallest element from S during the next iteration. (b) The analysis of the algorithm We first show that S 1 n/4 and S n/4 by the following figure: At least 1/4 of S known to be less than or equal to p. Each 5-element subset is sorted in non-decreasing sequence. p M At least 1/4 of S known to be greater than or equal to p. Then, at least n/4 elements are pruned away during each iteration. The problem remaining in Step 5 contains at most n/4 elements. Let T(n) be the time complexity of the algorithm. Then, Step takes T(n/5) time, Step 5 takes T(n/4), and the remaining
Algorithms Chapter 5: The Tree Searching Strategy - Examples 11 / 11 steps take O(n) time. Thus, we obtain the following recurrence. T(n) = T(n/4) + T(n/5) + O(n) (1) To solve the above recurrence, we set T(n) = a 0 + a 1 n + a 2 n 2 +..., where a 1 0. Then, T(n/4) = a 0 + (/4)a 1 n + (9/16)a 2 n 2 +... (2) T(n/5) = a 0 + (1/5)a 1 n + (1/25)a 2 n 2 +... () T((n/4)+(n/5)) = T(19n/20) = a 0 + (19/20)a 1 n + (61/400) a 2 n +... (4) By Equations (2) (4), we obtain T(n/4)+T(n/5) a 0 + T(19n/20) and hence, T(n) = T(n/4) + T(n/5) + O(n) T(19n/20) +cn cn + (19/20)cn + T((19/20) 2 n)... cn + (19/20)cn + (19/20) 2 cn +...+ (19/20) p cn + T((19/20) p+1 n) (note: (19/20) p+1 n 1 (19/20) p n) 19 p+ 1 1 ( ) 20 = cn + b 20cn + b 19 1 20 = O(n).