Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

GATE CS & DA Preparation Guide  Module 9 – Machine learning and AI (DA sections 6-7)

AI search: uninformed, informed and adversarial

Search for GATE DA: BFS, DFS, uniform-cost, iterative deepening, greedy and A* traced on one graph, heuristic tests, and minimax with alpha-beta pruning.

  • Intermediate
  • 25 minutes
  • Examples run with Python 3.14.8 and Pyodide 314.0.7
  • By MySmartCoPilot
Free core, Pro depth: 10 of 25 minutes free
Free: The frontier decides the algorithm · Uninformed searches differ in what they minimise · Greedy search and A add a heuristic · 3 more sections. With a pass: 2 in-depth parts and 3 interview answers, about 15 minutes.

What you will learn

  • Trace BFS, DFS, uniform-cost, depth-limited and iterative-deepening search, including the expansion order
  • Run greedy best-first and A* search and test heuristics for admissibility and consistency
  • Explain how tree or graph search and the timing of the goal test change what a question counts
  • Evaluate game trees with minimax and alpha-beta pruning and count the pruned leaves

Before you start

What you’ll learn · Before you start

What you will learn

  • Trace BFS, DFS, uniform-cost, depth-limited and iterative-deepening search, including the expansion order
  • Run greedy best-first and A* search and test heuristics for admissibility and consistency
  • Explain how tree or graph search and the timing of the goal test change what a question counts
  • Evaluate game trees with minimax and alpha-beta pruning and count the pruned leaves

Before you start

On this page

From S to the goal G on one small graph, breadth-first search returns a route of cost 13, while A* returns one of cost 9 after expanding only six nodes. Section 7 of the GATE DA syllabus starts with search in three kinds: uninformed, informed and adversarial. Uninformed search uses only the graph and its edge costs. Breadth-first search (BFS) expands the shallowest node first, depth-first search (DFS) the deepest, uniform-cost search (UCS) the node with the cheapest path so far, and iterative deepening search (IDS) runs depth-limited DFS with growing limits. Informed search adds a heuristic h(n), an estimate of the cost left to the goal. Greedy best-first search expands the node with the smallest h, and A* the node with the smallest f = g + h. Adversarial search scores game trees: minimax backs values up from the leaves, and alpha-beta pruning skips branches that cannot change the answer.

Not affiliated

MySmartCoPilot is a separate site from IIT Madras, IISc and the IITs, which alone decide what GATE asks.

The frontier decides the algorithm

Read the three tables row by row. The searches differ only in how their frontier: The nodes a search has generated but not yet expanded. Its order decides the search: a queue gives BFS, a stack DFS, and a priority queue on g, h or g + h gives UCS, greedy search or A*. orders the nodes waiting to be expanded, and that order decides whether each one is complete and optimal, and what it costs.

Breadth-first and depth-first search
Criterion BFSDFS
Frontier First in, first out: a queueLast in, first out: a stack
Complete Yes, if b is finiteOn finite graphs that check repeats
Optimal When all steps cost the sameNo
Time, space O(b^d), O(b^d)O(b^m), O(bm)
Iterative deepening and uniform-cost search
Criterion IDSUCS
Frontier DFS with limits 0, 1, 2 …A priority queue on g
Complete YesYes, with step costs above 0
Optimal When all steps cost the sameYes
Time, space O(b^d), O(bd)Grows with C*/ε
Greedy best-first search and A*
Criterion GreedyA*
Frontier A priority queue on hA priority queue on g + h
Complete On finite graphs that check repeatsYes, with step costs above 0
Optimal NoYes, if h is admissible (tree search)
Time, space O(b^m) at worstAs UCS at worst, when h = 0

