You are on page 1of 20

06-02-2024

Artificial Intelligence
(BITE308L)

Uninformed Search
Algorithms

Dr. S. Hemalatha
School of Computer Science Engineering & Information Systems
VIT, Vellore

Uninformed Search Algorithms

• Breadth-first Search
• Depth-first Search
• Uniform cost search
• Depth-limited Search
• Iterative deepening depth-first search
• Bidirectional Search

Prepared by S.Hemalatha/SCORE

1
06-02-2024

Breadth-first Search (BFS)


• A simple strategy in which the root node is expanded first
– then all the successors of the root node are expanded next
– then their successors, and so on
• BFS algorithm searches breadthwise in a tree or graph
– All the nodes are expanded at a given depth in the search tree before any nodes at the
next level are expanded
• BFS - an example of a general-graph search algorithm
• Implemented using FIFO queue data structure

Prepared by S.Hemalatha/SCORE

Bfs - algorithm

Prepared by S.Hemalatha/SCORE

2
06-02-2024

BFS – example

Prepared by S.Hemalatha/SCORE

BFS – example (book)

Prepared by S.Hemalatha/SCORE

3
06-02-2024

BFS – time complexity


• Basically TC of BFS: 𝑶 𝑽 + 𝑬
• Imagine searching a uniform tree where every state has b successors
– The root of the search tree generates 𝑏 nodes at the first level
– each of which generates b more nodes, for a total of 𝑏 at the second level
– Each of these generates b more nodes, yielding 𝑏 nodes at the third level
• Now suppose that the solution is at depth 𝑑
– In the worst case, it is the last node generated at that level
– Then the total number of nodes generated is
𝒃 + 𝒃𝟐 + 𝒃𝟑 + ⋯ + 𝒃𝒅 = 𝑶 𝒃𝒅
• If the algorithm were to apply the goal test to nodes when selected for expansion,
rather than when generated, the whole layer of nodes at depth d would be expanded
before the goal was detected and the time complexity would be
𝑶 𝒃𝒅 𝟏

Prepared by S.Hemalatha/SCORE

BFS – time complexity

• Space Complexity:
– Space complexity of BFS algorithm is given by the Memory size of frontier which is
𝑶 𝒃𝒅
• Completeness:
– BFS is complete, which means if the shallowest goal node is at some finite depth, then
BFS will find a solution.
• Optimality:
– BFS is optimal if path cost is a non-decreasing function of the depth of the node

Prepared by S.Hemalatha/SCORE

4
06-02-2024

BFS – advantages & disadvantages


• Advantages:
– BFS will provide a solution if any solution exists
– If there are more than one solutions for a given problem, then BFS will provide the
minimal solution which requires the least number of steps.

• Disadvantages:
– 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.

Prepared by S.Hemalatha/SCORE

Depth first search - DFS


• DFS always expands the deepest node in the current frontier of the search tree
• A recursive algorithm for traversing a tree or graph data structure
• It is called the depth-first search because it starts from the root node and follows
each path to its greatest depth node before moving to the next path
• DFS uses a stack data structure for its implementation
• The process of the DFS algorithm is similar to the BFS algorithm

Note:
• Backtracking is an algorithm technique for finding all possible solutions using
recursion

Prepared by S.Hemalatha/SCORE

5
06-02-2024

dfs - algorithm

Prepared by S.Hemalatha/SCORE

DFS – example

Prepared by S.Hemalatha/SCORE

6
06-02-2024

dFS – example (book)

Prepared by S.Hemalatha/SCORE

dFS – time complexity


• Basically TC of BFS: 𝑶 𝑽 + 𝑬
• Time Complexity:
– Time complexity of DFS will be equivalent to the node traversed by the algorithm
𝑻 𝒏 = 𝟏 + 𝒏 + 𝒏𝟐 + 𝒏𝟑 + ⋯ + 𝒏𝒎 = 𝑶 𝒏𝒎
– 𝒎 - maximum depth of any node and this can be much larger than d (Shallowest solution
depth)
• Completeness:
– DFS search algorithm is complete within finite state space as it will expand every node within a
limited search tree

Prepared by S.Hemalatha/SCORE

7
06-02-2024

dFS – time complexity


• Space Complexity: 𝑶 𝒃𝒎
– Equivalent to the size of the fringe set
– DFS algorithm needs to store only single path from the root node

