CS B551: E LEMENTS OF A RTIFICIAL I NTELLIGENCE Instructor: Kris Hauser 1
A GENDA Searching, data structures, and algorithms Breadth-first search Depth-first search Uniform-cost search 2
D EFINING A S EARCH P ROBLEM 3 State space S Successor function: x S SUCC (x) 2 S Initial state s 0 Goal test: x S GOAL? (x) =T or F Arc cost S
S TATE G RAPH 4 Each state is represented by a distinct node An arc (or edge) connects a node s to a node s’ if s’ SUCC (s) The state graph may contain more than one connected component
S OLUTION TO THE S EARCH P ROBLEM A solution is a path connecting the initial node to a goal node (any one) The cost of a path is the sum of the arc costs along this path An optimal solution is a solution path of minimum cost There might be no solution ! 5 I G
S EARCH G RAPH != S TATE G RAPH If states are allowed to be revisited, the search tree may be infinite even when the state space is finite If states are allowed to be revisited, the search tree may be infinite even when the state space is finite
S EARCH G RAPH N ODE != S TATE 7 PARENT-NODE STATE Depth of a node N = length of path from root to N (depth of the root = 0) BOOKKEEPING 5Path-Cost 5Depth RightAction Expanded yes... CHILDREN
F RINGE OF S EARCH T REE The fringe is the set of all search nodes that haven’t been expanded yet
S EARCH S TRATEGY The fringe is the set of all search nodes that haven’t been expanded yet The fringe is implemented as a priority queue FRINGE INSERT(node,FRINGE) REMOVE(FRINGE) The ordering of the nodes in FRINGE defines the search strategy 9
S EARCH A LGORITHM #1 SEARCH#1 1. If GOAL?(initial-state) then return initial-state 2. INSERT(initial-node,FRINGE) 3. Repeat: 4. If empty(FRINGE) then return failure 5.N REMOVE(FRINGE) 6.s STATE(N) 7.For every state s’ in SUCCESSORS(s) 8.Create a new node N’ as a child of N 9.If GOAL?(s’) then return path or goal state 10.INSERT(N’,FRINGE) 10 Expansion of N
B LIND S EARCH S TRATEGIES 11
B LIND S TRATEGIES Breadth-first Bidirectional Depth-first Depth-limited Iterative deepening Uniform-Cost (variant of breadth-first) 12 Arc cost = 1 Arc cost = c(action) 0
B READTH -F IRST S TRATEGY New nodes are inserted at the end of FRINGE FRINGE = (1)
B READTH -F IRST S TRATEGY New nodes are inserted at the end of FRINGE 14 FRINGE = (2, 3)
B READTH -F IRST S TRATEGY New nodes are inserted at the end of FRINGE 15 FRINGE = (3, 4, 5)
B READTH -F IRST S TRATEGY New nodes are inserted at the end of FRINGE 16 FRINGE = (4, 5, 6, 7)
P ERFORMANCE M EASURES Completeness A search algorithm is complete if it finds a solution whenever one exists [What about the case when no solution exists?] Optimality A search algorithm is optimal if it returns a minimum-cost path whenever a solution exists Complexity It measures the time and amount of memory required by the algorithm 17
I MPORTANT P ARAMETERS Maximum number of successors of any state branching factor b of the search tree Minimal length (≠ cost) of a path between the initial and a goal state depth d of the shallowest goal node in the search tree 18
E VALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is: Complete? Not complete? Optimal? Not optimal? 19
E VALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is: Complete Optimal if step cost is 1 Number of nodes generated: ??? 20
E VALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is: Complete Optimal if step cost is 1 Number of nodes generated: 1 + b + b 2 + … + b d = ??? 21
E VALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is: Complete Optimal if step cost is 1 Number of nodes generated: 1 + b + b 2 + … + b d = (b d+1 -1)/(b-1) = O(b d ) Time and space complexity is O(b d ) 22
T IME AND M EMORY R EQUIREMENTS d# NodesTimeMemory msec11 Kbytes 411,1111 msec1 Mbyte 6~ sec100 Mb 8~ sec10 Gbytes 10~ hours1 Tbyte 12~ days100 Tbytes 14~ years10,000 Tbytes 23 Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
T IME AND M EMORY R EQUIREMENTS d# NodesTimeMemory msec11 Kbytes 411,1111 msec1 Mbyte 6~ sec100 Mb 8~ sec10 Gbytes 10~ hours1 Tbyte 12~ days100 Tbytes 14~ years10,000 Tbytes 24 Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
R EMARK If a problem has no solution, breadth-first may run forever (if the state space is infinite or states can be revisited arbitrary many times) ?
B IDIRECTIONAL S TRATEGY 26 2 fringe queues: FRINGE1 and FRINGE2 s Time and space complexity is O(b d/2 ) O(b d ) if both trees have the same branching factor b Question: What happens if the branching factor is different in each direction?
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE FRINGE = (1)
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE FRINGE = (2, 3)
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE FRINGE = (4, 5, 3)
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
D EPTH -F IRST S TRATEGY New nodes are inserted at the front of FRINGE
E VALUATION b: branching factor d: depth of shallowest goal node m: maximal depth of a leaf node Depth-first search is: Complete? Optimal? 38
E VALUATION b: branching factor d: depth of shallowest goal node m: maximal depth of a leaf node Depth-first search is: Complete only for finite search tree Not optimal Number of nodes generated (worst case): 1 + b + b 2 + … + b m = O(b m ) Time complexity is O(b m ) Space complexity is O(bm) [or O(m)] [Reminder: Breadth-first requires O(b d ) time and space] 39
D EPTH -L IMITED S EARCH Depth-first with depth cutoff k (depth at which nodes are not expanded) Three possible outcomes: Solution Failure (no solution) Cutoff (no solution within cutoff) 40
I TERATIVE D EEPENING S EARCH Provides the best of both breadth-first and depth-first search Main idea: 41 IDS For k = 0, 1, 2, … do: Perform depth-first search with depth cutoff k (i.e., only generate nodes with depth k) Totally horrifying !
I TERATIVE D EEPENING 42
I TERATIVE D EEPENING 43
I TERATIVE D EEPENING 44
P ERFORMANCE Iterative deepening search is: Complete Optimal if step cost =1 Time complexity is: (d+1)(1) + db + (d-1)b 2 + … + (1) b d = O(b d ) Space complexity is: O(bd) or O(d) 45
C ALCULATION db + (d-1)b 2 + … + (1) b d = b d + 2b d-1 + 3b d-2 +… + db = (1 + 2b b -2 + … + db -d ) b d ( i=1,…, ib (1-i) ) b d = b d (b/(b-1)) 2 46
N UMBER OF G ENERATED N ODES (B READTH - F IRST & I TERATIVE D EEPENING ) d = 5 and b = 2 47 BFID 11 x 6 = 6 22 x 5 = x 4 = x 3 = x 2 = x 1 = /63 ~ 2
N UMBER OF G ENERATED N ODES (B READTH - F IRST & I TERATIVE D EEPENING ) d = 5 and b = BFID ,0003,000 10,00020, , ,111123, ,456/111,111 ~ 1.111
R ECAP BFS: Complete, optimal O(b d ) time and space DFS: Not complete nor optimal O(bd) space, unbounded time ID: Complete, optimal O(bd) space, O(b d ) time 49
R ELATED T OPICS Non-unit costs Revisited states Heuristic search 50
N ON - UNIT C OSTS Each arc has cost c > 0 The cost of the path to each node N is g(N) = costs of arcs Breadth-first is no longer optimal 51 Why?
U NIFORM -C OST S EARCH Expand node in FRINGE with the cheapest path 52 S 0 1 A 5 B 15 C SG A B C G 11 G 10 Suboptimal path!
S EARCH A LGORITHM #2 SEARCH#2 1. If GOAL?(initial-state) then return initial-state 2. INSERT(initial-node,FRINGE) 3. Repeat: 4. If empty(FRINGE) then return failure 5.N REMOVE(FRINGE) 6.s STATE(N) 7.If GOAL?(s) then return path or goal state 8.For every state s’ in SUCCESSORS(s) 9.Create a new node N’ as a child of N 10.INSERT(N’,FRINGE) 53 The goal test is applied to a node when this node is expanded, not when it is generated.
R EVISITED S TATES 54 8-queens No assembly planning Few puzzle and robot navigation Many search tree is finitesearch tree is infinite
A VOIDING R EVISITED S TATES Requires comparing state descriptions Breadth-first search: Store all states associated with generated nodes in VISITED If the state of a new node is in VISITED, then discard the node 55
A VOIDING R EVISITED S TATES Requires comparing state descriptions Breadth-first search: Store all states associated with generated nodes in VISITED If the state of a new node is in VISITED, then discard the node 56 Implemented as hash-table or as explicit data structure with flags
E XPLICIT D ATA S TRUCTURES Robot navigation VISITED: array initialized to 0, matching grid When grid position (x,y) is visited, mark corresponding position in VISITED as 1 Size of the entire state space! 57
H ASH T ABLES N-1 VISITED: Hash table of size N Hash function: map from S to {0,…,N-1} If hash function is chosen carefully to minimize chance of collision, then membership testing on M states averages O(M/N) time
A VOIDING R EVISITED S TATES Depth-first search: Solution 1: Store all states associated with nodes in current path in VISITED If the state of a new node is in VISITED, then discard the node ?? 59
A VOIDING R EVISITED S TATES Depth-first search: Solution 1: Store all states associated with nodes in current path in VISITED If the state of a new node is in VISITED, then discard the node Only avoids loops Solution 2: Store all generated states in VISITED If the state of a new node is in VISITED, then discard the node Same space complexity as breadth-first ! 60
A VOIDING R EVISITED S TATES IN U NIFORM -C OST S EARCH For any state S, when the first node N such that STATE(N) = S is expanded, the path to N is the best path from the initial state to S So: When a node is expanded, store its state into VISITED When a new node N is generated: If STATE(N) is in VISITED, discard N If there exits a node N’ in the fringe such that STATE(N’) = STATE(N), discard the node -- N or N’ -- with the highest- cost path 61
S EARCH A LGORITHM #3 SEARCH#3 1. If GOAL?(initial-state) then return initial-state 2. INSERT(initial-node,FRINGE) 3. Repeat: 4. If empty(FRINGE) then return failure 5.N REMOVE(FRINGE) 6.s STATE(N) 7.Add s to VISITED 7.If GOAL?(s) then return path or goal state 8.For every state s’ in SUCCESSORS(s) 9.Create a new node N’ as a child of N 10.If s’ is in VISITED then discard N’ 11.If there is N’’ in FRINGE with STATE(N’)=STATE(N’’) 12. If g(N’’) is lower than g(N’) then discard N’ 13.Otherwise discard N’’ 14.INSERT(N’,FRINGE) 62
H OMEWORK Readings: R&N Ch