Here b is the branching factor: b, the number of children each node of a search tree has (the largest number, when it varies). A uniform tree has 1 + b + b² + … + b^d nodes down to depth d., d the depth of the shallowest goal, m the maximum depth, C* the cheapest cost and ε the smallest step cost. Tree search may expand the same state many times, while graph search: A search that keeps a set of the states it has expanded and expands each at most once. Tree search keeps no such set, so it may expand the same state again through another path. keeps a set of expanded states and expands each at most once. Questions count expanded or generated nodes, with the goal tested on generation or on expansion.

Checkpoint

Check: The frontier and tree or graph search

  1. One question What does graph search keep that tree search does not?

    Choose one answer.

    Show the answer: One question

    Answer: A set of the states already expanded, so that each is expanded at most once

    Graph search keeps a set of expanded states and never expands one twice; tree search keeps no such set, so the same state can come back through different paths.

Uninformed searches differ in what they minimise

An undirected graph of eight nodes from S to the goal G, each edge labelled with its cost and each node with its h value.Sh = 8Ah = 9Bh = 5Ch = 4Dh = 6Eh = 3Fh = 1G (goal)h = 01453628721

The graph that all six searches run on

Text description of the diagram

Eight nodes with their heuristic values: S (h = 8), A (h = 9), B (h = 5), C (h = 4), D (h = 6), E (h = 3), F (h = 1) and the goal G (h = 0).

Undirected edges with their costs: S–A 1, S–B 4, S–C 5, A–D 3, A–E 6, B–E 2, C–G 8, D–G 7, E–F 2 and F–G 1.

Three routes matter. S–C–G has the fewest edges, 2, but costs 13. S–A–D–G costs 11. S–B–E–F–G has 4 edges and costs 9, the cheapest path.

Every search below runs from S to G with the same conventions, which any exam answer should state: neighbours in alphabetical order, ties broken alphabetically, the goal tested when a node is expanded, and graph search.

Search Expanded, in order Path Cost
BFS S A B C D E G S–C–G 13
DFS S A D G S–A–D–G 11
UCS S A B D C E F G S–B–E–F–G 9
IDS 13 expansions over limits 0, 1 and 2 S–C–G 13

BFS and IDS return the path with the fewest edges, which is not the cheapest. DFS dives through A and D and pays 11. Step through BFS, then UCS, which is Dijkstra’s algorithm run from S. Both figures run until every node is reached:

Interactive · step through it BFS from S on the lesson's graph Visit nodes in order of distance from the start, with a queue.
Graph of 8 nodes and 10 edges. queue: S.ABCDEFGSd = 0queue:Svisit order:empty
  • being compared or looked at
  • being moved or written
  • in its final place
  • the key, pivot or target
  • visited or ruled out

Pseudo-code

  1. queue ← [start]; seen ← {start}; dist(start) ← 0
  2. while the queue is not empty:
  3. u ← dequeue
  4. for each neighbour v of u:
  5. if v is not seen: seen ← seen + v; dist(v) ← dist(u) + 1; enqueue v

Breadth-first search from S: visit the nodes in order of distance (in edges) from S, using a queue. d is that distance.

When it finishes: Visit order: S A B C D E G F.

Visited 0
Edges checked 0

Interactive · step through it Uniform-cost search from S: Dijkstra's order Settle the closest unsettled node, then relax its edges; the table keeps every distance.
Graph of 8 nodes and 10 edges. queue (by dist): S 0.1453628721A∞B∞C∞D∞E∞F∞G∞S0queue (by dist):S 0
  • being compared or looked at
  • being moved or written
  • in its final place
  • the key, pivot or target
  • visited or ruled out

Pseudo-code

  1. dist(start) ← 0; dist(v) ← ∞ for every other v; add start to the queue
  2. while the queue is not empty:
  3. u ← the queued node with the smallest dist; settle u
  4. for each edge u → v with weight w:
  5. if dist(u) + w < dist(v): dist(v) ← dist(u) + w; prev(v) ← u; queue v
Distances found so far
Nodedistprev
A∞—
B∞—
C∞—
D∞—
E∞—
F∞—
G∞—
S0—

