Informed search uses problem-specific knowledge, in the form of a heuristic, to reach the goal much faster than blind search.

Heuristic Functions

A heuristic function h(n) estimates the cost of the cheapest path from node n to a goal, with h(goal) = 0. It encodes extra knowledge. For route finding, h can be the straight-line distance to the destination. For the 8-puzzle, h₁ is the number of misplaced tiles and h₂ is the sum of Manhattan distances of the tiles from their goal places; h₂ is more informed since it is always at least h₁. A heuristic is admissible if it never overestimates the true cost; it is consistent (monotone) if h(n) ≤ c(n, n′) + h(n′) for each successor n′, which implies admissibility. A good heuristic reduces the search effort greatly, and heuristics can be obtained from relaxed problems (fewer constraints).

Definition: Heuristic function. A function h(n) that estimates the cost of the cheapest path from node n to a goal; it is admissible if it never overestimates that cost.

Best-first search is a general scheme in which the frontier is a priority queue ordered by an evaluation function f(n); the node with the lowest f is expanded first. Different choices of f give different algorithms: uniform-cost search uses f(n) = g(n); greedy best-first search uses f(n) = h(n); A* uses f(n) = g(n) + h(n).

Greedy best-first search expands the node that appears closest to the goal, f(n) = h(n), ignoring the cost already paid. It is usually fast and finds a solution quickly, but it is not optimal (it may take a costly road) and not complete in graphs with loops without an explored set. Worst-case time and space are O(bᵐ), though a good heuristic improves this substantially. In route finding it heads straight towards the destination but can be lured into dead ends.

Hill Climbing as a Local Method

A relative of greedy search is hill climbing, which keeps only the current state and moves to the neighbouring state with the best heuristic value, stopping when no neighbour is better. It needs very little memory but may be trapped at local maxima, plateaus and ridges; random restarts and simulated annealing reduce the problem.

Table 2.3: Evaluation functions

Algorithm f(n) Property
Uniform-cost g(n) Optimal, uninformed
Greedy best-first h(n) Fast, not optimal
A* g(n) + h(n) Optimal if h admissible
Hill climbing h of neighbours Local, can get stuck

Example 2.3: Greedy search on a small graph

Problem: Edges: S-A (1), S-B (4), A-B (2), A-C (5), B-C (1), C-G (3). Heuristics: h(S) = 6, h(A) = 5, h(B) = 3, h(C) = 3, h(G) = 0. Run greedy best-first from S.

Solution:

  1. Expand S: A (h = 5), B (h = 3). Choose B, the lowest h.
  2. Expand B: neighbours A (5) and C (3) are in the frontier; choose C.
  3. Expand C: G (h = 0) is chosen and is the goal.
  4. Path S → B → C → G has cost 4 + 1 + 3 = 8.

Result: Greedy returns a path of cost 8, but the optimal path S → A → B → C → G costs 1 + 2 + 1 + 3 = 7.

import heapq
g = {"S": {"A": 1, "B": 4}, "A": {"S": 1, "B": 2, "C": 5},
     "B": {"S": 4, "A": 2, "C": 1}, "C": {"A": 5, "B": 1, "G": 3},
     "G": {"C": 3}}
h = {"S": 6, "A": 5, "B": 3, "C": 3, "G": 0}
pq, seen = [(h["S"], "S", ["S"], 0)], set()
while pq:
    _, n, path, cost = heapq.heappop(pq)
    if n == "G":
        print("greedy path", path, "cost", cost)
        break
    if n in seen:
        continue
    seen.add(n)
    for m, w in g[n].items():
        heapq.heappush(pq, (h[m], m, path + [m], cost + w))

Output

greedy path ['S', 'B', 'C', 'G'] cost 8

Common mistakes

  • Using an inadmissible heuristic and expecting an optimal result.
  • Believing greedy search is optimal because it looks closest to the goal.
  • Confusing g(n), the cost so far, with h(n), the estimated cost remaining.
  • Forgetting that h(goal) must be 0.

Key takeaways

  • h(n) estimates the remaining cost; admissible means it never overestimates.
  • Best-first search orders the frontier by f(n) in a priority queue.
  • Greedy uses f = h: fast but not optimal.
  • Heuristics come from relaxed problems; Manhattan distance beats misplaced tiles.

Practice questions

  1. What is an admissible heuristic?
  2. What evaluation function does greedy best-first search use?
  3. Which is the more informed 8-puzzle heuristic?

Answers to the practice questions

  1. One that never overestimates the true cost to the goal.
  2. f(n) = h(n).
  3. The sum of Manhattan distances, h₂, which is always at least the misplaced-tiles count.

Sources

  • Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit II