Exercises 1 - Search

Practice for Lecture 1 and Lecture 2. These are not collected and not graded.

Throughout, assume graph search: a state is expanded at most once. These conventions decide most of the answers below and are very important to remember:

  • When the search stops. Breadth-first and depth-first search terminate as soon as the goal is generated, so the goal never appears in their expansion order. Best-first search and A* terminate when the goal is expanded, so it does.
  • Generation order. When a state is expanded, its children are generated in alphabetical order. Expanding II generates CC, then DD.
  • Expansion tie-break. When two states sitting on the frontier have the same priority, the one earlier in the alphabet is expanded first. This only ever comes up for best-first search and A*, because a plain queue and a plain stack have no priorities to tie in the first place. BFS and DFS get their order entirely from the generation rule above plus the discipline of the frontier.
  • Depth-first ordering. A stack returns the most recent thing put on it, and children go on in generation order, so DFS expands the alphabetically last child first. Expanding II generates CC and then DD, so DD is expanded next, not CC. (On the exam the tree is drawn and children are generated left to right, so this is the rightmost child. Alphabetical order plays that role here, because an edge list has no left and right.)

The state space graph below has initial state II and goal state GG. Transition costs are on the edges and apply in both directions.

1.1. Trace breadth-first search on the graph above. List the states in the order they are expanded, then give the path the search returns and that path's cost.

Solution

Expansion order: I,C,D,AI, C, D, A. Path returned: ICAGI \rightarrow C \rightarrow A \rightarrow G, cost 1717.

Expanding II generates CC and DD. Expanding CC generates AA, BB, and EE. Expanding DD generates nothing new that matters. Expanding AA generates GG, and the search terminates there.

Note that BB was generated, back when CC was expanded, but never expanded itself, and GG is never expanded at all. Being on the frontier is not the same as having been expanded, and only the expanded states go in your answer.

BFS orders by number of transitions, not by cost, so it returns the first goal it can reach in three transitions. Whether 1717 is the cheapest way to reach a goal is a separate question, and one BFS never asks. Problem 1.3 asks it.

1.2. Now trace depth-first search on the same graph, listing the states in the order they are expanded and giving the path it returns with its cost.

Solution

Expansion order: I,D,E,C,BI, D, E, C, B. Path returned: IDECBGI \rightarrow D \rightarrow E \rightarrow C \rightarrow B \rightarrow G, cost 2222.

The stack drives down the alphabetically last branch each time. Expanding II generates CC and DD, and DD comes off first. Expanding DD generates EE; expanding EE generates BB and CC, so CC comes off first. Expanding CC generates AA and BB, so BB comes off. Expanding BB generates GG, and the search terminates.

Five transitions and cost 2222, worse on both counts than what BFS found. DFS optimizes nothing at all: it returns whichever goal its stack stumbles into first, and here the stack walked most of the graph to find the long way around.

1.3. Trace best-first search, which orders its frontier by f(n)=g(n)f(n) = g(n), by filling in the table below. Each column is one expansion, in order: the top row holds the state expanded and the bottom row holds its gg, the total cost of the path from II to that state. The first column is done for you, and you may not need every column.

Expansion123456
StateII
gg0
Solution
Expansion123456
StateIICCBBDDEEGG
gg015567

Path returned: ICBGI \rightarrow C \rightarrow B \rightarrow G, cost 77.

Watch expansions 3 and 4. BB and DD are both sitting on the frontier at g=5g = 5, so the tie-break decides, and BB goes first because it is earlier in the alphabet. This is the only place on the page where that rule actually changes anything.

Note also that DD and EE get expanded even though neither is on the returned path. Best-first search has no way to know that, and problem 2.4 is about buying that knowledge with a heuristic.

1.4. BFS and best-first search returned different paths. Which one is optimal, and what exactly does the other one optimize instead?

Solution

Best-first search is optimal: ICBGI \rightarrow C \rightarrow B \rightarrow G costs 77, against 1717 for the BFS path. BFS optimizes the number of transitions rather than cost. Both paths here happen to use three transitions, but BFS commits to the first one it finds and never reconsiders, while best-first search keeps going until it is certain nothing cheaper remains. The two agree only when every transition costs the same.

Same graph, now with a heuristic hh written beside each state.

2.1. Trace A*, which orders its frontier by f(n)=g(n)+h(n)f(n) = g(n) + h(n). Fill in the table below, one expansion per column as before, with the state expanded and its gg, hh, and ff at the moment of expansion.

Expansion1234
StateII
gg0
hh5
ff5
Solution
Expansion1234
StateIICCBBGG
gg0157
hh5620
ff5777