Shortest distances from S. Every node starts at ∞ except S at 0. Each round settles the queued node with the smallest distance: with no negative weights, no later path can beat it.

When it finishes: A: 1, B: 4, C: 5, D: 4, E: 6, F: 8, G: 9, S: 0.

Settled 0
Relaxations 0

Checkpoint

Check: BFS, DFS, uniform-cost and iterative deepening

  1. Question 1 of 2 BFS runs from S on the lesson's graph, neighbours in alphabetical order. Which node is expanded right after C?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: D

    The queue holds A, B, C after S, then D and E from A, nothing new from B and G from C. So the order is S A B C D E G, and D follows C.

  2. Question 2 of 2 Uniform-cost search runs from S on the lesson's graph. What is the path cost g of D when UCS expands it?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 4

    D is reached through A, at g = 1 + 3 = 4. UCS expands it after S, A and B, at the same cost as B, which wins the tie alphabetically.

Greedy search and A* add a heuristic

The same graph carries h1, the h beside each node in the figure above:

Search Expanded, in order Path Cost
Greedy S C G S–C–G 13
A* S B C E F G S–B–E–F–G 9

Greedy search is drawn to C, whose h of 4 looks closest to the goal, and pays 13 for S–C–G, whose last edge alone costs 8. UCS and A* both return the cheapest path, cost 9, but A* expands 6 nodes against 8: A’s f of 1 + 9 = 10 exceeds the cost 9, so A* never expands A, nor D behind it. Step through A*:

Interactive · stage by stage A* from S to G with h1, one expansion at a time Step through the six expansions. Each one takes the frontier entry with the smallest f = g + h.
A* from S to G with h1, one expansion at a time, step 6 of 6. Showing Expand S: f = 0 + 8, Expand B: f = 4 + 5, Expand C: f = 5 + 4, Expand E: f = 6 + 3, Expand F: f = 8 + 1, Expand G: the goal. In focus: Expand G: the goal.Expand S: f = 0 + 8frontier B 9, C 9, A 10Expand B: f = 4 + 5adds E with f = 6 + 3 = 9Expand C: f = 5 + 4adds G with f = 13 + 0Expand E: f = 6 + 3adds F with f = 8 + 1 = 9Expand F: f = 8 + 1adds G with f = 9 + 0 = 9Expand G: the goalS–B–E–F–G, cost 9

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Expand S. S has g = 0 and h = 8. Its neighbours enter the frontier with f = g + h, A at 1 + 9 = 10, B at 4 + 5 = 9 and C at 5 + 4 = 9.
  2. Expand B. B and C tie at f = 9, and the tie goes to B, alphabetically first. B adds E at g = 4 + 2 = 6, f = 6 + 3 = 9.
  3. Expand C. C and E tie at 9, and C goes first. C adds G through its costly edge, g = 5 + 8 = 13 and f = 13, which waits in the frontier.
  4. Expand E. E is next at f = 9. It adds F at g = 6 + 2 = 8, f = 8 + 1 = 9, and A again at g = 12, far worse than A's entry at 10.
  5. Expand F. F, at f = 9, adds G at g = 8 + 1 = 9 and f = 9 + 0 = 9, better than the entry of 13 through C.
  6. Expand the goal. G at f = 9 comes out next and passes the goal test, so A* returns S–B–E–F–G at cost 9 after six expansions. A, at f = 10, was never expanded, nor D behind it.

Of two admissible heuristics, the one that is at least as large everywhere dominates the other and never needs more expansions.

BFS, DFS, UCS, IDS, greedy and A* on the graph, with every expansion Python · search_trace.py
# Six searches on one invented graph from S to G: BFS, DFS, uniform-cost, iterative deepening, greedy best-first and A*.
# Conventions (state them in any exam answer): undirected edges; neighbours in alphabetical order; ties go to the
# alphabetically earlier node; the goal test happens when a node is expanded; graph search (no node is expanded twice).
import heapq
from collections import deque

