Search, Uninformed Algo
Search, Uninformed Algo
computer. For example, AI is used to recognize faces in photographs on your social media, beat
the World’s Champion in chess, and process your speech when you speak to Siri or Alexa on
your phone.
Important Terms:
m
● Search: Finding a solution to a problem, like a navigator app that finds the best route
from your origin to the destination, or like playing a game and figuring out the next
co
move.
● Knowledge:Representing information and drawing inferences from it.
● Uncertainty: Dealing with uncertain events using probability.
● Optimization: Finding not only a correct way to solve a problem, but a better—or the
e.
best—way to solve it.
● Learning: Improving performance based on access to data and experience. For example,
al
your email is able to distinguish spam from non-spam mail based on past experience.
● Neural Networks: A program structure inspired by the human brain that is able to
ir
perform tasks effectively.
● Language: Processing natural language, which is produced and understood by humans.
wa
Search:
In which we see how an agent can find a sequence of actions that achieves its goals
sh
m
co
e.
al
ir
wa
sh
yu
Agent:
An entity that perceives its environment and acts upon that environment. In a navigator app, for
example, the agent would be a representation of a car that needs to decide on which actions to
take to arrive at the destination.
State:
A configuration of an agent in its environment. For example, in a 15 puzzle, a state is any one
way that all the numbers are arranged on the board.
● Initial State:
The state from which the search algorithm starts. In a navigator app, that would be the
current location.
Actions:
m
Choices that can be made in a state. More precisely, actions can be defined as a function. Upon
receiving state s as input, Actions(s) returns as output the set of actions that can be executed in
state s. For example, in a 15 puzzle, the actions of a given state are the ways you can slide
co
squares in the current configuration (4 if the empty square is in the middle, 3 if next to a side, 2 if
in the corner).
e.
Transition Model:
A description of what state results from performing any applicable action in any state. More
al
precisely, the transition model can be defined as a function. Upon receiving state s and action a
as input, Results(s, a) returns the state resulting from performing action a in state s. For example,
given a certain configuration of a 15 puzzle (state s), moving a square in any direction (action a)
ir
will bring to a new configuration of the puzzle (the new state).
wa
State Space
The set of all states reachable from the initial state by any sequence of actions. For example, in a
15 puzzle, the state space consists of all the 16!/2 configurations on the board that can be reached
sh
from any initial state. The state space can be visualized as a directed graph with states,
represented as nodes, and actions, represented as arrows between nodes.
yu
pi
m
co
e.
al
ir
Goal Test
The condition that determines whether a given state is a goal state. For example, in a navigator
wa
app, the goal test would be whether the current location of the agent (the representation of the
car) is at the destination. If it is — problem solved. If it’s not — we continue searching.
Path Cost
sh
A numerical cost associated with a given path. For example, a navigator app does not simply
bring you to your goal; it does so while minimizing the path cost, finding the fastest way possible
for you to get to your goal state.
yu
Solution:A sequence of actions that leads from the initial state to the goal state.
● Optimal Solution: A solution that has the lowest path cost among all solutions.
In a search process, data is often stored in a node, a data structure that contains the following
data:
● A state
● Its parent node, through which the current node was generated
● The action that was applied to the state of the parent to get to the current node
● The path cost from the initial state to this node
Nodes contain information that makes them very useful for the purposes of search algorithms.
They contain a state, which can be checked using the goal test to see if it is the final state. If it is,
the node’s path cost can be compared to other nodes’ path costs, which allows choosing the
m
optimal solution. Once the node is chosen, by virtue of storing the parent node and the action that
led from the parent to the current node, it is possible to trace back every step of the way from the
initial state to this node, and this sequence of actions is the solution.
co
However, nodes are simply a data structure — they don’t search, they hold information. To
actually search, we use the frontier, the mechanism that “manages” the nodes. The frontier starts
e.
by containing an initial state and an empty set of explored items, and then repeats the following
actions until a solution is reached:
al
ir
wa
sh
yu
pi
m
co
e.
al
ir
Connect with me:
wa
😄
potentially result in them securing a seat at IIT instead
pi
of you.
In order to deal with various types of search problems, we have search algorithms, let us discuss
them one by one.
m
co
e.
al
ir
wa
sh
Before we get into the design of specific search algorithms, we need to consider the criteria that
might be used to choose among them. We can evaluate an algorithm’s performance in four ways:
pi
m
search strategies are distinguished by the order in which nodes are expanded.
co
1. Breadth-first Search
2. Depth-first Search
e.
3. Depth-limited Search
4. Iterative deepening depth-first search
al
5. Uniform cost search
6. Bidirectional Search
ir
1.1 Breadth-first search:
wa
● It ensures optimality in terms of finding the shallowest goal node, but it may not always
find the optimal solution in terms of the least-cost path if edge costs are not uniform.
● The algorithm generates all shallower nodes before deeper ones, making it inefficient in
yu
● Breadth-first search applies the goal test when a node is generated, ensuring that any
found goal node is indeed the shallowest one, eliminating the need to compare multiple
shallow goal nodes.
Algorithm of Breadth-first search:
1. Start with an initial node containing the starting state of the problem and a path cost of 0.
2. Check if the initial state is a goal state. If it is, you're done, and you have a solution.
3. Create a queue (think of it as a line) called the "frontier" and add the initial node to it.
4. Create an empty set called "explored" to keep track of the states we've already seen.
5. Now, repeat the following steps:
a. If the frontier is empty (there's no one left in the line), and you haven't found a
solution, then you've failed to find a solution. So, return failure.
b. Take the node at the front of the line (the shallowest one).
m
c. Add the state of this node to the explored set, so we remember we've seen it.
d. For each possible action you can take from the current state:
co
i. Create a new node that represents the result of taking that action.
6. Keep repeating these steps until you either find a solution or determine that there is no
solution (if the frontier becomes empty).
e.
al
ir
wa
sh
yu
pi
co
S000
0110
010G
e.
0000
al
In this maze:
ir
● S: Starting point
● G: Goal
● 0: An open path
wa
-Frontier: [S]
-Explored: []
2. Take the first node from the frontier (S), mark it as explored, and check its neighboring
nodes.
yu
-Frontier: []
-Explored: [S]
Neighbors of S:
pi
m
a. Move down (to 1,1)
b. Move right (to 0,3)
c. Move down (to 2,2)
co
d. Move right (to 1,3)
e. Move down (to G)
Frontier: [1,0, 0,2, 1,1, 0,3, 2,2, 1,3, G]
e.
Explored: [S, 0,1, 1,0, 0,2, 1,1, 0,3, 2,2]
7. Once you've reached the goal node (G), you have found a solution.
al
The path from S to G is: S -> (1,0) -> (0,2) -> (1,1) -> (0,3) -> (2,2) -> (1,3) -> G.
BFS guarantees that you find the shortest path in terms of the number of steps, and it
systematically explores all possible paths before returning a solution.
ir
wa
Performance:
Completeness: A search algorithm is complete if it guarantees finding a solution if one exists.
Optimality: An optimal search algorithm finds the shortest path to the goal.
sh
Time Complexity:
𝑑
● In the worst case, BFS also has a time complexity of O(𝑏 )(Exponential).
yu
● However, BFS systematically explores all nodes at each depth level before moving to the
next level.
pi
● BFS can consume significant memory, especially when the branching factor is high or the
tree is deep.
● It guarantees that it explores the shallowest nodes first, but this comes at the cost of
m
increased memory usage.
co
Where b = branching factor (require finite b) d = depth of shallowest solution
e.
● Uninformed search algorithms are suitable for small to moderately sized search spaces
al
where domain-specific knowledge is limited or unavailable.
● They are a good choice when you need a simple, general-purpose approach to
ir
problem-solving.
wa
Limitations:
unnecessary exploration.
yu
pi
1.2 Depth-First Search:
● Depth-First Search (DFS) is a complete search algorithm that explores as far as possible
along a branch of the search tree before backtracking.
● It may not guarantee optimality in terms of finding the shortest path, especially when
edge costs are not uniform.
● DFS tends to generate a deeper search tree before exploring shallower nodes, which can
be more memory-efficient than BFS for deep search trees.
● It uses a Last-In-First-Out (LIFO) stack for managing the frontier, which means it
m
explores one branch entirely before moving on to the next.
● The goal test is applied when a node is selected for expansion, which may lead to
co
multiple solutions.
e.
1. Start with an initial node containing the starting state of the problem and a path cost of 0.
2. Check if the initial state is a goal state. If it is, you've found a solution.
al
3. Create a stack (think of it as a stack of plates) called the "frontier" and add the initial
node to it.
ir
4. Create an empty set called "explored" to keep track of the states we've already seen.
5. Now, repeat the following steps:
a. If the frontier is empty, return failure.
wa
b. Take the node at the top of the stack (the deepest one).
c. Add the state of this node to the explored set.
d. For each possible action you can take from the current state:
sh
i. Create a new node that represents the result of taking that action.
e. Add the neighboring nodes to the stack.
6. Keep repeating these steps until you either find a solution or determine that there is no
solution (if the frontier becomes empty).
yu
pi
pi
yu
sh
wa
ir
al
e.
co
m
Depth-First Search on a Graph:
For example-
m
co
e.
Starting from S, DFS explores as far down a branch as possible before backtracking:
al
● S -> A -> D -> G
● S -> A -> E
ir
● S -> B -> C
wa
Performance:
Completeness: Yes (if the search tree is finite).
Optimality: No (it may not find the shortest path).
sh
Time Complexity
𝑑
● In the worst case, DFS can have a time complexity of O(𝑏 ) (Exponential)
● If the search tree is deep and has a high branching factor, DFS can take a long time to
yu
find a solution.
● DFS can perform relatively well when the solution is shallow or located early in the
search space (small d).
pi
Space Complexity:
● The space complexity of DFS is typically O(bd) (Linear), which accounts for the
maximum depth of the search tree.
● It uses memory proportional to the depth of the tree because it needs to store all nodes
along the current branch in the stack.
● DFS can be memory-efficient when the depth is limited, but it may run into memory
issues with deep search trees.
Advantages of Depth-First Search:
1. Memory Efficiency: DFS can be more memory-efficient than BFS for deep search trees
because it explores deeply before moving to the next branch.
2. Simplicity: Like BFS, DFS is conceptually simple and easy to implement.
3. Solves Uninformed Problems: It's suitable for solving uninformed search problems
where the goal is to find any valid solution.
m
Disadvantages of Depth-First Search:
co
1. Completeness and Optimality: It may not find a solution in infinite or cyclical graphs
and doesn't guarantee optimality.
2. Lack of Guidance: DFS doesn't use heuristic information, which can make it less
e.
efficient than informed search algorithms in some cases.
3. Potential Infinite Loops: In infinite graphs or graphs with cycles, DFS may get stuck in
al
infinite loops without reaching a solution.
4. Not Suitable for Shortest Path: It doesn't prioritize finding the shortest path, making it
unsuitable for certain problems.
ir
wa
.
sh
yu
pi
Analysis of BFS and DFS
Behaviour:
m
co
e.
al
When the two searches pick the fifth node, coloured green, they have explored different parts of
ir
the search tree, expanding the four nodes before in the order shown. (Discussed in Lectures)
wa
Space:
sh
yu
pi
Completeness:
1. When Search space is finite:
● Both DFS and BFS are complete for finite state spaces.
● Both are systematic
● They will explore the entire search space before reporting the failure. This
is because the termination criteria for both is same.
2. When Search space is infinite:
● If the state space is infinite, but with finite branching then DFS may go
m
down to an infinite path and not terminate.
● BFS will find a solution if there exists one.
● If there is no solution , both algorithm will not terminate for infinite spaces
co
Running Time
e.
al
ir
wa
sh
yu
pi
From above, we can say that the problems with large branching factor (b) both
sh
Summary:
yu
exponentially
● BFS is superior when the GOAL exists in the upper right portion of a search tree.
● BFS gives the optimal solution.
● DFS is effective when there are few sub trees in the search tree that have only
one connection to the rest of the states.
● DFS is best when the GOAL exists in the lower left portion of the search tree.
● DFS can be dangerous when the path closer to the START and farther from the
GOAL has been chosen.
● DFS is memory efficient as the path from start to current node is stored. Each
node should contain a state and its parent.
● DFS may not give an optimal solution.
m
2. It uses a depth limit parameter to determine how deep into the tree or graph it can explore
3. Depth-limited search alleviates the infinite-state space problem by setting a
predetermined depth limit.
co
4. It solves the infinite-path problem but can be incomplete if the shallowest goal is beyond
the limit.
5. Depth-limited search becomes nonoptimal if the depth limit is greater than the depth to
e.
the goal state.
𝑑
6. Its time complexity is O(𝑏 ), and its space complexity is O(bd), where b is the branching
al
factor and d is the depth limit.
7. Depth-first search is a special case of depth-limited search with an infinite depth limit ( =
ir
∞).
wa
● When the depth limit is reached or a goal state is found, stop the search.
● If the goal state is not found, backtrack to the previous node and continue the search from
there.
yu
● Repeat steps 2-4 until the goal is reached or the entire search space is explored.
pi
m
co
e.
al
Consider the below infinite search space, S is start state and G is the Goal State.
ir
wa
sh
yu
pi
For example:
Suppose we have a simplified navigation map with cities and roads. We want to find a path from
the "Start" city to the "Goal" city, but we don't want to explore paths that are too long. We will
use DLS to limit the depth of exploration.
m
co
e.
al
ir
wa
sh
yu
● Start: A
● Goal: Goal
pi
We start at city A and use DLS to explore the map while limiting the depth to 3 levels.
● Start at A (Depth 0).
● Explore neighbors of A: B and C (Depth 1).
● Explore neighbors of B: D and E (Depth 2).
● Explore neighbors of C: F (Depth 2).
● Explore neighbors of D: G and H (Depth 3).
● Explore neighbors of E: (No neighbors at Depth 3).
● Explore neighbors of F: I and J (Depth 3).
Now, we have reached the depth limit of 3. Since the "Goal" city is not within this limit, we stop
the exploration.
The path from "Start" to "Goal" within the depth limit is: A -> C -> F -> J -> Goal.
In this example, Depth-Limited Search (DLS) limits the exploration to a certain depth level (in
this case, 3 levels) to efficiently find a solution within the specified depth constraint. If the goal
is not found within the depth limit, DLS stops the search.
m
Performance of DFS and DLS:
● Completeness: DFS is not complete in infinite or cyclical graphs, while DLS is not
co
complete if the depth limit is insufficient.
● Optimality: Neither DFS nor DLS guarantee optimality.
● Time complexity: Exponential: O(b^m) in the worst case (DFS), and linear in terms of
depth limit for DLS.
e.
● Space complexity: Linear in the maximum depth for both DFS and DLS.
al
ir
wa
sh
yu
pi
Difference between DFS and DLS:
m
co
e.
al
ir
wa
sh
yu
pi
1.4 Iterative deepening depth-first search:
1. IDS gradually increases the depth limit to find the best depth for searching until a goal is
found.
2. It combines benefits of depth-first and breadth-first search, offering modest memory
requirements and completeness.
3. IDS generates nodes at different levels multiple times but remains reasonably efficient.
4. In terms of node generation, it is asymptotically similar to breadth-first search (BFS).
5. IDS is preferred for large search spaces when the depth of the solution is unknown.
m
Iterative Deepening Depth-First Search, often abbreviated as IDDFS, is a search strategy that
co
combines the benefits of depth-first search and iterative deepening. It is particularly useful when
the depth of the solution is unknown.
e.
Algorithm of Iterative Deepening Depth-First Search:
1. Start with a depth limit of 0.
2. Apply Depth-Limited Search (DLS) with the current depth limit.
3.
4.
al
If DLS finds a solution, return it.
If DLS returns "cutoff," increment the depth limit and repeat from step 2.
ir
5. If DLS returns failure and all depth limits have been exhausted, return failure.
wa
sh
yu
pi
For Example-
A
/\
B C
/\ \
D E F
/\ /\
G H I J
Algorithm Execution:
m
1. Start with a depth limit of 0.
2. Apply Depth-Limited Search (DLS) with the current depth limit:
a. At depth limit 0, we explore only node A.
co
b. Since node A is not the goal, we increment the depth limit to 1.
3. Apply DLS with depth limit 1:
a. At depth limit 1, we explore nodes A, B, and C.
e.
b. Node C is not the goal, so we continue.
c. We increment the depth limit to 2.
al
4. Apply DLS with depth limit 2:
a. At depth limit 2, we explore nodes A, B, C, D, E, and F.
b. Node F is not the goal, so we continue.
ir
c. We increment the depth limit to 3.
5. Apply DLS with depth limit 3:
wa
gradually increases it until it reaches the depth where the goal node is located. This strategy
allows IDDFS to control memory usage while systematically searching deeper levels of the tree.
yu
pi
m
co
e.
al
ir
wa
Performance:
sh
● Completeness: IDDFS is complete as long as the depth limit reaches or exceeds the
depth of the shallowest goal node.
● Optimality: Like DFS, IDDFS does not guarantee optimality; it may find a solution, but
it might not be the optimal one.
yu
● Time Complexity: The time complexity is O(b^d), where b is the branching factor and d
is the depth of the shallowest goal.
● Space Complexity: The space complexity is O(b*d), where b is the branching factor and
pi
Advantages of IDDFS:
1. Completeness: IDDFS guarantees finding a solution if one exists.
2. Memory Efficiency: It has relatively modest memory requirements compared to some
other search algorithms.
3. Simplicity: Conceptually easy to understand and implement.
4. Versatility: Suitable for problems with unknown solution depths.
Disadvantages of IDDFS:
1. Lack of Optimality: IDDFS does not guarantee finding the optimal solution.
2. Repetitive Node Generation: Nod
3. es at different levels may be generated multiple times.
4. Inefficiency for Large Branching: Becomes inefficient with high branching factors.
5. Limited Heuristic Use: Like DFS, it doesn't incorporate heuristic information for
efficient search.
m
1.5 Uniform cost search:
co
Uniform Cost Search, often abbreviated as UCS, is a search algorithm that aims to find the
optimal solution by exploring the lowest-cost paths in a weighted graph.
e.
Algorithm of Uniform Cost Search:
al
1. Start with an initial node containing the starting state of the problem and a path cost of 0.
2. Create a priority queue called the "frontier" and add the initial node to it with a priority of
0.
ir
3. Create an empty set called "explored" to keep track of the states we've already seen.
4. Repeat the following steps:
wa
a. If the frontier is empty (there's no one left in the queue), and you haven't found a
solution, then you've failed to find a solution. Return failure.
b. Take the node with the lowest path cost from the frontier.
c. If the state of this node is a goal state, you've found a solution. Return it.
sh
d. Add the state of this node to the explored set, so we remember we've seen it.
e. For each possible action you can take from the current state:
i. Create a new node that represents the result of taking that action.
yu
ii. If the new state is not in the explored set, calculate its path cost and add it to the
frontier with the calculated cost as its priority.
pi
Example:
Suppose you want to find the cheapest route from City A to City G on a road network with
different road segments having varying costs. The road network can be represented as a weighted
graph, where the cities are nodes, and the roads between them are edges with associated costs.
m
co
e.
al
ir
wa
sh
UCS Execution:
1. Start at City A with a path cost of 0.
2. Create a priority queue (frontier) and add the initial node (A) with a priority of 0 to the
pi
frontier.
3. Initialize an empty set (explored) to keep track of visited cities.
4. Perform the following steps: a. Take the node with the lowest path cost from the frontier
(initially, A with cost 0). b. Check if the current city is the goal (G). If yes, you've found a
solution. c. If not, mark the current city as explored and expand it:
a. From A, you can go to B (cost 1) or C (cost 5). Add B and C to the frontier.
b. Explored: [A], Frontier: [B(1), C(5)] d. Select the node with the lowest cost from
the frontier (B with cost 1).
c. Expand B to D (cost 2) and E (cost 3). Add them to the frontier.
d. Explored: [A, B], Frontier: [C(5), D(3), E(4)] e. Continue this process, always
selecting the lowest-cost node from the frontier:
e. Expand D to G (cost 3). Add G to the frontier.
f. Expand E to H (cost 6). Add H to the frontier.
g. Expand C to F (cost 4). Add F to the frontier. f. Select the lowest-cost node (D
with cost 3) from the frontier:
h. Expand G to H (cost 5) and I (cost 8). Add H to the frontier. g. Continue
exploring:
m
i. Expand H to I (cost 7).
j. Expand F to I (cost 10). h. Select the lowest-cost node (G with cost 3) from the
frontier:
co
k. You've reached the goal (City G) with a cost of 3.
Solution:
The cheapest route from City A to City G is through the following cities with the associated
e.
costs: A -> B -> D -> G, with a total cost of 3.
al
Uniform Cost Search (UCS) systematically explores paths with increasing costs and finds the
optimal solution in terms of the lowest cost. In this example, it successfully determined the most
cost-effective route.
ir
Performance:
wa
● Completeness: UCS is complete if the edge costs are non-negative. It will find the
optimal solution.
● Optimality: UCS guarantees finding the optimal solution by exploring the lowest-cost
sh
paths first.
● Time Complexity: The time complexity depends on the graph structure but is generally
O(b^d), where b is the branching factor and d is the depth of the optimal solution.
yu
● Space Complexity: The space complexity depends on the number of nodes in the
frontier, which can be high in some cases.
Advantages of UCS:
pi
1. Optimality: UCS is guaranteed to find the optimal solution with non-negative edge
costs.
2. Precision: It considers the exact cost of each path, making it suitable for cost-sensitive
problems.
3. Versatility: UCS can handle a wide range of problem domains with weighted graphs.
4. Minimal Memory Usage: Uses memory efficiently compared to some other search
algorithms.
Disadvantages of UCS:
1. Inefficient for Large Costs: If edge costs are very high, UCS can be slow and
memory-intensive.
2. Not Suitable for Negative Costs: UCS cannot handle graphs with negative edge costs.
3. Complexity: The time complexity can be high in graphs with many branches.
m
1. Bidirectional search runs two simultaneous searches from the initial state and the goal
state, aiming to meet in the middle, which can significantly reduce the search space.
co
2. The goal test in bidirectional search checks whether the frontiers of the two searches
intersect, and if they do, a potential solution is found.
3. The first solution encountered in bidirectional search may not always be optimal,
e.
requiring additional search to confirm.
𝑑/2
4. In the worst case, bidirectional search generates a total of 𝑏 nodes when both
al
directions use breadth-first search, reducing time complexity compared to unidirectional
breadth-first search.
ir
𝑑/2
5. The space complexity of bidirectional search is O(𝑏 ), with the need to keep at least
one of the frontiers in memory for intersection checking, which is a notable limitation.
wa
information about both the start and goal states and want to reduce the search space.
Algorithm:
yu
1. Start with two search frontiers: one from the start state and one from the goal state.
2. Initialize two sets: one for explored nodes in the forward direction (from start) and one
for explored nodes in the backward direction (from the goal).
3. While neither of the frontiers is empty:
pi
m
co
e.
Example:
al
ir
Consider a simple bidirectional search example on a graph with cities:
wa
sh
● Start state: A
yu
● Goal state: F
Execution:
1. Initialize two frontiers: one from A (start) and one from F (goal).
pi
m
𝑑/2
2. Space Efficiency: The space complexity of bidirectional search is O(𝑏 ), which is
considerably lower than the O(bd) space complexity of standard breadth-first search. This
co
makes it more memory-efficient, especially for large search spaces.
3. Optimality Trade-off: While bidirectional search can find a potential solution faster, the
first solution encountered may not be optimal. Additional exploration is often required to
e.
confirm optimality.
4. Effective for Unknown Depths: Bidirectional search is well-suited for situations where
al
the depth of the solution is unknown. It combines the benefits of breadth-first search with
reduced memory requirements.
5. Limited Memory: Despite its efficiency, bidirectional search still requires maintaining at
ir
least one frontier in memory for intersection checking. In cases with severe memory
constraints, this can be a limitation.
wa
Advantages:
● Can be much faster than traditional search algorithms, especially in large search spaces.
● Reduces the search space by simultaneously exploring from both ends.
sh
Disadvantages:
● Requires bidirectional information (knowledge of both the start and goal states).
● Complexity and overhead of managing two frontiers and explored sets.
pi
● May not always be more efficient, depending on the specific problem and graph structure.
❖ Comparing uninformed search strategies:
m
co
e.
al
References:
1. FIRST COURSE IN ARTIFICIAL INTELLIGENCE. Deepak Khemani.
ir
2. Artificial Intelligence: A Modern Approach Textbook by Peter Norvig and Stuart
J. Russell
wa