0% found this document useful (0 votes)
41 views22 pages

UNIT-2 (Part-1)

Uploaded by

Arun Gurjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views22 pages

UNIT-2 (Part-1)

Uploaded by

Arun Gurjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT-2 (Part- 1)

Introduction to Searching Techniques

Searching is a fundamental technique in artificial intelligence (AI) and computer science, used to
solve problems where the solution is not immediately apparent. It involves systematically
exploring a space of possible solutions (known as the search space) to find a solution that satisfies
the problem’s requirements.

Problem Solving by Searching

In many AI problems, the desired outcome is unknown, and the task is to search through possible
actions to find a solution. This process is called problem-solving by searching, where the objective
is to achieve a specific goal from an initial state using a sequence of actions.

➢ State Space: The set of all possible states that can be reached from the initial state by
applying various actions.
➢ Initial State: The state in which the problem-solving process begins.
➢ Goal State: The desired state that signifies the solution to the problem.
➢ Operators: Actions that transition the system from one state to another within the state
space.
➢ Path: A sequence of states connected by operators, representing moving from the initial
state to the goal state.
➢ Search Strategy: The method used to explore the state space, deciding which paths to
follow and which to ignore.

Searching Process:

1. Problem Formulation: Define the problem in terms of states, operators, initial state, and
goal state. This creates a formal representation of the problem.
2. Search: Using a search algorithm to explore the state space from the initial state towards
the goal state.
3. Solution: Once a path from the initial state to the goal state is found, it represents the
solution to the problem. The solution may be optimal (the shortest or least-cost path) or
just a valid path.

When searching for solutions, different algorithms are used depending on the characteristics of the
problem, such as whether it requires the shortest path, the least costly path, or any path that leads
to the goal. The search algorithms can be categorized into uninformed (blind) and informed
(heuristic) search methods.

Uninformed Search/ Blind Search strategies

Uninformed search strategies do not have additional knowledge about the problem domain other
than the problem definition itself. They systematically explore the search space.

Following are the various types of uninformed search algorithms:

1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search

Breadth-First Search (BFS)

Breadth-first search (BFS) is a fundamental algorithm used for exploring or traversing graphs or
tree data structures. It systematically explores the nodes in layers, starting from a given source
node and expanding all its neighbors before moving on to the next level of nodes. It is an
uninformed search method, meaning it does not use any additional knowledge about the problem
domain.

Key Characteristics of BFS:


Explores level by level: BFS explores all nodes at the current depth level before moving to the
next depth level. This makes BFS suitable for finding the shortest path (in terms of the number of
edges) in an unweighted graph.
• Queue-based: BFS uses a queue data structure to keep track of nodes to be explored,
ensuring a first-in, first-out (FIFO) order.
• Complete: BFS is guaranteed to find a solution if one exists, as it explores every possible
path.
• Optimal: BFS is optimal in unweighted graphs, meaning it always finds the shortest path
in terms of the number of edges (or actions) from the start node to the goal node.

Algorithm:

Here is the basic outline of the BFS algorithm:

1. Initialize:
o Start at the initial node (source node).
o Use a queue to keep track of nodes to explore. Enqueue the initial node.
o Keep a record of visited nodes to avoid revisiting the same node.
2. Explore Nodes:
o Dequeue the first node from the queue.
o Check if this node is the goal. If yes, return the path.
o If not, enqueue all its unvisited neighboring nodes and mark them as visited.
o Repeat this process until the goal node is found or the queue is empty (indicating
no solution exists).
3. Termination:
o The search ends when either the goal node is found or all reachable nodes have
been explored.
Time Complexity: O(b^d)

b: branching factor (maximum number of children a node can have).

d: depth of the shallowest goal node.

BFS explores the nodes level by level, so it expands all nodes at depth d before going to depth
d+1. In the worst case, it explores all nodes up to the depth of the goal, which results in
exponential time complexity with respect to the depth of the goal.
Applications of BFS:

1. Shortest Path in Unweighted Graphs: BFS is used to find the shortest path between two
nodes when all edges have equal weight.
2. Web Crawling: BFS can be used by web crawlers to visit and index websites starting from
a seed URL.
3. Social Networking: BFS is used to find connections or relationships between users (e.g.,
finding friends at a specific distance from a user).
4. Solving Puzzle Problems: BFS can be applied to puzzles like mazes or the 8-puzzle
problem, where it explores all possible moves level by level.