edges = {("S", "A"): 1, ("S", "B"): 4, ("S", "C"): 5, ("A", "D"): 3, ("A", "E"): 6,
         ("B", "E"): 2, ("C", "G"): 8, ("D", "G"): 7, ("E", "F"): 2, ("F", "G"): 1}
h = {"S": 8, "A": 9, "B": 5, "C": 4, "D": 6, "E": 3, "F": 1, "G": 0}
graph = {}
for (u, v), c in edges.items():
    graph.setdefault(u, {})[v] = c
    graph.setdefault(v, {})[u] = c


def cost(path):
    return sum(graph[a][b] for a, b in zip(path, path[1:]))


def report(name, order, path):
    print(f"{name:<9} expanded {' '.join(order):<17} path {'-'.join(path)}, cost {cost(path)}, {len(path) - 1} edges")


def bfs(start, goal):
    frontier, reached, order = deque([[start]]), {start}, []
    while frontier:
        path = frontier.popleft()
        order.append(path[-1])
        if path[-1] == goal:
            return order, path
        for n in sorted(graph[path[-1]]):
            if n not in reached:
                reached.add(n)
                frontier.append(path + [n])


def dfs(start, goal):
    frontier, expanded, order = [[start]], set(), []
    while frontier:
        path = frontier.pop()
        if path[-1] in expanded:
            continue
        expanded.add(path[-1])
        order.append(path[-1])
        if path[-1] == goal:
            return order, path
        for n in sorted(graph[path[-1]], reverse=True):  # pushed in reverse, so the first in order is popped first
            if n not in expanded:
                frontier.append(path + [n])


def best_first(start, goal, priority):
    frontier, expanded, order = [(priority(0, start), start, [start], 0)], set(), []
    while frontier:
        _, node, path, g = heapq.heappop(frontier)
        if node in expanded:
            continue
        expanded.add(node)
        order.append(node)
        if node == goal:
            return order, path
        for n in sorted(graph[node]):
            if n not in expanded:
                g2 = g + graph[node][n]
                heapq.heappush(frontier, (priority(g2, n), n, path + [n], g2))


def ids(start, goal):
    order = []
    for limit in range(len(graph)):
        def dls(path, depth):
            order.append(path[-1])
            if path[-1] == goal:
                return path
            if depth == limit:
                return None
            for n in sorted(graph[path[-1]]):
                if n not in path:  # no cycles along the current path
                    found = dls(path + [n], depth + 1)
                    if found:
                        return found
            return None
        found = dls([start], 0)
        if found:
            return order, found, limit


report("BFS", *bfs("S", "G"))
report("DFS", *dfs("S", "G"))
report("UCS", *best_first("S", "G", lambda g, n: g))
order, path, limit = ids("S", "G")
report("IDS", order, path)
print(f"          (found at depth limit {limit}; {len(order)} expansions over all the iterations)")
report("Greedy", *best_first("S", "G", lambda g, n: h[n]))
report("A*", *best_first("S", "G", lambda g, n: g + h[n]))

Output

BFS       expanded S A B C D E G     path S-C-G, cost 13, 2 edges
DFS       expanded S A D G           path S-A-D-G, cost 11, 3 edges
UCS       expanded S A B D C E F G   path S-B-E-F-G, cost 9, 4 edges
IDS       expanded S S A B C S A D E B E C G path S-C-G, cost 13, 2 edges
          (found at depth limit 2; 13 expansions over all the iterations)
Greedy    expanded S C G             path S-C-G, cost 13, 2 edges
A*        expanded S B C E F G       path S-B-E-F-G, cost 9, 4 edges

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 search_trace.py

It prints every expansion of all six searches.

Checkpoint