• Optimality:
– Considered to be non-optimal
• As it may generate a large number of steps or high cost to reach to the goal node

Prepared by S.Hemalatha/SCORE

dFS – advantages & disadvantages

• Advantages:
– 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

• Disadvantages:
– 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 sometime it may go to the
infinite loop

Prepared by S.Hemalatha/SCORE

8
06-02-2024

Depth Limited Search algorithm


Similar to DFS with a predetermined limit
• Drawback of the infinite path in the DFS resolved
• In this algorithm
– The node at the depth limit will treat as it has no successor nodes further

Can be terminated with 2 Conditions of failure:


1. Standard failure value:
– Problem does not have any solution
2. Cutoff failure value:
– No solution for the problem within a given depth limit

Prepared by S.Hemalatha/SCORE

Depth Limited Search algorithm


Similar to DFS with a predetermined limit
• Drawback of the infinite path in the DFS resolved
• In this algorithm
– The node at a depth limit 𝒍 will treat as it has no successor nodes further
Can be terminated with 2 Conditions of failure:
1. Standard failure value:
– Problem does not have any solution
2. Cutoff failure value:
– No solution for the problem within a given depth limit

Prepared by S.Hemalatha/SCORE

9
06-02-2024

Depth Limited Search algorithm


function DEPTH-LIMITED-SEARCH(problem, l) returns a solution, or failure, or cutoff
frontier ← a FIFO queue ini ally containing one path, for the problem's initial state
solution ← failure
while frontier is not empty do
parent ← pop(frontier)
if depth(parent) > l then
solution ← cutoff
else
for child in successors(parent) do
if child is a goal then
return child
add child to frontier
return solution

Prepared by S.Hemalatha/SCORE

Depth-limited search
Example

Prepared by S.Hemalatha/SCORE

10
06-02-2024

Depth Limited Search


time complexity
• Completeness:
– DLS search algorithm is complete if the solution is above the depth-limit
• Time Complexity:
– Time complexity of DLS algorithm is 𝑶 𝒃𝒍
• Space Complexity:
– Space complexity of DLS algorithm is 𝑶 𝒃𝒍
• Optimal:
– NOT optimal even if 𝑙 > 𝑑
DFS can be viewed as a special case of depth-limited search with 𝒍 = ∞

Prepared by S.Hemalatha/SCORE

Depth Limited Search


advantages & disadvantages
• Advantages:
– Memory efficient
• Disadvantages:
– Depth-limited search also has a disadvantage of incompleteness
– It may not be optimal if the problem has more than one solution

Prepared by S.Hemalatha/SCORE

11
06-02-2024

Uniform cost Search algorithm


Uniform-Cost search - To traverse a weighted tree or graph
• Comes into play when a different cost is available for each edge
• Primary goal: To find a path to the goal node which has the lowest cumulative cost

• Uniform-cost search expands nodes according to their path costs form the root node
– Used to solve any graph/tree where the optimal cost is in demand
• A uniform-cost search algorithm is implemented by the priority queue
– Gives maximum priority to the lowest cumulative cost
• Uniform cost search is equivalent to BFS algorithm
– If the path cost of all edges is the same

Prepared by S.Hemalatha/SCORE

Uniform cost Search algorithm

Prepared by S.Hemalatha/SCORE

12
06-02-2024

Uniform-cost Search
Example

Prepared by S.Hemalatha/SCORE

Uniform-cost Search
Example (another)

starting from node S


Reach any one of the
destination node
{G1, G2, G3}

Prepared by S.Hemalatha/SCORE

13
06-02-2024

Uniform-cost Search
Time Complexity
• Completeness:
– Said to be complete, as if there is a solution, UCS will find it
• Time Complexity:
– Cost of optimal solution: 𝑪∗
– Each step to get closer to the goal node: 𝜺
– Then the number of steps: 𝑪∗⁄𝜺 + 𝟏
• +1 is taken here, as we start from state 0 and end to 𝑪∗ ⁄𝜺
– Hence, the worst-case time complexity of UCS:
𝑪∗ ⁄𝜺
𝑶 𝒃𝟏

Prepared by S.Hemalatha/SCORE

