Unit 2
Unit 2
Unit 2
• S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Breadth-first Search
• Time Complexity: Time Complexity of BFS algorithm
can be obtained by the number of nodes traversed in
BFS until the shallowest Node. Where the d= depth of
shallowest solution and b is a node at every state.
T (b) = 1+b2+b3+.......+ bd= O (bd)
• Space Complexity: Space complexity of BFS algorithm
is given by the Memory size of frontier which is O(bd).
• 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.
BFS
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.
Assignment problem : BFS
2. Depth-first Search
• Depth-first search (DFS) is popular among the
uninformed search strategies in artificial intelligence to
explore and traverse a graph or tree data structure.
• The algorithm starts at a given node in the graph and
explores as far as possible along each branch before
backtracking.
• DFS is a recursive algorithm that follows the following
steps:
1. Mark the starting node as visited.
2. Explore all adjacent nodes that have not been visited.
3. For each unvisited adjacent node, repeat steps 1 and
2 recursively.
4. Backtrack if all adjacent nodes have been visited or
there are no unvisited nodes.
Depth-first Search
• DFS can be implemented using a stack data
structure or recursion.
• The recursive implementation is simpler to
understand but can cause a stack overflow if the
graph or tree is too large.
• DFS has several applications in AI, including
pathfinding, searching for solutions to a problem,
and exploring the state space of a problem.
• It is particularly useful when the solution is far
from the starting node because it can explore the
graph deeply before exploring widely.
Example : DFS
• In the below search tree, we have shown the flow of depth-first search,
and it will follow the order as:
Root node--->Left node ----> right node.
• It will start searching from root node S, and traverse A, then B, then D and
E, after traversing E, it will backtrack the tree as E has no other successor
and still goal node is not found. After backtracking it will traverse node C
and then G, and here it will terminate as it found goal node.
Pseudocode: DFS
DFS
• Completeness: DFS search algorithm is complete within
finite state space as it will expand every node within a
limited search tree.
• Time Complexity: Time complexity of DFS will be
equivalent to the node traversed by the algorithm. It is
given by:
T(n)= 1+ n2+ n3 +.........+ nm=O(nm)
• Where, m= maximum depth of any node and this can be
much larger than d (Shallowest solution depth)
• Space Complexity: DFS algorithm needs to store only single
path from the root node, hence space complexity of DFS is
equivalent to the size of the fringe set, which is O(bm)
• Optimal: DFS search algorithm is non-optimal, as it may
generate a large number of steps or high cost to reach to
the goal node.
DFS
Advantage:
• 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:
• 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.
Assignment problem : DFS
3. Depth-Limited Search Algorithm:
• A depth-limited search algorithm is similar to
depth-first search with a predetermined limit.
• Depth-limited search can solve the drawback of
the infinite path in the Depth-first search.
• In this algorithm, the node at the depth limit will
treat as it has no successor nodes further.
• Depth-limited search can be terminated with two
Conditions of failure:
• Standard failure value: It indicates that problem
does not have any solution.
• Cutoff failure value: It defines no solution for the
problem within a given depth limit.
Algorithm to find DLS.
Step 1: Start with the initial state of the problem.
Step 2: Start from the initial state and perform the DLS with a
current depth limit of 0.
Step 5: Repeat until the goal state is reached or all the nodes
are explored.
Example : IDDFS
Set the depth limit as 0 and start the search
from the initial state, i.e. A.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure
and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation
function (g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list.
For each successor n', check whether n' is already in the OPEN or CLOSED list, if
not then compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to