Check: Greedy best-first search and A*

  1. Question 1 of 2 Greedy best-first search starts at S on the lesson's graph. Which node does it expand second, and why?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: C, because its h of 4 is the smallest among A, B and C

    After S, the frontier holds A (h = 9), B (h = 5) and C (h = 4). Greedy search takes C, and then G through the edge of cost 8.

  2. Question 2 of 2 Why does A* with h1 never expand A on the lesson's graph?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: f(A) = 1 + 9 = 10 is above 9, the cost of the path it finds first

    A enters with f = g + h = 1 + 9 = 10. A* takes entries in order of f, and the goal comes out at f = 9 first, so A, and D behind it, are never expanded.

A heuristic must never overestimate

A heuristic is admissible heuristic: A heuristic that never overestimates: h(n) is at most the true cheapest cost from n to a goal, at every node. h = 0 is admissible but gives no guidance. when it never exceeds the true cost to the goal. At A, h = 9 and the cheapest route A–E–F–G costs 6 + 2 + 1 = 9, so the bound holds with equality. A heuristic is consistent heuristic: A heuristic with h(u) ≤ c(u, v) + h(v) on every edge and h = 0 at goals. Every consistent heuristic is admissible, but an admissible one can break the inequality on some edge. when h(u) ≤ c(u, v) + h(v) on every edge, which is stronger than admissible: h1 is consistent on the edge A–D because 9 ≤ 3 + h(D) = 3 + 6. Step through the tests:

Interactive · step by step Testing h1 on the corner of the graph around A Step through one admissibility test and three consistency tests, then see why consistency implies admissibility.
Testing h1 on the corner of the graph around A, step 6 of 6. Showing A, D, E, F, G, the goal. In focus: A, G, the goal.36217Ah = 9Dh = 6Eh = 3Fh = 1G, the goalh = 0

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. The heuristic h1. These five nodes of the graph carry h1, the estimate of the cost still to go. A heuristic is admissible when it never exceeds the true cheapest cost to G.
  2. Admissible at A. From A, the cheapest route to G is A–E–F–G, 6 + 2 + 1 = 9; A–D–G costs 10. h(A) = 9 is not above 9, so h1 is admissible at A, with equality.
  3. Consistent on A–D. Consistency asks h(u) ≤ c(u, v) + h(v) on every edge. On A–D, 9 ≤ 3 + 6, so it holds.
  4. Consistent on E–F. On E–F, h(E) = 3 ≤ 2 + h(F) = 3. It holds with equality.
  5. Consistent on F–G. On F–G, h(F) = 1 ≤ 1 + 0, and h(G) = 0 at the goal, which consistency needs as well.
  6. Why consistency is stronger. Adding the inequalities along A–E–F–G gives h(A) ≤ 6 + 2 + 1 + h(G) = 9, the true cost. A consistent heuristic therefore never overestimates; an admissible one can still break the triangle on some edge.

A* tree search returns a cheapest path whenever h is admissible; A* graph search, which never expands a node twice, needs a consistent heuristic for the same guarantee.

Checkpoint

Check: Admissible and consistent heuristics

  1. Question 1 of 2 Is h1 consistent on the edge S–B, where h(S) = 8, h(B) = 5 and the edge costs 4?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Yes, because 8 ≤ 4 + 5

    Consistency asks h(u) ≤ c(u, v) + h(v) on every edge. On S–B, 8 ≤ 4 + 5 = 9, so it holds.

  2. Question 2 of 2 The cheapest route from E to the goal is E–F–G, of cost 3. Which values of h(E) keep a heuristic admissible at E?

    Choose every answer that is right.

    Show the answer: Question 2 of 2

    Answer:

    • 2
    • 0
    • 3

    Admissible means h(E) ≤ h*(E) = 3, so 0, 2 and 3 qualify and 4 does not.

Minimax backs values up, and alpha-beta skips what cannot matter