Depth-First Search (DFS)

Depth-First Search (DFS) is another fundamental graph traversal algorithm. Unlike Breadth-First
Search (BFS), which explores nodes level by level, DFS explores as far down a branch as possible
before backtracking. It uses a last-in, first-out (LIFO) strategy, often implemented with recursion
or a stack.

Key Characteristics of DFS:

• Deep exploration: DFS goes deep into the graph along one branch, exploring as far as
possible before backtracking.
• Stack-based: DFS uses either recursion (which implicitly uses the call stack) or an explicit
stack to keep track of nodes.
• Not guaranteed to find the shortest path: Since DFS dives deep without considering path
lengths, it doesn’t always find the shortest path in terms of edges.
• Efficient in memory usage: DFS requires less memory than BFS, as it only needs to store
nodes on the current path.
• Complete: DFS will eventually explore all nodes if no cycles are present, though in cyclic
graphs, care must be taken to avoid revisiting nodes.

Algorithm:

Here’s the basic outline of the DFS algorithm:

1. Initialize:
o Start at the source node.
o Use a stack (or recursion) to keep track of nodes to explore.
o Keep track of visited nodes to prevent revisiting.
2. Explore Nodes:
o Pop a node from the stack (or visit recursively).
o Check if this node is the goal. If yes, return the path.
o If not, push all unvisited neighboring nodes onto the stack.
o Repeat this process until the goal is found or the stack is empty.
3. Termination:
o The search ends when either the goal node is found, or all nodes have been
explored.
Time Complexity: O(bm)

b: branching factor.
m: maximum depth of the search tree (or the depth limit, if one is set).

DFS can go as deep as possible before backtracking. In the worst case, it may traverse all nodes
up to the maximum depth mmm, which can be much larger than the depth of the goal node d,
leading to worse performance in comparison to BFS, especially in very deep or infinite trees.

Properties of DFS:
1. Not Optimal: DFS is not guaranteed to find the shortest path since it explores as far as
possible along one path before backtracking. In some cases, this can lead to a much longer
path than BFS would find.
2. Not Complete (in infinite-depth search spaces): In graphs with cycles or infinite paths,
DFS can get stuck exploring one infinite branch without ever finding a solution. To avoid
this, a depth limit or visited nodes tracking is used.
3. Memory Efficient: DFS generally uses less memory than BFS because it doesn’t need to
store all nodes at a given depth level but only those in the current branch.

Applications of DFS:

1. Topological Sorting: DFS is commonly used in topological sorting of a directed acyclic


graph (DAG), where nodes are processed in a linear order based on their dependencies.
2. Finding Connected Components: DFS can be used to identify connected components in
undirected graphs.
3. Solving Mazes: DFS is used in many applications involving pathfinding, such as mazes or
puzzles, where the goal is to explore all possible paths.
4. Detecting Cycles: DFS can be used to detect cycles in a graph by keeping track of the
visited nodes during traversal.
5. Generating Mazes: DFS is often used to generate mazes in algorithms where the solution
is represented by deep paths with occasional backtracking.
6. Artificial Intelligence (AI) and Game Search: DFS is used in some AI search problems,
particularly when the search tree is very deep, or the solution is far from the starting point.

Difference Between BFS & DFS


Uniform-Cost Search (UCS)

Uniform-cost search (UCS) is a search algorithm used to find the least-cost path in a graph where
each edge has a different cost. Unlike Depth-First Search (DFS) or Breadth-First Search (BFS),
UCS is an uninformed search that takes into account the cost of reaching nodes rather than the
number of edges or depth of the search. It is closely related to Dijkstra's algorithm for shortest
paths.

Key Characteristics of UCS:

• Explores by cost: UCS expands nodes based on the cumulative cost to reach them. It
always expands the lowest-cost node first, which ensures that it finds the least-cost path.
• Priority Queue-based: UCS uses a priority queue to keep track of nodes, where the
priority is the path cost. This ensures that nodes are expanded in the order of their
cumulative path cost.
• Complete: UCS is guaranteed to find a solution if one exists, as long as all edge costs are
non-negative.
• Optimal: UCS is optimal, meaning it always finds the least-cost path to the goal node if
the edge costs are positive.
Algorithm:

The basic outline of the UCS algorithm is as follows:

1. Initialize:
o Start at the initial node (source node).
o Use a priority queue to keep track of nodes to explore, where the priority is based
on the cumulative cost to reach the node.
o Keep a record of visited nodes to avoid revisiting them with higher costs.
2. Explore Nodes:
o Dequeue the node with the lowest cost from the priority queue.
o If this node is the goal, return the path and the cost.
o If not, enqueue all unvisited neighboring nodes with their cumulative path cost and
mark them as visited.
o Repeat the process until the goal node is found or the priority queue is empty.
3. Termination:
o The search ends when either the goal node is found, or all reachable nodes have
been explored.
Time Complexity: O(b^C∗/ϵ)

• b: branching factor.
• C^*: cost of the optimal solution.
• ε: the smallest edge cost (assuming all edge costs are positive).

UCS expands nodes in order of increasing path cost, ensuring it finds the least-cost solution. The
complexity depends on the number of different path costs, which in turn relates to the smallest edge cost
ϵ\epsilonϵ. The time complexity is similar to BFS but with consideration for the cost of paths, leading to
more nodes expanded when there are many paths with low costs before reaching the optimal solution.

Properties of UCS:

1. Optimal: UCS always finds the least-cost path to the goal if all edges have non-negative
costs.
2. Complete: UCS is complete, meaning it will find a solution if one exists, as long as there
are no negative edge costs.
3. Time-Consuming for Large Graphs: UCS can be slow for large graphs or graphs with
many nodes and edges, especially when there are many nodes to explore with similar path
costs.

Applications of UCS:

1. Shortest Path in Weighted Graphs: UCS is used to find the shortest path in graphs where
edges have different costs, such as road networks with varying distances or travel times.
2. Network Routing: UCS is used in network routing algorithms where packets must be sent
through the least-cost path, considering factors like bandwidth or latency.
3. Robot Navigation: In robotics, UCS is used to find the least-cost path through a map where
different terrains have different traversal costs.
4. Puzzle Solving: UCS can be applied to puzzle problems where each move has a different
cost, and the goal is to minimize the total cost of the solution.

Iterative Deepening Depth-First Search (IDDFS)


Iterative Deepening Depth-First Search (IDDFS) combines the strengths of both Depth-First
Search (DFS) and Breadth-First Search (BFS). It is an uninformed search technique that
systematically increases the depth limit in DFS, performing a series of depth-limited searches,
until the goal is found. It is often preferred when the solution depth is unknown but the search
space is large, as it provides the completeness of BFS while using the space efficiency of DFS.

Key Characteristics of IDDFS:

• Combines DFS and BFS: IDDFS uses a DFS-based strategy to go deep into the graph but
limits the depth, like BFS, by systematically exploring shallower depths first.
• Space Efficient: Like DFS, it uses minimal memory (proportional to the depth of the
search).
• Complete: IDDFS is guaranteed to find a solution if one exists, as it explores the search
space incrementally.
• Optimal: If the cost of each step is the same, IDDFS is guaranteed to find the shortest path
(in terms of the number of edges).

Algorithm:

1. Initialize:
o Start with an initial depth limit of 0.
o Perform a depth-limited DFS.
o If the goal is not found, increase the depth limit and repeat the DFS.
2. Depth-Limited Search:
o Perform DFS but only up to the current depth limit.
o If the goal is found within the limit, return the solution.
o If the goal is not found, increment the depth limit and repeat.
3. Repeat:
o Continue increasing the depth limit and performing DFS until the goal is found or
the entire search space is explored.
Time Complexity: O(b^d)
o The time complexity is the same as BFS: O(b^d), where b is the branching factor,
and d is the depth of the goal node.
o Although nodes are revisited at each depth level, the cost is not significantly greater
than BFS since the deeper levels dominate the runtime.
• Space Complexity: O(b * d)
o Like DFS, IDDFS has a space complexity proportional to the depth of the search,
O(b * d), where d is the depth of the goal and b is the branching factor.
o It is more memory efficient than BFS, which requires O(b^d) space.

Properties of IDDFS:

1. Complete: IDDFS is complete, meaning it will find a solution if one exists, even in infinite
search spaces, as long as the branching factor is finite.
2. Optimal (with uniform edge costs): If each edge has the same cost, IDDFS is guaranteed
to find the shortest path (in terms of the number of edges).
3. Space-Efficient: IDDFS uses space proportional to the depth of the search, making it much
more memory-efficient than BFS for deep graphs.
4. Repetitive Node Exploration: Although nodes at shallower depths are revisited multiple
times, the deeper levels dominate the overall time complexity, making the cost of revisiting
negligible for large graphs.

