0% found this document useful (0 votes)
5 views

Unit 2

Uploaded by

wovit87983
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 2

Uploaded by

wovit87983
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Search Algorithms in Artificial Intelligence

• In Artificial Intelligence, Search techniques are universal problem-solving


methods.
• Rational agents or Problem-solving agents in AI mostly used these search
strategies or algorithms to solve a specific problem and provide the best result.
Search Algorithm Terminologies:
• Search: Searching is a step by step procedure to solve a search-problem in a
given search space. A search problem can have three main factors:
1. Search Space: Search space represents a set of possible solutions, which a
system may have.
2. Start State: It is a state from where agent begins the search.
3. Goal test: It is a function which observe the current state and returns whether
the goal state is achieved or not.
Search tree: A tree representation of search problem is called Search tree. The root
of the search tree is the root node which is corresponding to the initial state.
• Actions: It gives the description of all the available actions to the agent.
• Transition model: A description of what each action do, can be represented as a
transition model.
• Path Cost: It is a function which assigns a numeric cost to each path.
• Solution: It is an action sequence which leads from the start node to the goal node.
• Optimal Solution: A solution has the lowest cost among all solutions.
Properties of Search Algorithms
Completeness: A search algorithm is said to be complete if it guarantees to return a
solution if at least any solution exists for any random input.
Optimality: If a solution found for an algorithm is guaranteed to be the best solution
(lowest path cost) among all other solutions, then such a solution for is said to be an
optimal solution.
Time Complexity: Time complexity is a measure of time for an algorithm to complete
its task.
Space Complexity: It is the maximum storage space required at any point during the
search, as the complexity of the problem.
Types of search algorithms
Based on the search problems we can classify the search algorithms into
uninformed (Blind search) search and informed search (Heuristic search)
algorithms.
Breadth-First Search (BFS)
• Breadth-first search is the most common search strategy for traversing a tree
or graph. This algorithm searches breadthwise in a tree or graph, so it is
called breadth-first search.
• BFS algorithm starts searching from the root node of the tree and expands
all successor node at the current level before moving to nodes of next level.
• The breadth-first search algorithm is an example of a general-graph search
algorithm.
• Breadth-first search implemented using FIFO queue data structure.
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.

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).

2. Processing the Current Node:


o Remove the node with the lowest f(n) value from the open list.
o If this node is the goal, the algorithm terminates as the path has been found.
o Otherwise, add this node to the closed list (the set of nodes already evaluated).

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.

 Euclidean Distance: For scenarios where, diagonal movements are possible.


Alpha-Beta Pruning:
• Alpha-beta pruning is a modified version of the minimax algorithm. It is an optimization
technique for the minimax algorithm.
• As we have seen in the minimax search algorithm that the number of game states it has to
examine are exponential in depth of the tree. Since we cannot eliminate the exponent, but we
can cut it to half.
• Hence there is a technique by which without checking each node of the game tree we can
compute the correct minimax decision, and this technique is called pruning.
• This involves two threshold parameter Alpha and beta for future expansion, so it is called
alpha-beta pruning. It is also called as Alpha-Beta Algorithm.
• Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only prune the
tree leaves but also entire sub-tree.
• The two-parameter can be defined as:
1. Alpha: The best (highest-value) choice we have found so far at any point along the path of
Maximizer. The initial value of alpha is -∞.
2. Beta: The best (lowest-value) choice we have found so far at any point along the path of
Minimizer. The initial value of beta is +∞.
• Condition for Alpha-beta pruning:
• The main condition which required for alpha-beta pruning is:
α>=β
Key points about alpha-beta pruning:
• The Max player will only update the value of alpha.
• The Min player will only update the value of beta.
• While backtracking the tree, the node values will be passed to upper nodes
instead of values of alpha and beta.
• We will only pass the alpha, beta values to the child nodes.
Local Search Algorithms
Local search algorithms are a class of heuristic-based algorithms that explore the search space
by iteratively moving from one solution to a neighboring solution with the goal of improving or
optimizing a given objective function. Unlike systematic algorithms (e.g., BFS, DFS), local
search algorithms focus on finding good enough solutions rather than exhaustively searching
for an optimal one.
• Characteristics of Local Search Algorithms:
1. Exploration of Neighbouring Solutions: These algorithms evaluate neighbouring solutions
based on some criteria and move towards more promising ones.
2. Memory Efficiency: Local search typically operates using constant memory, since it only
keeps track of the current solution and its neighbours.
3. Incomplete: These algorithms are not guaranteed to find the optimal solution or explore all
possibilities. They focus on "local" improvement.
4. Iterative Improvement: They iteratively refine the solution until a stopping criterion is met,
such as a time limit, a fixed number of iterations, or a solution that satisfies certain
conditions.
• Applications:
 Traveling Salesman Problem (TSP): Local search algorithms like genetic algorithms
and simulated annealing are often used to find near-optimal solutions.
 Scheduling: Hill climbing and tabu search are effective for job scheduling in industrial
systems.
 Optimization in Machine Learning: Gradient-based algorithms are commonly used in
neural network training.
• Challenges in Local Search:
• Local Optima: Local search methods may get stuck in suboptimal regions of the
search space.
• Plateaus and Ridges: Flat regions or narrow peaks can slow down or halt the search
process.
• Diversity in Exploration: Some algorithms (e.g., hill climbing) can explore a limited
portion of the search space, leading to incomplete solutions.

You might also like