Unit 2
Unit 2
Q1 Q2
Q3
Depth-First Search (DFS)
• Depth-first search is 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.
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.
Q1 Q2
Difference Between BFS and DFS
Parameters BFS DFS
Data Structure BFS uses Queue data structure for DFS uses Stack data structure.
finding the shortest path.
Definition BFS is a traversal approach in DFS is also a traversal approach in
which we first walk through all which the traverse begins at the
nodes on the same level before root node and proceeds through the
moving on to the next level. nodes as far as possible until we
reach the node with no unvisited
nearby nodes.
Conceptual Difference BFS builds the tree level by level. DFS builds the tree sub-tree by sub-
tree.
Approach used It works on the concept of FIFO It works on the concept of LIFO
(First In First Out). (Last In First Out).
Suitable for BFS is more suitable for searching DFS is more suitable when there
vertices closer to the given source. are solutions away from source.
Depth Limited Search (DLS)
• Depth Limited Search is a modified version of DFS that imposes a limit on the depth of
the search.
• This means that the algorithm will only explore nodes up to a certain depth, effectively
preventing it from going down excessively deep paths that are unlikely to lead to the
goal.
• By setting a maximum depth limit, DLS aims to improve efficiency and ensure more
manageable search times.
How Depth Limited Search Works
• Initialization: Begin at the root node with a specified depth limit.
• Exploration: Traverse the tree or graph, exploring each node’s children.
• Depth Check: If the current depth exceeds the set limit, stop exploring that path and
backtrack.
• Goal Check: If the goal node is found within the depth limit, the search is successful.
• Backtracking: If the search reaches the depth limit or a leaf node without finding the
goal, backtrack and explore other branches.
Applications of Depth Limited Search in AI:
• Pathfinding in Robotics
• Network Routing Algorithms
• Puzzle Solving in AI Systems
Advantages:
• When there is a leaf node depth which is as large as the highest level allowed, do
not describe its children, and then discard it from the stack.
Disadvantages:
• Depth-limited search also has a disadvantage of incompleteness.
• It may not be optimal if the problem has more than one solution.
Q1 1 Q2 A
2 3 B C
4 5 D E F
6 G H I J
A
J K
B C
Q3 D E F
G H I K
L M
Iterative Deepening Search (IDS)
• A search algorithm known as IDS combines the benefits of DFS with Breadth First
Search (BFS).
• The graph is explored using DFS, but the depth limit steadily increased until the
target is located.
• In other words, IDS continually runs DFS, raising the depth limit each time, until
the desired result is obtained.
Advantages
• Ensures that a solution will be found if one is there in the search space.
• Minimizes the algorithm's memory footprint by only storing the nodes up to the
current depth limit.
Disadvantages
• IDS has the disadvantage of potentially visiting certain nodes more than once,
which might slow down the search.
Uniform-cost Search Algorithm
• Uniform-cost search is a searching algorithm used for traversing a weighted tree or graph.
• This algorithm comes into play when a different cost is available for each edge.
• The primary goal of the uniform-cost search is to find a path to the goal node which has the
lowest cumulative cost.
• Uniform-cost search expands nodes according to their path costs from the root node.
• It can be used to solve any graph/tree where the optimal cost is in demand.
• A uniform-cost search algorithm is implemented by the priority queue.
• It gives maximum priority to the lowest cumulative cost.
Advantages:
• Uniform cost search is optimal because at every state the path with the least cost is chosen.
• It is a type of comprehensive algorithm that will find a solution if one exists.
Disadvantages:
• It does not care about the number of steps involve in searching and only concerned about path
cost. Due to which this algorithm may be stuck in an infinite loop.
Q1 S
1 5
3
A B C
3 5 2
G F E
1
Des
Q2. Consider the following tree graph where the nodes represent cities, and the edges represent the travel
cost between them:
• A is connected to B with a cost of 4.
• A is connected to C with a cost of 3.
• B is connected to D with a cost of 2.
• B is connected to E with a cost of 5.
• C is connected to F with a cost of 6.
• D is connected to G with a cost of 1.
• E is connected to G with a cost of 4.
• F is connected to G with a cost of 2.
The goal is to find the least-cost path from A (the start node) to G (the goal node) using Uniform Cost
Search.
1. Draw the tree graph corresponding to the above cities and travel costs.
2. Apply the Uniform Cost Search algorithm on the tree graph to find the least-cost path from A to G.
Show each step of the algorithm, including the exploration of nodes and the updating of the priority queue.
3. What is the least-cost path from A to G? What is the total cost of this path?
Bidirectional Search
• Bidirectional Search is an algorithm that finds the shortest path between a source and a goal by
simultaneously running two searches: one forward from the source and one backward from the goal.
• The idea is that the two searches will meet somewhere in the middle, significantly reducing the
search space compared to a traditional search algorithm (like BFS or DFS).
How Bidirectional Search Works:
• Forward Search: This search starts from the initial state (source).
• Backward Search: This search starts from the goal and explores nodes in reverse.
• Meeting in the Middle: The search stops when the two searches meet at a common node, meaning a
path has been found from the source to the goal.
Steps of the Algorithm:
• Initialize two queues: one for forward search and one for backward search.
• Add the source node to the forward search queue and the goal node to the backward search queue.
• Alternate between expanding the forward and backward searches by expanding nodes one level at a
time.
• If the searches meet at a node, the path is found.
• If there is no common node after both searches have been exhausted, there is no solution.
Advantages:
• Faster in large search spaces.
• Reduces the number of nodes explored significantly.
Disadvantages:
• Requires a method to search backwards, which might not always be
straightforward.
• More memory-intensive since it requires maintaining two search frontiers.
B E
A D G
C F
Informed Search Algorithms
• Best First Search (BFS)
Best First Search (BFS) is a pathfinding algorithm that explores a graph by selecting the most promising node according to a
specified heuristic function. It prioritizes nodes based on the estimated cost to reach the goal, typically using heuristics like
distance or some problem-specific metric. The primary goal of Best First Search is to minimize the exploration of
unnecessary nodes, making it faster in many scenarios compared to uninformed search methods.
Steps in Best First Search Algorithm:
1. Initialize: Start with a priority queue (usually a min-heap) to hold the nodes to be explored. The priority is based on the
heuristic cost, often denoted as ℎ(𝑛), which is an estimate of the cost from node 𝑛n to the goal.
2. Start at the Initial Node: Place the starting node into the priority queue with its heuristic value.
3. Explore the Node with the Lowest Heuristic Value:
Remove the node with the lowest heuristic value from the queue.
If it is the goal node, return the path as the solution.
4. Expand the Node:
Generate all successors (neighboring nodes) of the current node.
5. Add Successors to the Priority Queue:
For each successor, calculate the heuristic cost.
Add them to the priority queue if they haven't been explored or if their current path is better (lower heuristic cost).
6. Repeat:
Continue the process until the goal node is found, or the queue is empty (indicating that there is no solution).
A* star search algorithm
The A* (A-star) search algorithm is a widely used pathfinding and graph traversal algorithm that is
efficient and guarantees finding the shortest path in many situations. It combines the benefits of Dijkstra’s
Algorithm and Best-First Search by using a heuristic to guide its search, which makes it both complete and
optimal.
• Key Concepts
• A* works by evaluating nodes using two values:
(1) g(n): The cost to reach the current node from the start node.
(2) h(n): A heuristic that estimates the cost to reach the goal from the current node (often calculated using
Manhattan distance, Euclidean distance, etc.).
(3) These two values are combined into a function:
f(n) = g(n) + h(n)
Where:
g(n) is the exact cost of the path from the start node to the current node 𝑛
h(n) is the estimated cost from node 𝑛 to the goal (heuristic).
f(n) is the estimated total cost of the path through node 𝑛 to the goal.
Steps of the A* Algorithm
1. Initialization: Start by adding the initial node to a priority queue (often called the open list) with a
priority of f(n).
3. Expanding Neighbours:
o For each neighbor of the current node, calculate g(n) and f(n) (considering the cost to move from the
current node to the neighbor).
o If the neighbor is already in the closed list with a lower cost, skip it.
o If the neighbor is not in the open list, or the new path to the neighbor is better than the previously
known one, update it and add it to the open list.
4. Repeat: Continue expanding nodes, always choosing the one with the lowest f(n) value, until the goal is
reached or the open list is empty (which means there is no valid path).
Heuristics
The quality of the A* algorithm depends on the heuristic function h(n). It must be:
Admissible: It never overestimates the cost to reach the goal, ensuring optimality.
Consistent (or Monotonic): The estimated cost of reaching the goal is always less than or equal to the cost
of reaching a neighbor plus the cost from the neighbor to the goal.
Common heuristics:
Manhattan Distance: Suitable for grids where only horizontal and vertical movements are allowed.