Search Strategies in AI:
Uninformed Search Strategies
Dr. Shiwani Gupta
A.P. COMP, I/C HoD AI&DS
TCET, Mumbai
[Link]
[Link]
[Link]
[Link]
[Link]
Example: Missionaries and Cannibals problem
(Three missionaries and three cannibals are on one side of a river, along with a boat that
can hold max two people. Find a way to get everyone to the other side, without ever
leaving a group of missionaries in one place outnumbered by the cannibals in that place)
States: a state consists of an ordered sequence of three numbers representing
the number of missionaries, cannibals, and boats on the bank of the river
from which they started. Thus, the start state is (3,3,1)
Operators: from each state the possible operators are to take either one
missionary, one cannibal, two missionaries, two cannibals, or one of each
across in the boat. Thus, there are at most five operators, although most states
have fewer because it is necessary to avoid illegal states.
Goal test: reached state (0,0,1)
Path cost: number of crossings
SHIWANI GUPTA 2
Missionaries and Cannibals: State Space
1 1
2 c c 2
c c
1 1 1 1
m m 2 2 m m
1c c c 1c
1 1
c c
1 1
c c
2 2
m m
1
m
1c
3
Crypt-Arithmetic puzzle
• Problem Statement:
– Solve the following puzzle by assigning numeral (0-9) in such a
way that each letter is assigned unique digit which satisfy the
following addition.
– Constraints : No two letters have the same value. (The constraints
of arithmetic).
S E N D
+ M O R E
_________________________________
M O N E Y
_________________________________
• Initial Problem State
– S = ? ; E = ? ;N = ? ; D = ? ; M = ? ;O = ? ; R = ? ;Y = ?
S E N D
Constraints: + M O R E
Y = D + E C1
E =
N =
N + R + C1
E + O + C2
C2
C3
_________________________________
O =
M =
S + M + C3
C4
C4
M O N E Y
Initial State _________________________________
M = 1 C4 = 1
O = 1 + S + C3
O
S = 9 S = 8
O
C3 = 0 C3 = 1
O = 0 O = 1
Fixed
M = 1
O = 0
N = E + O + C2 = E + C2 C2 = 1 (must) N = E + 1
E = 2 E = 3 ….. E = 5
N = 3 N = 6
E = N + R + C1 E = N + R + C1
10 + 2 = 3 + R + C1 10 + 5 = 6 + R + C1
O O
R = 9 R = 8 R = 9 R = 8
C1 =0 C1 = 1 C1 = 0 C1 = 1
O O
10 + Y = D + E = D + 2 10 + Y = D + E = D + 5
O O
D = 8 D = 9 D = 7
Y = 0 Y = 1 Y = 2
The first solution obtained is:
M = 1, O = 0, S = 9, E = 5, N = 6, R = 8, D = 7, Y = 2
Search Problem
• State space
– each state is an abstract representation of the
environment
– the state space is discrete
• Initial state
• Successor function
• Goal test
• Path cost
SHIWANI GUPTA
Search Problem
• State space
• Initial state:
– usually the current state
– sometimes one or several hypothetical states
(“what if …”)
• Successor function
• Goal test
• Path cost
SHIWANI GUPTA
Search Problem
• State space
• Initial state
• Successor function:
– [state subset of states]
– an abstract representation of the possible actions
• Goal test
• Path cost
SHIWANI GUPTA
Search Problem
• State space
• Initial state
• Successor function
• Goal test:
– usually a condition
– sometimes the description of a state
• Path cost
SHIWANI GUPTA
Search Problem
• State space
• Initial state
• Successor function
• Goal test
• Path cost:
– [path positive number]
– usually, path cost = sum of step costs
– e.g., number of moves of the empty tile
SHIWANI GUPTA
Assumptions in Basic Search
• The environment is static
• The environment is discretizable
• The environment is observable
• The actions are deterministic
SHIWANI GUPTA
Environment Types
• Static (vs. dynamic): If the environment can change while an agent
is deliberating, then we say the environment is dynamic for that
agent; otherwise it is static. Static environments are easy to deal
with because the agent need not keep looking at the world while it
is deciding on an action, nor need it worry about the passage of
time. If the environment does not change with the passage of time
but the agent's performance score does, then we say the
environment is semidynamic. eg. chess; taxi driving; timed chess
• Discrete (vs. continuous): If there are a limited number of distinct,
clearly defined percepts and actions we say that the environment is
discrete. eg. chess, checkers; taxi driving
SHIWANI GUPTA
Environment Types
• Accessible/ Fully observable (vs. inaccessible/ partially
observable): If an agent's sensory apparatus gives it access to the
complete state of the environment, then we say that the environment
is accessible to that agent. An accessible environment is convenient
because the agent need not maintain any internal state to keep track
of the world. eg. chess; playing cards, taxi driving
• Deterministic (vs. stochastic): If the next state of the environment is
completely determined by the current state and the actions selected
by the agents, then we say the environment is deterministic. In
principle, an agent need not worry about uncertainty in an
accessible, deterministic environment. If the environment is
inaccessible, however, then it may appear to be nondeterministic.
eg. timed chess; taxi driving, playing soccer
SHIWANI GUPTA
Search strategies
• A search strategy is defined by picking the order of node expansion
• Strategies are evaluated along the following dimensions:
– completeness: does it always find a solution if one exists?
– time complexity: time taken to find solution
– space complexity: maximum number of nodes in memory
– optimality: does it always find a least-cost solution?
SHIWANI GUPTA 14
Search strategies
• Time and space complexity are measured in terms of
– b: maximum branching factor (state expanded to yield new states)
of the search tree
– d: depth of the least-cost solution
– m: maximum depth of the state space (may be ∞)
SHIWANI GUPTA 15
Uninformed search strategies
(BLIND SEARCH)
Uninformed search strategies use only the information available in the
problem definition
• Breadth-first search
• Uniform-cost search
• Depth-first search
• Depth-limited search
• Iterative deepening search
• Bidirectional search
SHIWANI GUPTA 16
Real World problems
• Route Finding applications, such as routing in computer networks,
automated travel advisory systems, and airline travel planning systems.
• Touring and traveling salesperson problem is a famous touring
problem in which each city must be visited exactly once. The aim is to find
the shortest tour.
• VLSI layout with cell layout and channel routing.
• Robot navigation is a generalization of the route-finding problem. Rather
than a discrete set of routes, a robot can move in a continuous space with an
infinite set of possible actions and states.
• Assembly sequencing of complex objects by a robot, the problem is to
find an order in which to assemble the parts of some object. If the wrong
order is chosen, there will be no way to add some part later in the sequence
without undoing some of the work already done.
SHIWANI GUPTA 17
Example Problem: Map of Romania
SHIWANI GUPTA 18
Tree search example
SHIWANI GUPTA 19
Tree search example
SHIWANI GUPTA 20
Tree search example
SHIWANI GUPTA 21
Data structures for search trees
A node is a data structure with
five components:
• the state in the state space to
which the node corresponds
• the node in the search tree
that generated this node
(this is called the parent
A node is a bookkeeping data node)
structure used to represent the • the operator that was applied
search tree for a particular to generate the node
problem instance as generated • the number of nodes on the
by a particular algorithm. path from the root to this
A state represents a node (the depth of the node)
configuration (or set of • the path cost of the path from
configurations) of the world the initial state to the node
Thus, nodes have depths and
parents, whereas states do not.
SHIWANI GUPTA 23
Breadth-first search
• Expand shallowest unexpanded node
• Implementation:
– fringe is a FIFO queue, i.e., new successors go at end
SHIWANI GUPTA 24
Breadth-first search
• The root node is expanded first, then all the nodes generated by the
root node are expanded next, and then their successors, and so on.
• In general, all the nodes at depth d in the search tree are expanded
before the nodes at depth d + 1.
SHIWANI GUPTA 25
Breadth-first search
• If there is a solution, breadth-first search is guaranteed to find it
• if there are several solutions, breadth-first search will always find the
shallowest goal state first.
SHIWANI GUPTA 27
Properties of breadth-first search
• Complete? Yes
• Time? 1+b+b2+b3+… +bd = O(bd)
• Space? O(bd) (keeps every node in memory)
• Optimal? Yes
Memory requirements are a bigger problem for BFS than the execution time
(eg. for depth=6 nodes= time=18 min. memory=111MB
for depth=10 nodes= time=135 days memory=1TB)
Exponential complexity search problems can be solved for only small instances
SHIWANI GUPTA 28
BFS
Example
Application
• In peer-to-peer network like bit-torrent, BFS is used to
find all neighbor nodes
• Search engine crawlers use BFS to build index. Starting
from source page, it finds all links in it to get new pages
• Using GPS navigation system, BFS is used to find
neighboring places
• In networking, when we want to broadcast some packets,
we use the BFS algorithm
• Path finding algorithm is based on BFS or DFS
• BFS is used in Ford-Fulkerson algorithm to find maximum
flow in a network. eg. set of pipes moving water, traffic in
N/W
Application
• You can use a breadth-first search to identify persons within a certain
distance 'd' from a person in social networks up to 'd's levels.
• Cheney's technique uses the breadth-first search for duplicating
garbage collection. Because of the better locality of reference, breadth-
first search is favored over the Depth First Search algorithm.
• Cycle detection in undirected graphs can be done using either Breadth-
First Search or Depth First Search. BFS can also be used to find cycles
in a directed graph.
• Identifying routes: To see if there is a path between two vertices, you
can use either Breadth-First or Depth First Traversal.
• Finding all nodes within one connected component: To locate all
nodes reachable from a particular node, you can use either Breadth-
First or Depth First Traversal.
Advantage of BFS
• BFS will provide a solution if any solution exists.
• If there are more than one solution for a given problem, then BFS
will provide the optimal solution which requires the least number
of steps.
SHIWANI GUPTA 33
Disadvantage of BFS
• It can be slow since it expands all the nodes at each level before
moving on to the next level.
• Breadth-first search only returns an optimal path if all edges have
the same cost.
• It requires lots of memory since each level of the tree must be
saved into memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root
node.
SHIWANI GUPTA 34
Uniform-cost search
• Expand least-cost unexpanded node on fringe as measured by path
cost
• Breadth First Search expands least-depth node
• Equivalent to breadth-first if all step costs are equal
• The first solution that is found is guaranteed to be the cheapest
solution, because if there was a cheaper path that was a solution, it
would have been expanded earlier, and thus would have been
found first
• Uniform cost search finds the cheapest solution provided: the cost
of a path must never decrease as we go along the path
SHIWANI GUPTA 35
Uniform-cost search
In this algorithm from the starting state we will visit the adjacent states and
will choose the least costly state then we will choose the next least costly
state from the all un-visited and adjacent states of the visited states, in this
way we will try to reach the goal state even if we reach the goal state we
will continue searching for other possible paths (if there are multiple
goals) . We will keep a priority queue which will give the least costliest
next state from all the adjacent states of visited states.
SHIWANI GUPTA 36
Example
Advantage
• Uniform cost search is optimal because at
every state the path with the least cost is
chosen.
Disadvantage
• It does not care about the number of
steps involved in searching and only
concerned about path cost. Due to which
this algorithm may be stuck in an infinite
loop.
Depth-first search
• Expand deepest unexpanded node
• Implementation:
fringe = LIFO queue, i.e., put successors at front
SHIWANI GUPTA 40
Depth-first search
• Depth-first search always expands one of the nodes at the deepest
level of the tree. Only when the search hits a dead end (a nongoal
node with no expansion) does the search go back and expand nodes
at shallower levels.
SHIWANI GUPTA 41
Depth-first search
• Depth-first search can get stuck going down the wrong path.
• Many problems have very deep or even infinite search trees, so
depth-first search will never be able to recover from an unlucky
choice at one of the nodes near the top of the tree.
SHIWANI GUPTA 42
Depth-first search
• The search will always continue downward without backing up, even
when a shallow solution exists.
• Thus, on these problems depth-first search will either get stuck in an
infinite loop and never return a solution, or it may eventually find a
solution path that is longer than the optimal solution.
SHIWANI GUPTA 43
Depth-first search
• That means depth-first search is neither complete nor optimal.
• Because of this, depth-first search should be avoided for search trees
with large or infinite maximum depths.
SHIWANI GUPTA 44
Depth-first search
SHIWANI GUPTA 45
Depth-first search
• It is also common to implement depth-first search with a recursive
function that calls itself on each of its children in turn.
• In this case, the queue is stored implicitly in the local state of each
invocation on the calling stack.
SHIWANI GUPTA 46
Depth-first search
• Complete? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
complete in finite spaces
SHIWANI GUPTA 47
Depth-first search
• Time? O(bm): terrible if maximum depth m is much larger than
branching factor b.
For problems that have many solutions, depth-first may actually be
faster than breadth-first, because it has a good chance of finding a
solution after exploring only a small portion of the whole space.
SHIWANI GUPTA 48
Depth-first search
• Space? O(bm), i.e., linear space!
Depth-first search needs to store only a single path from the root to a
leaf node, along with the remaining unexpanded sibling nodes for
each node on the path.
SHIWANI GUPTA 49
Depth-first search
• Optimal? No
SHIWANI GUPTA 50
Depth-first search
SHIWANI GUPTA 51
Application
• If we perform DFS on unweighted graph, then it will create
Minimum Spanning Tree for all pair shortest path tree
• We can detect cycles in a graph using DFS. If we get one back-
edge during DFS, then there must be one cycle.
• Using DFS we can find path between two given vertices u and v.
• We can perform topological sort to schedule jobs from given
dependencies among jobs.
• Using DFS, we can find strongly connected components of a
graph. If there is a path from each vertex to every other vertex,
that is strongly connected.
Application
• Solving puzzles with only one solution, such as mazes. (DFS can be adapted
to find all solutions to a maze by only including nodes on the current path in
the visited set.)
• Web crawlers: Depth-first search can be used in the implementation of web
crawlers to explore the links on a website.
• Maze generation: Depth-first search can be used to generate random mazes.
• Backtracking: Depth-first search can be used in backtracking algorithms.
• Bipartite Graph: We can augment either BFS or DFS when we first
discover a new vertex, color it opposite its parents, and for each other edge,
check it doesn’t link two vertices of the same color. The first vertex in any
connected component can be red or black!
Advantage of DFS
• DFS requires very less memory as it only
needs to store a stack of the nodes on the path
from root node to the current node.
• It takes less time to reach to the goal node than
BFS algorithm (if it traverses in the right path).
Disadvantage of DFS
• There is the possibility that many states keep
re-occurring, and there is no guarantee of
finding the solution.
• DFS algorithm goes for deep down searching
and it may sometime go to the infinite loop.
Example
DFS
TASK
Depth-limited search
• Avoids the pitfalls of depth-first search by imposing a cutoff on the
maximum depth of a path.
• Depth-limited search = depth-first search with depth limit l
i.e., nodes at depth l have no successors
• The time and space complexity of depth-limited search is similar to
depth-first search. It takes time and O(bl) space, where l is the
depth limit.
SHIWANI GUPTA 59
Advantage
• Depth-limited search is Memory efficient.
Disadvantage
• Depth-limited search also has a disadvantage
of incompleteness.
• It may not be optimal if the problem has more
than one solution.
Example
Example Problem: Map of Romania
SHIWANI GUPTA 63
Example: Map of Romania
• There are 20 cities, so we know that if there is a solution, then it must be
of length 19 at the longest.
• We can implement the depth cutoff using operators of the form "If you
are in city A and have traveled a path of less than 19 steps, then generate a
new state in city B with a path length that is one greater."
• With this new operator set, we are guaranteed to find the solution if it
exists, but we are still not guaranteed to find the shortest solution
• Depth-limited search is complete but not optimal.
• If we choose a depth limit that is too small, then depth-limited search is
not even complete.
SHIWANI GUPTA 64
Depth First Iterative deepening search
• We picked 19 as an "obvious“ depth limit for the Romania problem,
but in fact if we studied the map carefully, we would discover that
any city can be reached from any other city in at most 9 steps.
• This number, known as the diameter of the state space, gives us a
better depth limit, which leads to a more efficient depth-limited
search.
• It sidesteps the issue of choosing the best depth limit by trying all
possible depth limits: first depth 0, then depth 1, then depth 2, and so
on.
SHIWANI GUPTA 65
Depth First Iterative deepening search
• In effect, iterative deepening combines the benefits of depth-first
search and breadth-first search.
•It is optimal and complete, like breadth-first search, but has
only the modest memory requirements of depth-first search.
•The order of expansion of states is similar to breadth-first, except that
some states are expanded multiple times.
More efficient than DFS but less than BFS
SHIWANI GUPTA 66
Depth First Iterative deepening search l =0
SHIWANI GUPTA 67
Depth First Iterative deepening search l =1
SHIWANI GUPTA 68
Depth First Iterative deepening search l =2
SHIWANI GUPTA 69
Depth First Iterative deepening search l =3
SHIWANI GUPTA 70
Depth First Iterative deepening search
• Number of nodes generated in a depth-limited search to depth d with
branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
• Number of nodes generated in an iterative deepening search to depth
d with branching factor b:
NIDS = (d+1)b0 + d b1 + (d-1)b2 + … + 3bd-2 +2bd-1 + 1bd
• For b = 10, d = 5,
– NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
– NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456
• Overhead = (123,456 - 111,111)/111,111 = 11%
SHIWANI GUPTA 71
Properties of Depth First iterative deepening
search
• Complete? Yes
• Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
• Space? O(bd)
• Optimal? Yes, if step cost = 1
Iterative deepening is the preferred search method when there is a
large search space and the depth of the solution is not known.
SHIWANI GUPTA 72
Advantage of IDDFS
• It combines the benefits of BFS and DFS
search algorithm in terms of fast search and
memory efficiency.
Drawback of IDDFS
• The time taken is exponential to reach the goal
node.
• The main problem with IDDFS is the time and
wasted calculations that take place at each
depth.
• The situation is not as bad as we may think of
especially when the branching factor is found
to be high.
Bidirectional Search
Bidirectional search replaces single search graph (which is likely to
grow exponentially) with two smaller sub graphs – one starting from
initial vertex and other starting from goal vertex. The search terminates
when two graphs intersect.
• Forward search from source/initial vertex toward goal vertex
• Backward search from goal/target vertex toward source vertex
Bidirectional Search
• In many cases it is faster, it dramatically reduce the amount of
required exploration.
• Suppose if branching factor of tree is b and distance of goal
vertex from source is d, then the normal BFS/DFS search
complexity would be O(bd).
• On the other hand, if we execute two search operation then the
complexity would be O(bd/2) for each search and total complexity
would be O(bd/2 +bd/2) which is far less than O(bd).
Bidirectional Search
We can consider bidirectional approach when-
• Both initial and goal states are unique and completely defined.
• The branching factor is exactly the same in both directions.
Performance measures
Completeness: Bidirectional search is complete if BFS is used in both
searches.
Optimality: It is optimal if BFS is used for search and paths have
uniform cost.
Time and Space Complexity: Time and space complexity is O(bd/2).
Advantage of Bidirectional search
• Less time consuming as the shortest path
length is used for the search.
Disadvantage of Bidirectional Search
• The goal node has to be known before the
search can be done simultaneously in both
directions.
Summary of algorithms
b is the branching factor
d is the depth of solution
m is the maximum depth of the search tree
/ is the depth limit.
SHIWANI GUPTA 80