Path returned: ICBGI \rightarrow C \rightarrow B \rightarrow G, cost 77.

Look at the very first choice. Ranked by hh alone, DD at h=5h = 5 beats CC at h=6h = 6. Ranked by ff, CC sits at 1+6=71 + 6 = 7 and DD at 5+5=105 + 5 = 10, so A* takes CC instead. Adding the one term gg reverses the decision. Problem 2.2 is about what that term is buying.

2.2. Suppose you dropped the gg term and ranked the frontier by f(n)=h(n)f(n) = h(n) alone. Would that search still be guaranteed to return the optimal path? Say what the gg term is doing for A*, and use this graph's first expansion to make the point concrete.

Solution

No. Ranking by hh alone throws away everything already spent to get where you are, so the search commits to whatever merely looks closest to the goal.

The first expansion shows it. From II the frontier holds CC at h=6h = 6 and DD at h=5h = 5, so ranking by hh takes DD, even though reaching DD costs 55 where reaching CC costs 11. A* ranks the same two by ff: CC at 1+6=71 + 6 = 7 against DD at 5+5=105 + 5 = 10, and takes CC.

That is what gg is for. It holds the search accountable for what a path has already cost, so a state that is cheap to reach but looks a little further from the goal can still win. Without it there is nothing to stop the search walking confidently in an expensive direction.

2.3. A* expanded four states; best-first search in problem 1.3 expanded six. Which two states did the heuristic save, and why were they skipped?

Solution

DD and EE. Best-first search expanded both because it ranks by gg alone, and at g=5g = 5 and g=6g = 6 they came up before the goal did at g=7g = 7.

A* never touches them. Adding hh puts DD at f=5+5=10f = 5 + 5 = 10 and EE at f=6+3=9f = 6 + 3 = 9, both well above the f=7f = 7 shared by every state on the optimal path. The heuristic is what tells A* that those two lead away from the goal, which is information best-first search simply does not have.

Problem 3: Heuristic Quality

Still the same graph, and still the heuristic hh from problem 2.

3.1. Fill in h(n)h^*(n), the true future cost from nn to the goal, for every state; the first column is done for you. Then answer: is hh admissible?

State nnIIAABBCCDDEEGG
h(n)h(n)5726530
h(n)h^*(n)7
Solution
State nnIIAABBCCDDEEGG
h(n)h(n)5726530
h(n)h^*(n)7926630

Every h(n)h(n)h(n) \leq h^*(n), so hh is admissible. It is exact at BB, CC, and EE and an underestimate at II, AA, and DD.

Two of these are worth checking carefully. h(A)=9h^*(A) = 9 by the direct edge AGA \rightarrow G, which happens to beat going back through CC and BB at 7+4+2=137 + 4 + 2 = 13. And h(I)=7h^*(I) = 7, which is exactly the cost best-first search returned in 1.3, as it must be: hh^* at the initial state is the optimal solution cost.

3.2. Suppose a classmate proposes h2h_2 with h2(D)=8h_2(D) = 8 and every other value the same as hh. Is h2h_2 admissible? Would A* with h2h_2 still have returned the optimal path on this graph?

Solution

h2h_2 is not admissible: h2(D)=8>6=h(D)h_2(D) = 8 > 6 = h^*(D), so it claims DD is further from the goal than it really is.

A* with h2h_2 would still return ICBGI \rightarrow C \rightarrow B \rightarrow G at cost 77. DD is not on the optimal path, and inflating it only makes A* avoid DD more eagerly than before.

That is the point worth taking away: inadmissibility means optimality is no longer guaranteed, not that it always breaks. You cannot test admissibility by running one example and liking the answer.

3.3. You have two admissible heuristics h3h_3 and h4h_4 for a different problem, given below, with neither better than the other everywhere. Fill in the row for the heuristic you should actually use, and say why.

StateIIAABBCCGG
h3h_352610
h4h_434630
use
Solution
StateIIAABBCCGG
h3h_352610
h4h_434630
use54630

Take max(h3,h4)\max(h_3, h_4) at each state. Every value is still at most hh^*, since each one came from a heuristic that never overestimates, so the combination stays admissible, and it is at least as close to hh^* everywhere as either input. Closer to hh^* means fewer expansions (potentially)!

3.4. Suppose we run A* on a new problem and it generates N=52N = 52 nodes, not counting the root, finding the goal at depth d=3d = 3. Estimate the effective branching factor bb^*.

Solution