Applications of IDDFS:

1. Pathfinding: IDDFS is commonly used in pathfinding problems, especially when the


depth of the solution is unknown, but space constraints are important.
2. Game Tree Search: In AI game tree searches, IDDFS is used for searching game trees
where the depth of the solution varies or is unknown, but finding the optimal solution (or
a good approximation) is important.
3. Robotics: IDDFS can be used in robotic navigation where space is a concern, and it is
necessary to find a path through a large search space.
4. Artificial Intelligence (AI): In AI, IDDFS is often used in combination with heuristic
search algorithms for large and complex problem spaces that require an optimal solution
without excessive memory usage.

Heuristic Search/Informed Search

Heuristic Search is a search strategy in artificial intelligence (AI) that uses a heuristic function to
guide the search process toward the goal more efficiently. A heuristic function estimates the cost
or distance from a given state to the goal, helping the algorithm prioritize nodes that are likely to
lead to a solution faster.

Unlike uninformed search algorithms (such as BFS, DFS, UCS), which do not have specific
knowledge about the problem space and explore blindly, heuristic search algorithms use
problem-specific information to make more informed decisions. This makes them more efficient,
particularly for large search spaces.

Key Concept:
• Heuristic Function (h(n)): This function provides an estimate of the cost from node n to
the goal. It is problem-dependent and not necessarily exact, but it helps reduce the search
space by prioritizing more promising paths.
• Evaluation Function (f(n)): In some heuristic search algorithms (e.g., A*), an evaluation
function f(n) combines the actual cost to reach node n (g(n)) and the heuristic estimate
(h(n)) to guide the search: f(n)=g(n)+h(n)

Types of Heuristic Search Algorithms

1. Greedy Best-First Search:


o Greedy Best-First Search uses only the heuristic function h(n) to expand the node
that seems closest to the goal, based purely on the heuristic value.
o Evaluation function: f(n)=h(n)

Example:
If you are navigating a map, a simple heuristic might be the straight-line distance/
Euclidean Distance between the current location and the destination.

o Advantages:
▪ Fast for problems where the heuristic is highly accurate.
▪ Often finds a solution quickly, as it aggressively follows paths that seem to
lead directly to the goal.
o Disadvantages:
▪ Not optimal: It might find a suboptimal solution.
▪ Not complete: If the heuristic misguides the search, it might not find a
solution at all, especially if there are loops or dead ends in the search space.
2. A* Search:
o A* is the most well-known and widely used heuristic search algorithm. It combines
both the actual cost to reach a node g(n) and the heuristic estimate h(n).
o A* balances between exploring the least costly path and the path that seems closest
to the goal.
o Evaluation function: f(n)=g(n)+h(n)

Example:
A* is commonly used in pathfinding, such as in GPS systems or video games, where it
considers both the distance already traveled and the estimated distance remaining.

o Advantages:
▪ Optimal: A* guarantees finding the least-cost solution, assuming the
heuristic is admissible (i.e., never overestimates the true cost).
▪ Complete: A* will always find a solution if one exists, as long as the search
space is finite and the cost of moving from one state to another is always
positive.
o Disadvantages:
▪ Memory-intensive: A* requires storing all explored nodes, which can lead
to high memory usage for large search spaces.
▪ Slower than Greedy Best-First Search in terms of exploration speed, as it
balances between cost and heuristic guidance.
3. Hill Climbing:
o Hill Climbing is a local search algorithm that continuously moves toward the node
with the best heuristic value (the "highest point" in the search space). It does not
consider the cost to reach the node but tries to reach the goal by making short-term
decisions based on the heuristic.

Example:
Imagine you're climbing a hill in foggy weather with only a local view of the terrain, and
you move toward the direction that seems steepest, hoping it's the highest peak.

Types of Hill Climbing:

➢ Simple hill Climbing.


➢ Steepest-Ascent hill-climbing
➢ Stochastic hill Climbing

Simple Hill Climbing Algorithm

Step 1: Evaluate the initial state, if it is the goal state then return success and Stop.

Step 2: Loop Until a solution is found or there is no new operator left to apply.

