Chapter 2
State Space Search and Heuristic Technique
Contents
› Informed search strategies.
– The best-first search
– A* Algorithm
– AO* Algorithm
› Uninformed search strategies.
– Breadth-first search
– Uniform cost search
– Depth-first search
– Bidirectional search
› Generate and Test Strategy.
– Hill Climbing
– Mean end analysis
State Space Search
State Space Search is a way to solve problems in Artificial Intelligence (AI).
You start from a starting point and try different steps to reach the goal.
Example: You are in a maze.
The starting point is where you begin.
The goal is the exit of the maze.
Each step or move (left, right, forward) is an action.
The search means trying different paths to get out of the maze
Term Meaning
A situation or position. Example:
State
Where you are in the maze.
Initial State Where you start.
Goal State Where you want to reach.
A move you can make. Example:
Action
Step left or right.
Path A series of actions to reach the goal.
What is a Heuristic Function
What is a Heuristic Function?
Definition:
A heuristic function (usually denoted as h(n)) is a method that gives an estimated cost from
the current state n to the goal state.
It's a way of guiding the search intelligently, making it faster and more efficient.
State Space Search Technique
There are 2 types of search techniques:
1). Uninformed Search Technique
2). Informed (Heuristic) Search Technique
Uninformed Search Technique
Uninformed Search Technique
Uninformed search means the algorithm has no extra knowledge about the goal's location.
It simply explores all possible paths without any clue and treats every option equally.
These methods are simple but can be slow
Types of Uninformed Search:
Depth First Search (DFS) – Explores one path deeply before backtracking.
Breadth First Search (BFS) – Explores all possible moves at each level before going deeper.
Uniform Cost Search (UCS) – Chooses the path with the lowest total cost.
Bidirectional Search – Searches forward from the start and backward from the goal, meeting in the middle.
Depth First Search (DFS)
DFS = "Drill deep first!
Core Principle:
Explore as far as possible along a single branch before backtracking.
Think of it like navigating a maze: take one path until you hit a dead end, then backtrack to the last junction.
Key Terminologies:
Backtracking:
When you reach a node with no unvisited neighbors, retreat to the previous node to explore alternative paths.
Stack (LIFO):
DFS uses a stack (last-in-first-out) to track nodes.
Visited Marking:
Nodes should be marked as "visited" to avoid reprocessing and infinite loops.
Depth First Search (DFS)
Traversal Process:
Start at the root node (or any chosen starting point).
Move to an unvisited neighbor, prioritizing one direction (e.g.,
leftmost).
Continue deeper until a dead end (node with no unvisited
neighbors) is reached.
Backtrack to the most recent node with unvisited neighbors and
repeat.
Depth First Search (DFS)
Advantages of DFS (Depth-First Search)
Uses Less Memory
DFS stores only the current path (stack), so it needs less memory than BFS.
Finds Deep Solutions Quickly
Good when the solution is far from the starting point (deep in the graph/tree).
Useful for Solving Certain Problems
Good for: Detecting cycles in a graph
Backtracking Support
DFS naturally supports backtracking.
Depth First Search (DFS)
Disadvantages of DFS
May Not Find the Shortest Path
DFS can find a solution, but it may not be the best or shortest one.
Can Get Stuck in Infinite Loops
If the graph has cycles and visited nodes are not tracked, DFS may keep going forever.
Breadth-First Search (BFS)
Use BFS for finding the closest solution
Core Principle:
Explore all neighbors at the current level before moving deeper.
Key Terminologies:
Queue (FIFO): BFS uses a queue (first-in-first-out). The oldest unvisited node is processed next.
Level Order: Nodes are processed layer by layer: root (level 0), its neighbors (level 1), their neighbors (level 2), etc.
Visited Marking: Nodes are marked "visited" when enqueued to prevent duplication.
Breadth-First Search (BFS)
Traversal Process:
Start at the root node and add it to the queue.
Remove the front node from the queue, visit it,
and enqueue all its unvisited neighbors.
Repeat until the queue is empty.
Breadth-First Search (BFS)
Advantages of BFS:
Easy to Implement Using Queue
BFS is simple to code using a queue data structure (FIFO – First In, First Out).
Level-by-Level Exploration
It explores nodes in levels, which is useful for problems like:
Finding all nodes at a certain depth (e.g., all friends 2 steps away in a social network)
Shortest transformation sequences (e.g., word ladder problems)
Can Be Used for Cycle Detection in Undirected Graphs
BFS can help detect whether a graph contains cycles.
Breadth-First Search (BFS)
Disadvantages of BFS:
Not Suitable for Memory-Constrained Systems:
BFS uses memory proportional to the number of nodes at a level, so not good for low-memory
environments.
Poor for Problems with Very Deep Solutions:
If the solution is far from the root, BFS becomes slow and memory-heavy.
Uniform Cost Search (UCS)
UCS is a search algorithm that expands the least-cost node
first
It uses a priority queue where nodes are ordered by the total
cost from the start node.
Traversal Order:
Nodes are visited in increasing order of path cost. So UCS
behaves like BFS, but it chooses the cheapest cost path rather
than the shallowest path.
Uniform Cost Search (UCS)
Advantage:
Complete Search
If there is a solution, UCS will definitely find it, provided the branching factor is finite and step costs are
positive.
Handles Varying Costs
Unlike BFS or DFS, UCS can handle graphs with unequal edge costs.
Uniform Cost Search (UCS)
Disadvantage:
Slow for Large Graphs
UCS can be very slow because it explores many nodes with low costs before reaching the goal, especially if
the goal is deep.
High Memory Usage
UCS maintains a priority queue (frontier) of all possible paths, which can consume a lot of memory.
Explores Unnecessary Paths
Bi-directional Search
Bidirectional Search is a graph search algorithm that runs two
simultaneous searches:
One forward from the start node
One backward from the goal node
The search ends when the two searches meet in the middle,
Bi-Directional Search
Advantage:
Faster Than BFS
Especially in large graphs, since it reduces the depth explored by half.
Optimal Solution
If both searches use BFS, the result is the shortest path.
Bi-Directional Search
Disadvantage:
Requires Both Start and Goal
You must know the goal state in advance and be able to search backward from it.
Difficult in Complex State Spaces
For example, in puzzles or planning problems, it's hard to define how to move "backward."
Informed Search Technique
A* Algorithm
A* (pronounced A star) is an informed search algorithm that finds the shortest path from a start node to
a goal node using a combination of:
Actual cost from start (g(n))
Estimated cost to goal (h(n)) — heuristic
f(n)=g(n)+h(n)
g(n) = actual cost from the start to node n (Edge value)
h(n) = estimated cost from node n to goal (heuristic) (Node value)
f(n) = total estimated cost of the path through n
Advantage:
Optimal
A* finds the shortest path if the heuristic h(n) is:
Admissible (never overestimates the cost to reach the goal)
Consistent (also called monotonic)
Efficient
A* is faster than UCS or BFS because it uses a heuristic to guide the search
Disadvantage:
High Memory Usage
A* stores all possible paths in memory (priority queue), which can grow large for big graphs.
Heuristic Design
A bad heuristic can make A* slow or even behave like uninformed search (UCS).
AO* Algorithm
The AO* (And-Or Star) algorithm is a heuristic search algorithm used in AND-OR graphs, where:
Some nodes require one child to be solved (OR nodes).
Some nodes require multiple children to be solved together (AND nodes)
f(n)=g(n)+h(n)
g(n) = actual cost from the start to node n (Edge value)
h(n) = estimated cost from node n to goal (heuristic) (Node value)
f(n) = total estimated cost of the path through n
Advantage:
Handles Complex Problems
Solves AND-OR problems that other search methods like A*, BFS can’t.
Optimal with Admissible Heuristics
Like A*, it finds the least-cost solution if the heuristic is admissible.
Efficient Backtracking
Updates previous nodes dynamically when better paths are found.
Disadvantage:
Difficult to Implement
Managing AND/OR combinations and updating costs recursively is complex.
Memory Intensive
Needs to maintain and update multiple partial solution trees.
Best First Search Algorithm
Best-First Search is an informed search algorithm that explores the most promising node first based on a
heuristic function h(n).
It uses a priority queue, where nodes with the lowest heuristic cost are given priority.
Choose the node that "looks best" — the one that seems closest to the goal, based only on estimated cost
(h(n)), not actual cost (g(n)).
f(n)=h(n)
h(n) = estimated cost from node n to the goal
No g(n) (actual cost so far) is considered.
Advantage:
Faster Than Uninformed Search
Uses heuristic to guide the search, so it can find a solution quicker than DFS or BFS.
Simple to Implement
Only needs a heuristic, not actual path cost tracking.
Good for Large Graphs.
Disadvantage:
Not Optimal
Since it ignores path cost, it may not find the shortest path.
Incomplete
If the graph is infinite or has loops, it might get stuck.
Generate and Test Method (Informed search)
Also there are 2 approaches to generate solution :
Generating Complete Solutions
Generating Random Solutions
It is one of the Simplest Approach out of all It works in two modules
1. Generate Module
Step-1 Generate all solutions
Step-2 Select one solution
2. Test Module
Step-3 If solution is found then select it otherwise again go to step 1 and find another solution
Hill Climbing (The "Greedy Climber")
What it is: Always choose the next step that gives you the immediate biggest improvement towards your goal,
like climbing a hill by always going steepest uphill. It focuses only on the very next step.
How it works:
Look Around: From your current position, look at all the possible small changes/next steps you could take.
Pick the Best Now: Choose the single step that improves your situation the most right this second (makes you
"higher" on the hill).
Move: Take that step.
Repeat: Keep doing steps 1-3 until no step makes you better off – you're at the top (of that hill)!
Pros: Simple, fast to make decisions in the short term. Often makes quick initial progress.
Local Maximum
Think of a small hill.
The algorithm climbs and reaches the top of this hill.
It feels like the top, but it's not the highest.
Example: You find a good answer, but a better one exists somewhere else.
Global Maximum
This is the highest hill (best solution).
The goal of hill climbing is to reach here.
It's the best answer possible in the entire area.
Plateau (Flat Local Maximum)
Imagine a flat area with no ups or downs.
The algorithm doesn’t know which way to go because all nearby points are
equal.
It gets confused and may stop or wander randomly.
Ridge
A narrow slope that looks like a peak.
The algorithm may think it's the best spot and stop too soon, even though a better place is nearby.
Current State
The place where the algorithm is right now while searching.
It keeps changing as it tries to find better answers.
Shoulder
A flat area with a hidden path going up.
If the algorithm keeps trying, it can find a better answer just beyond the edge.
Term Meaning in Simple Words
Local Maximum Good answer, but not the best
Global Maximum The best answer in the whole area
Plateau Flat area – hard to choose where to go
Ridge Narrow high area – may look like a peak but isn’t
Shoulder Flat area with a hidden better path
Current State Where the algorithm is searching right now
Means-End Analysis
It is a problem-solving technique used in Artificial Intelligence (AI).
•Goal: Reduce the difference between the current state (where you are now) and the
goal state (where you want to be).
•Idea: Break the problem into smaller sub-problems and solve them step-by-step.
•Process:
• Identify the difference between the current state and goal state.
• Select an action (means) that can reduce that difference.
• Apply the action and get a new current state, Repeat until the goal is reached.
Example:
Current state: You have no cake.
Goal state: You want a chocolate cake.
Difference: Ingredients + baking.
Means: Go to the store → Buy ingredients → Mix → Bake.
After each step, you’re closer to the goal.
Q&A