Heuristic Search - Notes with Working Examples
1. Generate and Test
Definition:
Generate and Test is a brute-force search strategy that generates possible solutions and tests each one to
determine whether it satisfies the goal condition.
Steps:
1. Generate a possible solution.
2. Test if it is a valid solution.
3. If yes, return it. Else, repeat.
Example:
Goal: Find a number between 1 and 10 that is divisible by 7.
Algorithm:
for i in range(1, 11):
if i % 7 == 0:
return i
Output: 7
2. Best First Search
Definition:
Best First Search is a search algorithm that explores a graph by expanding the most promising node chosen
according to a specified rule.
Steps:
1. Place the starting node on the open list.
2. Loop until goal is found or open list is empty:
Heuristic Search - Notes with Working Examples
- Pick the node with the best heuristic.
- Expand it and add its successors to the open list.
Example:
Graph:
A --1--> B --3--> D
A --4--> C --1--> D
Heuristic h(D)=0, h(B)=3, h(C)=1, h(A)=4
Start=A, Goal=D
Order of Expansion: A -> C -> D
3. Beam Search
Definition:
Beam Search is a heuristic search algorithm that explores a graph by expanding only a fixed number of best
nodes at each level (beam width).
Steps:
1. Start at root node.
2. Expand all children but keep only top 'k' best nodes based on heuristic.
Example:
Let beam width = 2
From A, suppose it expands B, C, D with heuristics 5, 2, 4.
Keep C and D for next level since they are best 2.
Heuristic Search - Notes with Working Examples
4. Hill Climbing
Definition:
Hill Climbing is a local search algorithm that continuously moves towards the direction of increasing value
(higher heuristic).
Steps:
1. Start with an initial solution.
2. Loop:
- Evaluate neighbors.
- Move to neighbor with best improvement.
Problem: Can get stuck at local maxima.
Example:
f(x) = -x^2 + 4x
Start at x = 0
Check x=1 -> f(1)=3, x=2 -> f(2)=4, x=3 -> f(3)=3
Stop at x=2
5. A* Search
Definition:
A* Search is a pathfinding algorithm that uses both actual cost from start (g) and estimated cost to goal (h):
f(n) = g(n) + h(n).
Heuristic Search - Notes with Working Examples
Steps:
1. Add start node to open list with f = g + h.
2. Loop:
- Pick node with smallest f.
- If goal, stop.
- Else, expand and compute f for successors.
Example:
Graph: A--1-->B--3-->D, A--4-->C--1-->D
Heuristics: h(D)=0, h(B)=3, h(C)=1, h(A)=4
Node | g | h | f
------------------
A |0|4|4
B |1|3|4
C |4|1|5
D | 5 | 0 | 5 (via B)
Path: A -> B -> D