Step 3: Select and apply an operator to the current state.

Step 4: Check the new state:

If it is a goal state, then return to success and quit.

Else if it is better than the current state then assign a new state as a

current state.

Else if not better than the current state, then return to step 2.

Step 5: Exit.

State Space Diagram for Hill Climbing


Steepest-Ascent Hill Climbing

Step 1: Evaluate the initial state, if it is the goal state then return success and stop,

else make the current state as an initial state.

Step 2: Loop until a solution is found or the current state does not change.

a) Let SUCC be a state such that any successor of the current state will be better than it.
b) For each operator that applies to the current state:

i. Apply the new operator and generate a new state.


ii. Evaluate the new state.
iii. If it is goal state, then return it and quit, else compare it to the SUCC.
iv. If it is better than SUCC, then set new state as SUCC.
v. If the SUCC is better than the current state, then set current state to SUCC.

Step 3: Exit.

Stochastic Hill Climbing

➢ Stochastic hill climbing does not examine all its neighbours before moving.
➢ Rather, this search algorithm selects one neighbour node at random and decides whether
to choose it as a current state or examine another state.
Issues in Hill Climbing
Advantages:

▪ Simple and fast.


▪ Works well when the heuristic is accurate and there are no local optima to
trap the search.

Disadvantages:

▪ Not optimal: Can easily get stuck in local optima, where every neighboring
node has a worse heuristic value, even though it is not the global solution.
▪ Not complete: Hill Climbing might never find a solution, especially if the
search space has plateaus (flat regions with no better-neighboring nodes) or
ridges (steep changes in heuristic values).

Advantages of Heuristic Search over Uninformed Search


1. Efficiency:
o Heuristic Search: Uses domain-specific knowledge to prioritize which paths to
explore, reducing the search space and often leading to faster solutions.
o Uninformed Search: Explores blindly (e.g., BFS, DFS), potentially exploring
irrelevant or suboptimal paths before reaching the solution.
2. Speed:
o Heuristic Search: When the heuristic function is well-designed, the search process
can be significantly faster because it guides the search toward the goal.
o Uninformed Search: In large search spaces, uninformed search algorithms may
take longer as they may have to explore all possible paths (e.g., BFS explores level
by level).
3. Flexibility:
o Heuristic Search: Adaptable to different problem domains by using various
heuristic functions, making it suitable for complex real-world problems (e.g.,
navigation, scheduling, AI in games).
o Uninformed Search: Typically less flexible, as they are designed for more general
use cases without considering specific problem features.
4. Memory Usage:
o Heuristic Search (e.g., A*): This may require more memory, as it stores
information about nodes and paths to compute the cost and heuristic.
o Uninformed Search (e.g., DFS): DFS, for example, can be more memory-efficient
but lacks the guidance of heuristics.

Disadvantages of Heuristic Search over Uninformed Search


1. Heuristic Design:
o Heuristic Search: The effectiveness of the search heavily depends on the quality
of the heuristic. A poor heuristic can mislead the search, leading to inefficient
exploration or suboptimal solutions.
o Uninformed Search: Does not rely on heuristics, so there's no risk of being misled
by incorrect estimates.
2. Complexity:
o Heuristic Search: Requires designing a heuristic function, which can be complex
and domain-specific. A well-crafted heuristic might not be obvious or easy to
implement.
o Uninformed Search: Simpler to implement as it doesn't require any domain
knowledge.
3. Optimality:
o Heuristic Search: Greedy Best-First Search, for example, may not guarantee
finding the optimal solution, whereas A* does, but at a higher memory cost.
o Uninformed Search: Algorithms like UCS and BFS are guaranteed to find the
optimal solution but at the expense of time and space.

8-Puzzle Game:
The 8-puzzle game is a classic sliding tile puzzle that consists of a 3x3 grid with 8 numbered tiles
and one empty space. The tiles are numbered from 1 to 8, and the goal is to move the tiles
horizontally or vertically into the empty space until the numbers are arranged in the correct order
(typically, tiles are arranged from 1 to 8 in row-major order with the empty space in the last
position).

Structure of the Game:

• Board: A 3x3 grid, with 8 numbered tiles and one empty tile (usually represented as 0 or
a blank space).
• Initial State: Any random arrangement of the 8 tiles and an empty space.
• Goal State: The tiles must be arranged in the following order:

Example:

You might also like