In a game tree, MAX moves at the root and MIN replies. Take two MIN nodes with the leaves 4 and 6, and 1 and 7. Minimax gives them the values 4 and 1, and MAX picks 4. alpha-beta pruning: Minimax search that keeps two bounds, α for MAX and β for MIN, and stops exploring a node once they cross. It returns the minimax value while skipping branches that cannot change it. search reaches the same 4 without looking at the leaf 7: once the second MIN node has seen 1, it is worth at most 1, and MAX already has 4. Step through it:

Interactive · step by step Alpha-beta on a tree of four leaves Step through the leaves in order and watch α, the value MAX can already get, stop the search before the last leaf.
Alpha-beta on a tree of four leaves, step 7 of 7. Showing MAX (value 4), MIN 1 (worth 4), MIN 2 (at most 1), leaf 4, leaf 6, leaf 1, leaf 7 (never read). In focus: MAX, MIN 1.MAXvalue 4MIN 1worth 4MIN 2at most 1leaf 4leaf 6leaf 1leaf 7never read

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. The tree. MAX moves at the root and MIN replies; the leaves are scores for MAX. Alpha-beta reads the leaves from left to right and keeps α, the value MAX can already be sure of.
  2. First leaf. MIN 1 reads its first leaf, 4. MIN takes the smallest value, so MIN 1 is worth at most 4.
  3. Second leaf. The second leaf is 6, and MIN prefers 4, so MIN 1 is worth exactly 4.
  4. α rises to 4. By choosing MIN 1, MAX is sure of 4, so α = 4 at the root.
  5. MIN 2 reads 1. MIN 2 reads its first leaf, 1, so MIN 2 is worth at most 1, which is below α = 4.
  6. The last leaf is skipped. Whatever the last leaf holds, MIN 2 is worth at most 1, and MAX will not choose it over 4. The leaf 7 is never read.
  7. The value. MAX picks MIN 1, and the root's value is 4, the minimax value, after reading three of the four leaves.

Alpha-beta pruning keeps two bounds, α for MAX and β for MIN, and stops exploring a node once they cross. Move ordering decides how much alpha-beta prunes. Expectimax replaces MIN nodes by chance nodes that average their outcomes.

Checkpoint

Check: Minimax and alpha-beta on a small tree

  1. Question 1 of 2 MAX moves at the root over two MIN nodes, with the leaves 5 and 8, and 2 and 9. What is the minimax value of the root?

    Type a number.

    Show the answer: Question 1 of 2

    Answer: 5

    MIN takes the smaller leaf at each node, 5 and 2, and MAX takes the larger of those, 5.

  2. Question 2 of 2 MAX moves at the root over two MIN nodes, with the leaves 5 and 8, and 2 and 9. Alpha-beta searches the tree from left to right. Which leaf does it never read?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The leaf 9

    After the first MIN node, α = 5 at the root. The second MIN node reads 2, so it is worth at most 2 < 5, and its leaf 9 cannot change the answer.

Practise: search in GATE’s formats

Practise the five ideas, starting with the one you know least.

Practice that adapts to you

Practise this lesson

