2. A search problem
⢠A Figure below represents a map.
⢠The nodes represent cities, and the links represent direct road
connections between cities.
⢠The number associated to a link represents the length of the
corresponding road.
⢠The search problem is to find a path from a city S to a city G
S
G
D
E
B
A
C
5
3
4
4
6
3
5
AI, Subash Chandra Pakhrin 2
3. Classes of search methods:
⢠Uninformed (or blind) search:
⢠The order in which potential solution paths are considered is arbitrary, using
no domain specific information to judge where the solution is likely to lie
⢠Informed (heuristic) search:
⢠The domain dependent (heuristic) information is used in order to search the
space more efficiently.
⢠Searching using heuristic function.
AI, Subash Chandra Pakhrin 3
4. Measuring problem solving Performance
We will evaluate the performance of search algorithm in four ways
⢠Completeness: An algorithm is said to be complete if it definitely finds solution
to the problem, if exist.
⢠Time Complexity: How long (worst or average case) does it take to find a
solution ?
⢠Usually measured in terms of the number of nodes expanded.
⢠Space Complexity: How much space is used by the algorithm ? Usually
measured in terms of the maximum number of nodes in memory at a time.
⢠Optimality / Admissibility: If a solution is found, is it guaranteed to be an
optimal one ?
AI, Subash Chandra Pakhrin 4
5. Measuring problem solving Performance
Time and space complexity are measured in terms of
⢠b â maximum branching factor (number of successor of any node) of
the search tree
⢠m â maximum depth of the search tree
⢠d â depth of the shallowest goal node
AI, Subash Chandra Pakhrin 5
7. Uninformed / Blind Search
Blind Search
⢠The algorithm doesnât use any information about the domain
⢠British Museum Search
⢠Breadth First Search
⢠Depth First Search
⢠Iterative Deepening Search
⢠Bidirectional Search
AI, Subash Chandra Pakhrin 7
8. British Museum Approach
⢠Find every possible path
# Note: Rule we should develop the tree associated with the search in
Lexical (alphabetical) order.
S
A B
B D
C
E
G
A C
D
G
E
British Museum
Expansion of Tree
AI, Subash Chandra Pakhrin 8
9. Breadth First Search (BFS)
⢠Build up the tree level by level
⢠At some point when you scan across a level you will find that you
have completed the path that goes to the goal.
AI, Subash Chandra Pakhrin 9
10. Breadth First Search (BFS)
⢠Fringe (OPEN / QUEUE / FRONTIER)
⢠Fringe is a list of nodes which are waiting to be expanded.
⢠The new nodes which are generated will be put at the back of
fringe
⢠Breadth First Search results in the expansion of shallowest nodes
earlier than the deeper nodes.
AI, Subash Chandra Pakhrin 10
11. Breadth First Search (BFS)
Let fringe be a list containing the initial state
LOOP
if fringe is empty return failure
Node <- remove â first (fringe)
if Node is a goal
then return the path from initial state to Node
else generate all successors of Node,
and %% Expand shallowest node first add generated nodes to the back of fringe
END LOOP
AI, Subash Chandra Pakhrin 11
12. Search Space (Apply BFS for the search space)
A
G
C
HFD
E
B
AI, Subash Chandra Pakhrin 12
13. Previous Search
Space will give rise
to the this Search
Tree
A
B
HGE
FB
HGG
FC
D GD E
C
d = 0 Level 0: Expanded
d = 1 Level 1: All nodes Expanded
d = 2 Level 2: All nodes Expanded
d = 3 And so on
AI, Subash Chandra Pakhrin 13
14. BFS
S
A D
B
E
C
F
G
D A
E E B B
A CECFBFD
Itâs an stupid approach:
Because, it extends the path that go to the same node more than once.
AI, Subash Chandra Pakhrin 14
15. BFS FRINGE Expansion
Initially
⢠FRINGE: A // we will execute loop of an algorithm.
⢠FRINGE: BC // remove first node from the FRINGE and get
// its successor
⢠FRINGE: CDE // B is removed from the FRINGE as B is the
// first node in the FRINGE and successor of
// âCâ DE will be generated and merged at the
// back of the FRINGE.
⢠FRINGE: DEDG // C is removed from the FRINGE and its
// successor D and G are generated and put at
// the back of FRINGE
⢠FRINGE: EDGCF // D is selected for expansion so, D will be removed
// form FRINGE and its successors C & F will be
// generated and merged at the back of FRINGE
⢠FRINGE: DGCF // E will be removed from the FRINGE and its
// successor will be put at the back of the fringe
// Since E has no successors it will be simply removed from the
// FRINGE
⢠FRINGE: GCFBF // D will be removed from the FRINGE and its successors B & F
// will be generated and added to the back of FRINGE.
⢠G is the next node to be expanded and G happens to be goal node, In this case search will stop.
AI, Subash Chandra Pakhrin 15
16. Breadth First Search (BFS)
⢠First node A is expanded then
⢠Node B is expanded
⢠Node C is expanded
⢠Node D is expanded
⢠Node E is expanded
⢠Node D is expanded
⢠Node G is expanded
In BFS nodes are expanded in this order according to their level.
AI, Subash Chandra Pakhrin 16
17. BFS Evaluation
⢠Enqueue nodes on FRINGE in FIFO order
⢠Completeness
⢠Does it always find a solution if one exists ?
⢠Yes
⢠If shallowest goal node is at some finite depth d and if b is finite.
⢠Optimal (i.e. admissible)
⢠If all paths have the same cost. Otherwise, finds solution with shortest path
length.
⢠Exponential Time and Space Complexity, O (b d), where d is the depth
of the solution and b is the branching factor ( i.e. number of children)
at each node.
⢠A complete search tree of depth d where each non â leaf node has b
children, has a total of
1 + b + b 2 + ⌠+ b d = (b d +1 - 1) / (b â 1) = O (b d) nodesAI, Subash Chandra Pakhrin 17
18. BFS Evaluation
⢠For a complete search tree of depth 12, where every node at depths
0, 1, 2, ⌠, 11 has 10 children and every node at depth 12 has 0
children, there are O (10 12) nodes in the complete search tree.
⢠If BFS expands 1000 nodes / sec and each node uses 100 bytes of
storage, then BFS will take 35 years to run in the worst case, and it
will use 111 Terabytes of memory.
AI, Subash Chandra Pakhrin 18
19. BFS
⢠Advantages
⢠Finds the path of minimal length to the goal.
⢠Disadvantage
⢠Requires the generation and storage of a tree whose size is exponential the
depth of the shallowest goal node.
AI, Subash Chandra Pakhrin 19
20. Depth First Search
Let fringe be a list containing the initial state
LOOP
if fringe is empty return failure
Node <- remove â first (fringe)
if Node is a goal
then return the path from initial state to Node
else generate all successors of Node,
and : : Expand deepest node first add generated nodes to the front of
fringe
END LOOP AI, Subash Chandra Pakhrin 20
21. Depth First Search
⢠Fringe is
maintained as Last
in First out (LIFO)
structure or a
stack structure.
⢠We always go
down to left
branch by
convention
A
B
HGE
FB
HGG
FC
D GD E
C
AI, Subash Chandra Pakhrin 21
25. DFS Evaluation
⢠Completeness
⢠Does it always find a solution if one exists ?
⢠NO
⢠If search space is infinite and search space contains loops then DFS may not find solution.
⢠Exponential Time Complexity, O (b d), where d is the depth of the
solution and b is the branching factor ( i.e. number of children) at
each node.
⢠Space Complexity:
⢠It needs to store only a single path from the root node to a leaf node, along
with remaining unexpanded sibling nodes for each node on the path.
⢠Total no. of nodes in memory: O (bm)
⢠Optimal (i.e. admissible)
⢠DFS expand deepest node first, it expands entire left sub-tree even if right
sub-tree contains goal nodes at levels 2 or 3. Thus we can say DFS may not
always give optimum solution.
AI, Subash Chandra Pakhrin 25
26. Depth First Search
AI, Subash Chandra Pakhrin 26
A
B
HGE
FB
HGG
FC
D GD E
C
Class work:
Expand this search
tree using Depth
First Search
approach
27. Depth First Search
Fringe: A
Fringe: BC
Fringe: DEC
Fringe: CFEC
Fringe: GFEC
AI, Subash Chandra Pakhrin 27
28. Questions
⢠Consider the following graph. Starting from state A, execute DFS. The
goal node is G. Show the order in which the nodes are expanded.
Assume that the alphabetically smaller node is expanded first to
break ties.
AI, Subash Chandra Pakhrin 28
A
G
C
HFD
E
B
29. DFS
AI, Subash Chandra Pakhrin 29
1. FRINGE: A
2. FRINGE: BC
3. FRINGE: DEC
4. FRINGE: FEC
5. FRINGE: HEC
6. FRINGE: EC
7. FRINGE: C
7. FRINGE: DG
8. FRINGE: G
30. Formalizing search in a state space
⢠A state space is a graph (V, E)
⢠V is a set of nodes and
⢠E is a set of arcs
⢠Each arc has a fixed, positive cost.
⢠Each node is a data structure
⢠A state description
⢠The parent of the node
⢠Depth of the node
⢠The operator that generated this node
⢠Cost of this path (sum of operator costs)
AI, Subash Chandra Pakhrin 30
31. Formalizing search II
⢠A solution is a sequence of operators that is associated with a path
from a start node to a goal node.
⢠Cost of a solution: sum of the arc costs on the solution path
⢠For large state spaces, it isnât practical to represent the whole space.
State space search makes explicit a sufficient portion of an implicit
state space graph to find a goal node.
⢠Each node represents a partial solution path from the start node to
the given node.
⢠In general, from this node there are many possible paths that have this partial
path as a prefix.
AI, Subash Chandra Pakhrin 31
32. Some issues
⢠Search process constructs a search tree, where
⢠Root is the initial state and
⢠Leaf nodes are nodes
⢠Not yet expanded (i.e., in fringe) or
⢠Having no successors (i.e. âdead endsâ)
⢠Search tree may be infinite because of loops even if state space is
small
⢠Return a path or a node depending on problem.
AI, Subash Chandra Pakhrin 32
33. Depth limited search (limit)
Let fringe be a list containing the initial state
LOOP
if fringe is empty return failure
Node <- remove-first (fringe)
if Node is a goal
then return the path from initial state to Node
else if depth of Node = limit return cutoff
else add generated nodes to the front of fringe
End LOOP
AI, Subash Chandra Pakhrin 33
34. Depth limited search (limit)
⢠If we limit the depth before hand a solution may not be found at that
depth therefore we have a variation of depth first search which is
called:
Depth First Iterative Deepening (DFID)
AI, Subash Chandra Pakhrin 34
35. Depth First Iterative Deepening (DFID)
⢠First do DFS to depth 0 (i.e., treat start node as having no successors),
then, if no solution found, do DFS to depth 1, etc.
AI, Subash Chandra Pakhrin 35
Until solution found do
DFS with depth cutoff c
c = c + 1
36. Iterative Deepening
⢠Advantage
⢠Linear memory requirements of depth-first search
⢠Guarantee for goal node of minimal depth
⢠Procedure
⢠Successive depth-first searches are conducted â each with depth bounds
increasing by 1
AI, Subash Chandra Pakhrin 36
39. Depth First Iterative Deepening (DFID)
⢠Complete
⢠Optimal/Admissible if all operators have the same cost. Otherwise,
not optimal but guarantees finding solution of shortest length (like
BFS).
⢠Time complexity is a little worse than BFS or DFS because nodes near
the top of the search tree are generated multiple times, but because
almost all of the nodes are near the bottom of a tree, the worst case
time complexity is still exponential, O (b d).
AI, Subash Chandra Pakhrin 39
40. Depth First Iterative Deepening (DFID)
⢠If branching factor is b and solution is at depth d, then nodes at
depth d are generated once, nodes at depth d-1 are generated twice,
etc.
⢠Hence b d + 2b (d-1) + ⌠+db <= b d /(1 - 1/b)2 = O(b d)
⢠Linear space complexity, O(b d), like DFS
⢠Has advantage of BFS (i.e. completeness) and also advantages of DFS
(i.e. limited space and finds longer paths more quickly)
⢠Generally preferred for large state spaces where solution depth is
unknown
AI, Subash Chandra Pakhrin 40
41. Iterative Deepening Search
AI, Subash Chandra Pakhrin 41
⢠Run Iterative Deepening search on the same graph.
A
G
C
HFD
E
B
42. Iterative
Deepening
Search
LIMIT = 0
FRINGE: A
LIMIT = 1
FRINGE: A
FRINGE: BC
FRINGE: C
FRINGE: empty
AI, Subash Chandra Pakhrin 42
LIMIT = 2
FRINGE = A
FRINGE = BC
LIMIT = 2
FRINGE = DEC
FRINGE = EC
LIMIT = 2
FRINGE = C
FRINGE = DG
LIMIT = 2
FRINGE = G
GOAL
46. Bi-directional search
⢠Alternate searching from the start state towards the goal and from the
goal state towards the start.
⢠Stops when the frontiers intersect.
⢠Works well only when there are unique start and goal states.
⢠Problem: How do we search backwards from goal ?
⢠Requires the ability to generate âpredecessorâ states. Predecessors of node n =
all nodes that have n as successor
46AI, Subash Chandra Pakhrin
47. Bi-directional search
⢠For bidirectional search to work well, there must be an efficient way to
check whether a given node belongs to the other search tree.
⢠Select a given search algorithm for each half.
⢠Can (sometimes) lead to finding a solution more quickly.
47AI, Subash Chandra Pakhrin
48. Bi-directional search
b = branching factor
d = distance from start to goal state
BFS: O (b d) ;Time and space
complexity
No of Nodes expanded in
BDS: 2* O (b d/2)
48AI, Subash Chandra Pakhrin
50. Search Graphs
⢠If the search space is not a tree, but a graph, the search tree may contain
different nodes corresponding to the same state.
50AI, Subash Chandra Pakhrin
51. A state space that Generates an Exponentially Growing
Search Space
51AI, Subash Chandra Pakhrin
52. Avoiding Repeated States
⢠In increasing order of effectiveness in reducing size of state space and
with increasing computational costs:
⢠Do not return to the state you just came from.
⢠Do not create paths with cycles in them.
⢠Do not generate any states that was ever created before.
⢠CLOSED IS A LIST WHICH KEEPS TRACK OF ALL THE EXPANDED NODE, AND WHENEVER
WE GENERATE A NODE WE CHECK IF IT IS NOT ALREAD IN CLOSED
⢠Net effect depends on frequency of âloopsâ in state space.
52AI, Subash Chandra Pakhrin
53. Graph Search Algorithm
Let fringe be a list containing the initial state // FRINGE = OPEN
Let closed be initially empty // CLOSED WILL KEEP ALL THE EXPANDED NODES
LOOP
If fringe is empty return failure
Node <- remove - first (fringe)
If Node is a goal
then return the path from initial state to Node
else put Node in closed
generate all successors of Node S
For all nodes m in S
if m is not in closed
merge m into fringe
End LOOP
53AI, Subash Chandra Pakhrin
54. Uniform Cost Search (UCS)
⢠Modified version of BFS to make optimal.
⢠Enqueue nodes by path cost.
⢠Let g(n) = cost of the path from the start node to the current node n.
Sort nodes by increasing value of g.
Expand lowest cost node of the fringe.
Search continues by visiting the next node which has the least total
cost from the root.
⢠Complete
⢠Optimal / Admissible
⢠Exponential time and space complexity, O (b d)
54AI, Subash Chandra Pakhrin
55. Uniform Cost Search (UCS)
55AI, Subash Chandra Pakhrin
If we do Uniform Cost
Search (UCS) we can
find the minimum
path cost path from
start to goal.
56. Uniform Cost Search
4
1
341315
11
73612 161810
A
B C
D E F H
I J G1 K L M N G2
Note: Heurisitic
estimates are not
used in this search!
Paths from root
are generated.
4 11
A
B C
11
19
Since B has the least cost,
we expand it.
4
34
11
A
C
F H
1415
Node H has the least cost thus far, so we expand it.
4 11
A
B
4
C
11
A
0
Start with
root node
A.
1315
D E 17
B
19
1315
D E 17
Of our 3 choices, C
has the least cost so
weâll expand it.
4
34
11
A
C
F
15
B
19
1315
D E 17
1
N
H
7
G215
21
We have a goal, G2 but
need to expand other
branches to see if there is
another goal with less
distance.
4
34
11
A
C
F
B
19
1315
E 17
1
N
H
7
G2
15 21
36
L M
21 18
Note: Both
nodes F and N
have a cost of
15, we chose to
expand the
leftmost node
first. We
continue
expanding until
all remaining
paths are
greater than 21,
the cost of G2
AI, Subash Chandra Pakhrin 56
57. Uniform Cost Search
AI, Subash Chandra Pakhrin 57
S
A B
B D
C G
A C
D E
S
G
D
E
B
A
C
5
3
4
4
6
3
53 5
7
9 9
11
6
11 12 15
⢠Extend node that has shortest accumulated length from start node
to the current node.
Editor's Notes
Do not generate any states that was ever created before.
We need to maintain a list other than fringe in the algorithms: List CLOSED is a list