Solve N+1=1+b+(b)2+(b)3N + 1 = 1 + b^* + (b^*)^2 + (b^*)^3, so 53=1+b+(b)2+(b)353 = 1 + b^* + (b^*)^2 + (b^*)^3.

There is no closed form, so bracket it. b=3.3b^* = 3.3 gives 51.151.1, which undershoots; b=3.35b^* = 3.35 gives 53.253.2, which just overshoots. So b3.35b^* \approx 3.35.

A heuristic reporting b=1.2b^* = 1.2 on the same problem would be dramatically better: its search tree is nearly a single path to the goal.

Problem 4: Conceptual Miscellany

These do not depend on the graph above.

4.1. A classmate argues that best-first search does not need a priority queue:

We already keep a graveyard, so we never expand the same state twice. That is what stops us from wasting work. A plain FIFO queue would return the same path.

What is wrong with this argument?

Solution

The two structures do different jobs, and the graveyard cannot cover for the priority queue.

The graveyard prevents repeated work. Without it a search can expand the same state again and again, but it says nothing about which state to expand next.

The priority queue is what decides which state to expand next, and ordering by gg is what makes the first goal expanded the cheapest one. Swap in a FIFO queue and you no longer have best-first search, you have breadth-first search, which orders by number of transitions instead. Whenever transitions cost different amounts, that returns a different path, and usually a worse one. Problems 1.1 and 1.3 are exactly this case: BFS returned a path costing 1717 where best-first search returned one costing 77, and both searches kept a graveyard the whole time.

So the classmate has kept the mechanism that saves work and thrown away the mechanism that guarantees optimality on non-uniform cost problems.

That qualifier matters, because on a uniform cost problem the classmate would be right. When every transition costs the same, fewest transitions and cheapest are the same thing, so BFS is optimal and the priority queue buys nothing. The priority queue earns its keep exactly when costs differ.

4.2. For each situation, name the search strategy you would use and give a one-line reason.

  1. All transitions cost the same, and memory is tight.
  2. Transition costs vary and you have no idea how to estimate distance to the goal.
  3. Transition costs vary and you have an admissible, consistent heuristic.
  4. All transitions cost the same and you know the goal is exactly five transitions deep.
Solution
  1. Iterative deepening: it keeps only the current path in memory, like depth-first search, but because it tries every depth limit in increasing order it still returns the shallowest goal, which with uniform costs is the cheapest one. Plain depth-first search would fit the memory budget too, but it would return whichever goal it happened to reach first.
  2. Best-first search with f(n)=g(n)f(n) = g(n): it is optimal for varying costs and needs no heuristic.
  3. A*: the heuristic cuts expansions, and admissibility plus consistency keeps it optimal under graph search.
  4. Depth-limited search with limit 55: it gets depth-first memory without the risk of diving past the goal, and there is no reason to pay for the repeated shallower passes iterative deepening would make when you already know the depth.

4.3. A* keeps both gg and hh for every state on the frontier, and its frontier is a priority queue rather than a plain queue or stack. Name two ways this makes A* more expensive than breadth-first search.

Solution

Bookkeeping per state. Every frontier entry stores gg, and hh has to be computed for it, where BFS stores nothing beyond the state and the path that reached it.

Cost per frontier operation. A priority queue costs O(logn)O(\log n) per insertion and removal, where a plain queue or stack is O(1)O(1).

Neither of these changes how many states get expanded, which is the thing A* is actually trying to reduce. So a heuristic is only worth using when the expansions it saves outweigh the overhead it adds on every state that remains.

Practice: admissibility and consistency

Each instance gives you a fresh state space graph, edge costs, and a heuristic. Fill in hh^*, decide both properties, and supply a witness where one fails. The checker accepts any genuine witness, not just the one it had in mind, and remember that the consistency inequality is directional.

Practice: search traces

Each instance draws a fresh search tree and asks for all five strategies at once, the way the exam does. Two conventions decide most of the answer, so read them each time: children are generated left to right, which means a stack expands the rightmost child first, and the strategies with a priority-queue frontier goal-test at expansion while the others goal-test at generation. Only the first group ever expands its own goal.

Practice: admissible combinations

Given two admissible heuristics, decide whether a combination of them is still admissible. The test that settles every row is the extreme case: if both inputs are exactly hh^*, can the expression come out above hh^*?

Practice: completeness and optimality

For a strategy applied to a class of problem, decide whether completeness is guaranteed and whether optimality is. Both halves of each row matter: a strategy can be complete without being optimal, and the problem class is doing as much work in the question as the strategy is.

Created · Updated
Copyright © 2026 Jared Coleman. All rights reserved.