Topic 1 Uninformed Search [George F Luger. 2008. Artificial Intelligence Structures and Strategies for Complex Problem Solving. 6 th Ed. Pearson. ISBN: 0321545893.] 1
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 2
Introduction Uninformed search is brute-force (or blind) search. Informed search is heuristic search. A graph is a useful representation for a state space. A graph is made up of nodes (vertices) connected by edges (arcs). A graphs is easily represented by an adjacency matrix. 3
1. Depth-First Search (DFS). Search order by DFS 4
1. Depth-First Search (DFS) Depth-First Search (DFS) uses a stack (or LIFO) structure to support searching branches to their greatest depth before backtracking to previously unexplored branches. Definition of tree depth: The depth of a tree is the length of the simple path (i.e., no cycle or loop) from the root node to the furthest node. 5
1. Depth-First Search (DFS) function DFS; // George F. Luger, Stack open = [Start] // initialize closed = [ ] while open [ ] do // states remain remove leftmost state X from open if X is a goal then return SUCCESS // goal found else generate children of X put X on closed 6
1. Depth-First Search (DFS) end if a child Y of X already on open or closed discard Y // loop check put remaining children on left end of open end // end else end // end while return FAIL // no states left 7
1. Depth-First Search (DFS). Figure 3.15 Graph for DFS example (U is goal). 8
1. Depth-First Search (DFS) 0. open = [A]; closed = [ ] 1. open = [B, C, D]; closed = [A] 2. open = [E, F, C, D]; closed = [B, A] 3. open = [K, L, F, C, D]; closed = [E, B, A] 4. open = [S, L, F, C, D]; closed = [K, E, B, A] 5. open = [L, F, C, D]; closed = [S, K, E, B, A] 6. open = [T, F, C, D]; closed = [L, S, K, E, B, A] 9
1. Depth-First Search (DFS) 7. open = [F, C, D]; closed = [T, L, S, K, E, B, A] 8. open = [M, C, D], (as L is already on closed); closed = [F, T, L, S, K, E, B, A] 9. open = [C, D]; closed = [M, F, T, L, S, K, E, B, A] 10. open = [G, H, D]; closed = [C, M, F, T, L, S, K, E, B, A] 11. and so on until either U is found or open = [ ]. 10
1. Depth-First Search (DFS) If a search algorithm always finds a solution (if a solution exists) in a graph, the search algorithm is called completeness. If a search algorithm always finds the best solution (i.e., the lowest path cost among all solutions) in a graph, the search algorithm is called optimality. DFS is not complete and optimal. 11
1. Depth-First Search (DFS) DFS is best-first search (BestFS) with f(n) = depth(n). 12
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 13
2. Breadth-First Search (DFS). Search order by BFS 14
2. Breadth-First Search (BFS) Breadth-First Search (BFS) uses a queue (or FIFO) to support search of most recently unexplored nodes first. BFS always expands the shallowest unexpanded node. 15
2. Breadth-First Search (BFS) Definition of branching factor (BF): - The node BF is number of children of the node. - If all nodes have the same BF, the tree BF is the node BF. - If the tree is not uniform (i.e., different nodes have different BFs), the tree BF is defined as the average of BFs. 16
2. Breadth-First Search (BFS) function BFS // George F. Luger open = [Start] // initialize closed = [ ] while open [ ] do // states remain remove leftmost state X from open if X is a goal then return SUCCESS // goal found else generate children of X put X on closed 17
2. Breadth-First Search (BFS) end if a child Y of X already on open or closed discard Y // loop check put remaining children on right end of open end // end else end // end while return FAIL // no states left 18
2. Breadth-First Search (BFS). Figure 3.15 Graph for BFS example (U is goal). 19
2. Breadth-First Search (BFS) 0. open = [A]; closed = [ ] 1. open = [B, C, D]; closed = [A] 2. open = [C, D, E, F]; closed = [B, A] 3. open = [D, E, F, G, H]; closed = [C, B, A] 4. open = [E, F, G, H, I, J]; closed = [D, C, B, A] 20
2. Breadth-First Search (BFS) 5. open = [F, G, H, I, J, K, L]; closed = [E, D, C, B, A] 6. open = [G, H, I, J, K, L, M] (as L is already on open); closed = [F, E, D, C, B, A] 7. open = [H, I, J, K, L, M, N]; closed = [G, F, E, D, C, B, A] 8. and so on until either U is found or open = [ ]. 21
2. Breadth-First Search (BFS) BFS is complete if branching factor b is finite. That is, if b is finite, BFS always finds the shallowest solution. BFS is optimal if all step costs are identical. BFS is a special case of uniform-coast search (UCS). That is, when all step costs are equal (i.e., g(n) = depth(n)), UCS becomes (or reproduces) BFS. In other words, BFS is UCS with g(n) = depth(n). BFS is best-first search (BestFS) with f(n) = depth(n). 22
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 23
3. Depth-Limited Search (DLS). Search order by DLS (depth limit = 3) 24
3. Depth-Limited Search (DLS) function DLS(l); // depth limit / bound l = 0, 1, 2,... open = [Start] // initialize closed = [ ] while open [ ] do // states remain remove leftmost state X from open put X on closed if X is a goal then return SUCCESS // goal found if the depth of X is equal to the depth limit go back to while loop 25
3. Depth-Limited Search (DLS) end else generate children of X if a child Y of X already on open or closed discard Y // loop check put remaining children on left end of open end // end else end // end while return FAIL // no states left 26
3. Depth-Limited Search (DLS). Figure 3.15 Graph for DLS example (U is goal). 27
3. Depth-Limited Search (DLS) 0. open = [A]; closed = [ ] 1. open = [B, C, D]; closed = [A]; X = A; depth(a) = 0 < 2 2. open = [E, F, C, D]; closed = [B, A]; X = B; depth(b) = 1 < 2 3. open = [F, C, D]; closed = [E, B, A]; X = E; depth(e) = 2 = limit 28
3. Depth-Limited Search (DLS) 4. open = [C, D]; closed = [F, E, B, A]; X = F; depth(f) = 2 = limit 5. open = [G, H, D]; closed = [C, F, E, B, A]; X = C; depth(c) = 1 < 2 6. open = [H, D]; closed = [G, C, F, E, B, A]; X = G; depth(g) = 2 = limit 29
3. Depth-Limited Search (DLS) 7. open = [D]; closed = [H, G, C, F, E, B, A]; X = H; depth(h) = 2 = limit 8. open = [I, J], closed = [D, H, G, C, F, E, B, A]; X = D; depth(d) = 1 < 2 9. open = [J]; closed = [I, D, H, G, C, F, E, B, A]; X = I; depth(i) = 2 = limit 30
3. Depth-Limited Search (DLS) 10. open = []; closed = [J, I, D, H, G, C, F, E, B, A]; X = J; depth(j) = 2 = limit 11. open = [ ]. // return FAIL 31
3. Depth-Limited Search (DLS) DLS is not complete and optimal. 32
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 33
4. Iterative-Deepening Search (IDS). Search order by IDS 34
4. Iterative-Deepening Search (IDS) IDS performs a DLS of the state space with a depth bound of 0. If it fails to find a goal, DLS performs another DLS with a depth bound of 1. This continues, increasing the depth bound by one each time. At each iteration, the algorithm performs a complete DLS with the current depth limit. No information about the state space is retained between iterations. 35
4. Iterative-Deepening Search (IDS). Figure 3.10 Node order in IDS [Matthew L. Ginsberg] 36
4. Iterative-Deepening Search (IDS) IDS is complete if branching factor b is finite. IDS is optimal if all step costs are equal. 37
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 38
5. Uniform-Cost Search (UCS) UCS (or Branch and Bound) is a variation of the best-first search (BestFS). Let us denote the following notations. - g(n) is the lowest path cost of going from the start node s to the current node n. - h(n) is the heuristically estimated distance from the current node n to the goal node g. - f(n) = g(n) + h(n) is called a heuristic evaluation function. 39
5. Uniform-Cost Search (UCS) f(n) is the heuristically estimated distance from the start node s to the goal node g. UCS uses only g(n) when exploring (examining) each node n. The node with the lowest value of g is expanded first. 40
5. Uniform-Cost Search (UCS) UCS expands the node n with the lowest path cost g(n) by storing the frontier as a priority queue ordered by g. UCS expands nodes in order of their optimal path cost. Hence, the first goal node selected for expansion must be the optimal solution. 41
5. Uniform-Cost Search (UCS) Algorithm: Uniform-Coast Search // Ariel Felner Input: Source vertex s 0. closed = [] 1. open.insert(s) // open is a priority queue 2. while open empty do 3. u = open.extract_min() 4. if u is a goal then return the corresponding solution 42
5. Uniform-Cost Search (UCS) 5. for each vertex v Adj(u) do // for each child v of u 6. g(v) = g(u) + cost(u, v) 7. v = check_for duplicates(v) 8. open.insert(v ) 9. closed.insert(u) 43
5. Uniform-Cost Search (UCS) check_for_duplicates(v): Duplicate check is done as follows. When a new node v is generated, we check whether it is already in open. - If v is in open, we keep a single copy of v with the smallest g-value among the two copies (labeled v in the algorithm). - If v is in closed, its new copy is discarded. 44
5. Uniform-Cost Search (UCS). Graph for UCS example (start node S, goal node E) 45
5. Uniform-Cost Search (UCS) closed = [] open = [S] 1. u = X = S; open = [] v = B: g(v) = g(b) = g(s) + cost(s, B) g(b) = 0 + 80 = 80 v = C: g(v) = g(c) = g(s) + cost(s, C) g(c) = 0 + 99 = 99 open = [S B(80), S C(99)] closed = [S] 46
5. Uniform-Cost Search (UCS) 2. u = X = B; open = [S C(99)] v = D: g(v) = g(d) = g(b) + cost(b, D) g(d) = 80 + 97 = 177 open = [S C(99), S B D(177)] closed = [S, B] 47
5. Uniform-Cost Search (UCS) 3. u = X = C; open = [S B D(177)] v = E: g(v) = g(e) = g(c) + cost(c, E) g(e) = 99 + 211 = 310 open = [S B D(177), S C E(310)] closed = [S, B, C] 48
5. Uniform-Cost Search (UCS) 4. u = X = D; open = [S C E(310)] v = E: g(v) = g(e) = g(d) + cost(d, E) g(e) = 177 + 101 = 278 open = [S B D E(278), S C E(310)] open = [S B D E(278)] closed = [S, B, C, D] 49
5. Uniform-Cost Search (UCS) 5. u = X = E; open = [] u = E is a goal then return the corresponding solution: S B D E(278) v = none closed = [S, B, C, D, E] 6. open = [] stop. The solution path is S B D E(278) 50
5. Uniform-Cost Search (UCS) UCS is best-first search (BestFS) with f(n) = g(n). UCS is A * search with h(n) = 0 (i.e., f(n) = g(n)). 51
Contents 1. Depth-First Search (DFS) 2. Breadth-First Search (BFS) 3. Depth-Limited Search (DLS) 4. Iterative-Deepening Search (IDS) 5. Uniform-Cost Search (UCS) 6. Bidirectional Search (BDS) 52
6. Bidirectional Search (BDS). Search order by BDS 53
6. Bidirectional Search (BDS) When the start and goal nodes are known, the bidirectional search (BDS) can be used. The idea behind BDS is to run two search algorithms (e.g., BFS, O(b d )) simultaneously, namely one forward from the initial state and the other backward from the goal - hoping that the two searches meet in the middle. The motivation is that b d/2 + b d/2 is much less than b d. 54
6. Bidirectional Search (BDS) BDS is implemented by replacing the goal test with a check to see whether the frontiers of the two searches intersect; if they do, a solution has been found. The time complexity of BDS using breadth-first searches in both directions is O(b d/2 ). The space complexity of BDS is also O(b d/2 ). When the two paths meet at a common node, a complete and optimal solution path is found. 55
6. Bidirectional Search (BDS) A major disadvantage of BDS is that BDS requires knowledge of the goal node. The goal node is the most commonly desired node in search, and is therefore not commonly known a priori. 56
6. Bidirectional Search (BDS) Notations used s = start (initial) node, t = terminal (goal) node. S = set of nodes reached from s whose minimum distance from s is known. S = set of nodes reached from S by one edge but are not in S. g s (x) = current shortest distance from s to x. g s (s) = 0 57
6. Bidirectional Search (BDS) wf(x) = the immediate predecessor node of x along the path from s to x (i.e., parent of x). T = set of nodes which have a path to t whose minimum distance is already found. T = set of nodes reached from T by one edge but are not in T. g t (x) = current shortest distance from x to t. wt(x) = the immediate successor node of x along the path from x to t (i.e., child of x). 58
6. Bidirectional Search (BDS) Call the forward algorithm F and the backward algorithm B; we now wish to combine them into a bidirectional search (BDS) algorithm. Algorithm BDS // Ira Pohl 1. (Initialize) Perform F 1 (the first step of the forward algorithm) and B 1. That is, S = {s}, T = {t} 59
6. Bidirectional Search (BDS) 2. (Strategy) Decide to go forward (go to Step 3) or backward (go to Step 4). 3. (Forward expansion) Perform F 2. If n T go to Step 5 else go to Step 2. 4. (Backward expansion) Perform B 2. If n S go to Step 5 else go to Step 2. 5. (Terminate) If x S T, stop. The solution path is (s x t). 60
6. Bidirectional Search (BDS) Example: Apply the BDS to the figure given below, where start node is A and goal node is I. 61
6. Bidirectional Search (BDS) 1. F 1 : S = {A}, B 1 : T = {I} 2. Decide to go to Step 3. 3. F 2 : S = {A, E, B, J}. Go back to Step 2 2. Decide to go to Step 4. 4. B 2 : T = {I, G}. Go back to Step 2 62
6. Bidirectional Search (BDS) 2. Decide to go to Step 4. 4. B 3 : T = {I, G, E, H}. n = E S so go to Step 5. 5. x S T = {E}. Stop. The solution path is (A E G I) It is noted that the decision made in Step 2 really affects the performance of the bidirectional search (BDS). 63
6. Bidirectional Search (BDS) The cardinality comparison principle can be used for Step 2 as follows. // 2. (Strategy) Decide to go forward (go to Step 3) or backward (go to Step 4). 2. If S < T then go to Step 3 else go to Step 4. 64
Exercises 1. Provide the search order for the nodes shown in the figure given below for DFS, BFS, DLS (d = 2), IDS (start depth = 1) and BDS (start node A, goal node N). 65
Exercises 2. In general, IDS is better than DFS. Draw a graph to show the case in which DFS is better than IDS. 3. Explain why DLS is not complete in general. 66
Exercises 4. Using the UCS algorithm, find the shortest path from the start node A to the goal node F from the figure given below. 67
References 1. George F Luger. 2008. Artificial Intelligence Structures and Strategies for Complex Problem Solving. 6 th Ed. Pearson. ISBN: 0321545893. 2. M. Tim Jones. 2008. Artificial Intelligence A Systems Approach. Jones & Bartlett Learning. ISBN: 0763773379. 68
References 3. Ben Coppin. 2004. Artificial Intelligence Illuminated. Jones & Bartlett Learning. ISBN: 0763732303. 4. Ivan Bratko. 2011. Prolog Programming for Artificial Intelligence. 4 th Ed. Pearson. ISBN: 0321417461. 69
References 5. Stuart J. Russell, Peter Norvig. 2009. Artificial Intelligence A Modern Approach. 3 rd Ed. Pearson. ISBN: 0136042597. 6. Matthew L. Ginsberg. 1993. Essentials of Artificial Intelligence. Morgan Kaufmann. ISBN: 0124121535. 70