Uniform-cost Search
Time Complexity
• Space Complexity:
𝑪∗ ⁄𝜺
𝑶 𝒃𝟏
– Same logic as that of time complexity is applicable

• Optimal:
– UCS is always optimal as it only selects a path with the lowest path cost

Prepared by S.Hemalatha/SCORE

14
06-02-2024

Iterative deepening depth-first Search


IDDF: Combination of DFS and BFS algorithms
• Finds out the best depth limit
– By gradually increasing the limit until a goal is found
• Performs depth-first search up to a certain depth limit, and
• Keeps increasing the depth limit after each iteration until the goal node is found
• Combines the benefits of
– Breadth-first search's fast search and
– depth-first search's memory efficiency
• The iterative search algorithm is useful uninformed search
– when search space is large,
– and depth of goal node is unknown

Prepared by S.Hemalatha/SCORE

Iddf-Search
algorithm

Prepared by S.Hemalatha/SCORE

15
06-02-2024

Iddf-Search
example (book)

Prepared by S.Hemalatha/SCORE

Iddf-Search
Example

Prepared by S.Hemalatha/SCORE

16
06-02-2024

Iddf-Search
Time Complexity
• Completeness:
– This algorithm is complete is if the branching factor is finite
• Time Complexity:
– 𝑏: Branching factor
– 𝑑: Depth
– Worst-case time complexity: 𝑶 𝒃𝒅

• Space Complexity: 𝑶 𝒃𝒅
• Optimal:
– If path cost is a non- decreasing function of the depth of the node

Prepared by S.Hemalatha/SCORE

Bidirectional Search Algorithm


Bidirectional search algorithm
• Runs 2 simultaneous searches
1. Forward-search: From initial state &
2. Backward-search: From goal node
• to find the goal node
• replaces one single search graph with two small subgraphs
– One starts the search from an initial vertex
– Other starts from goal vertex
– The search stops when these two graphs intersect each other
• Bidirectional search can use search techniques such as BFS, DFS, DLS, etc

Prepared by S.Hemalatha/SCORE

17
06-02-2024

Bidirectional Search
Example
• Forward direction:
– It starts traversing from
node 1 in the
• Backward direction:
– Starts from goal node 16
• Terminates at:
– Node 9 where two searches
meet

Prepared by S.Hemalatha/SCORE

Bidirectional Search
/ /
Motivation behind this search: 𝑏 +𝑏 is much less than 𝑏
• Goal test replaced with a check to see if the frontiers of the two searches
intersect
– if they do, a solution has been found
 the first such solution found may not be optimal
even if the two searches are both breadth-first;
some additional search is required to make sure there isn’t another short-
cut across the gap
• The check can be done
– when each node is generated / selected for expansion
– & will take constant time

Prepared by S.Hemalatha/SCORE

18
06-02-2024

Bidirectional Search
/ /
Motivation behind this search: 𝑏 +𝑏 is much less than 𝑏
• Example:
– If a problem has solution depth d=6, and each direction runs breadth-first search
one node at a time, then in the worst case the two searches meet when they have
generated all of the nodes at depth 3
– For b=10, this means a total of 2,220 node generations, compared with 1,111,110
for a standard breadth-first search
/
• Time complexity: O 𝑏
• Space complexity: O 𝑏 /

– Can be reduced this by roughly half if one of the two searches is done by iterative
deepening,
• but at least one of the frontiers must be kept in memory so that the intersection check
can be done
• This space requirement is the most significant weakness of bidirectional search
Prepared by S.Hemalatha/SCORE

Bidirectional Search
Advantages & Disadvantages
• Advantages:
– Bidirectional search is fast
– Bidirectional search requires less memory

• Disadvantages:
– Implementation of the bidirectional search tree is difficult
– In bidirectional search, one should know the goal state in advance

Prepared by S.Hemalatha/SCORE

19
06-02-2024

Bidirectional Search
Time Complexity
• Completeness:
– Bidirectional Search is complete if we use BFS in both searches
• Time Complexity:
– Time complexity of bidirectional search using BFS is O(bd)

• Space Complexity:
– Space complexity of bidirectional search is O(bd)
• Optimal:
– Bidirectional search is Optimal

Prepared by S.Hemalatha/SCORE

Comparison
uninformed search algorithms

Prepared by S.Hemalatha/SCORE

20

You might also like