12 questions about 11 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 12 Which data structure holds the frontier of breadth-first search?

    Choose one answer.

    Show the answer: Question 1 of 12

    Answer: A first-in, first-out queue

    BFS expands the shallowest node first, which a FIFO queue gives: nodes leave in the order they were added. A stack gives DFS, a queue ordered by g gives uniform-cost search and one ordered by h gives greedy search.

  2. Question 2 of 12 A uniform tree has branching factor 3. How many nodes are there from the root down to depth 2, the root included?

    Type a number.

    Show the answer: Question 2 of 12

    Answer: 13

    1 + 3 + 9 = 13. In general a uniform tree has 1 + b + b² + … + b^d nodes down to depth d, which is O(b^d).

  3. Question 3 of 12 Which node does uniform-cost search expand next?

    Choose one answer.

    Show the answer: Question 3 of 12

    Answer: The frontier node with the smallest path cost g so far

    Uniform-cost search orders the frontier by g, the cost of the path found to each node, which makes it optimal when every step cost is positive.

  4. Question 4 of 12 On the lesson's graph, BFS returns S–C–G (cost 13) and UCS returns S–B–E–F–G (cost 9). Why do they differ?

    Choose one answer.

    Show the answer: Question 4 of 12

    Answer: BFS finds the path with the fewest edges, UCS the path with the smallest total cost

    BFS minimises the number of edges, which is the cheapest path only when every step costs the same. UCS orders by g, so it returns the cheapest path, cost 9.

  5. Question 5 of 12 A frontier node has path cost g = 4 and heuristic value h = 5. What is its A* priority f?

    Type a number.

    Show the answer: Question 5 of 12

    Answer: 9

    A* orders the frontier by f = g + h = 4 + 5 = 9, an estimate of the cheapest total cost of a path through the node.

  6. Question 6 of 12 Why can greedy best-first search return an expensive path?

    Choose one answer.

    Show the answer: Question 6 of 12

    Answer: It orders the frontier by h alone and ignores the cost of the path so far

    A node that looks close to the goal can sit at the end of a costly path. A* fixes this by adding g to h.

  7. Question 7 of 12 With positive edge costs, which searches always return a cheapest path when one exists?

    Choose every answer that is right.

    Show the answer: Question 7 of 12

    Answer:

    • A* tree search with an admissible heuristic
    • Uniform-cost search

    UCS and A* with a heuristic that never overestimates expand nodes in order of a lower bound on the cost through them. Greedy search ignores the cost so far, and DFS returns the first path it reaches.

  8. Question 8 of 12 What does it mean for a heuristic to be admissible?

    Choose one answer.

    Show the answer: Question 8 of 12

    Answer: It never overestimates the true cheapest cost from a node to a goal

    Admissible means h(n) ≤ h*(n) for every node n, where h*(n) is the true cheapest cost to a goal; h = 0 is admissible but gives no guidance.

  9. Question 9 of 12 Which statements about a consistent heuristic are true?

    Choose every answer that is right.

    Show the answer: Question 9 of 12

    Answer:

    • For every edge from u to v, h(u) ≤ c(u, v) + h(v)
    • A consistent heuristic with h(goal) = 0 is admissible

    Consistency is the triangle inequality on every edge. Adding it up along a cheapest path shows that h never overestimates, so consistent implies admissible; the converse fails, which is why A* graph search needs the stronger condition.

  10. Question 10 of 12 Compared with plain minimax, what does alpha-beta pruning change?

    Choose one answer.

    Show the answer: Question 10 of 12

    Answer: Only how many leaves are read; the root's value is the same

    Alpha-beta returns the minimax value while skipping branches that cannot matter: in the small tree it reached 4 without reading the leaf 7.

  11. Question 11 of 12 MAX moves at the root and has two moves, each leading to a MIN node. The first MIN node has leaves 3 and 5, the second has leaves 2 and 9. What is the minimax value of the root?

    Type a number.

    Show the answer: Question 11 of 12

    Answer: 3

    MIN takes the smaller leaf at each node, 3 and 2, and MAX takes the larger of those, 3.

  12. Question 12 of 12 MAX moves at the root and has two moves, each leading to a MIN node. The first MIN node has leaves 3 and 5, the second has leaves 2 and 9. Alpha-beta search visits the tree left to right. How many leaves does it never examine?

    Type a number.

    Show the answer: Question 12 of 12

    Answer: 1

    After the first MIN node, MAX can guarantee 3. The second MIN node's first leaf is 2, so that node is worth at most 2, less than 3, and its leaf 9 cannot change the result.

Expansion counts, heuristic tests and A* failures, worked Pro: opens with a Pro, Premium or Ultimate pass

The same graph gives different counts under different conventions, and a heuristic can quietly break A*. This part counts both ways, tests three heuristics against the true costs and reproduces the two ways A* returns a worse path.

In this part:

  • Tree search or graph search, and when the goal is tested
  • Iterative deepening, expansion by expansion
  • Testing heuristics against true costs
  • Two ways A* returns a worse path, reproduced
  • Time and space in numbers: b = 10 and d = 5
  • Your turn: trace A* graph search
    • Trace A* graph search, expansion by expansion

1 diagram · 2 runnable examples (Python) · 1 exercise with sample tests · about 10 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Minimax and alpha-beta on a 27-leaf tree, leaf by leaf Pro: opens with a Pro, Premium or Ultimate pass

Game-tree questions ask for a value, the best move and how many leaves alpha-beta skips. This part works one tree of depth 3 and branching factor 3 in full, then shows what move ordering and chance nodes change.

In this part:

  • Minimax values backed up level by level
  • Alpha-beta traced with α and β at every node
  • Move ordering: from 17 leaves to 11, with Knuth and Moore’s count
  • Expectimax for chance nodes
  • A ten-minute set, with answers

1 diagram · 1 runnable example (Python) · about 8 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Interview questions

  1. Fresher · Warm-up

    What is the difference between an admissible and a consistent heuristic, and which does A* graph search need?

    An admissible heuristic never overestimates: at every node, h(n) is at most h*(n), the true cheapest cost from n to a goal. A consistent heuristic obeys a triangle inequality on every edge, h(u) ≤ c(u, v) + h(v), with h = 0 at goals. Consistency is the stronger condition: adding the inequality up along a cheapest path shows that a consistent heuristic is admissible, but an admissible one can break the inequality on some edge. A* tree search returns a cheapest path whenever h is admissible. A* graph search, which never expands a node twice, needs a consistent heuristic for the same guarantee. In the lesson's graph, h(A) = 9 equals the true cost from A, and the edge A–D satisfies 9 ≤ 3 + 6.

  2. Fresher to mid

    On a road map, breadth-first search and uniform-cost search return different routes between two towns. Explain why with a small example, say when the two always agree, and say which one you would use in a route planner.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  3. Fresher to mid

    Your A* route planner returns a route of cost 12 although the cheapest route costs 10. List the possible causes, show how the trace tells them apart, and fix each one.

    Model answer, Pro. First step and follow-ups

  4. Fresher to mid

    Alpha-beta search on your game tree prunes almost nothing. Why can that happen, how many leaves can perfect move ordering save for branching factor b and depth d, and how do game programs order moves in practice?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • The frontier decides the search: a queue gives BFS, a stack DFS, g gives UCS, h greedy search and g + h A*.
  • BFS and IDS find the path with the fewest edges; UCS finds the cheapest path, and so does A* with a consistent heuristic.
  • Admissible means never overestimating; consistent means h(u) ≤ c(u, v) + h(v) on every edge.
  • Minimax backs values up from the leaves, and alpha-beta returns the same value while skipping branches.

Terms in this lesson

admissible heuristic
A heuristic that never overestimates: h(n) is at most the true cheapest cost from n to a goal, at every node. h = 0 is admissible but gives no guidance.
alpha-beta pruning
Minimax search that keeps two bounds, α for MAX and β for MIN, and stops exploring a node once they cross. It returns the minimax value while skipping branches that cannot change it.
branching factor
b, the number of children each node of a search tree has (the largest number, when it varies). A uniform tree has 1 + b + b² + … + b^d nodes down to depth d.
consistent heuristic
A heuristic with h(u) ≤ c(u, v) + h(v) on every edge and h = 0 at goals. Every consistent heuristic is admissible, but an admissible one can break the inequality on some edge.
frontier
The nodes a search has generated but not yet expanded. Its order decides the search: a queue gives BFS, a stack DFS, and a priority queue on g, h or g + h gives UCS, greedy search or A*.
graph search
A search that keeps a set of the states it has expanded and expands each at most once. Tree search keeps no such set, so it may expand the same state again through another path.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.