Frontier, cycles and costs
Search maintains candidate paths that have not yet been expanded. This collection is called the frontier. Expanding a candidate creates paths that extend it by one permitted edge. The same room can appear at the end of several different candidates. A search tree records those different histories; the state graph has one node per represented state.
Expand a frontier by one step
Use the directed room map from the previous lesson: A → B (2), A → C (5), B → A (2), B → C (1), with C and D having no outgoing edges. The following example performs one expansion from the start and separately inspects a second expansion. Each path includes its start. We use a list to illustrate the frontier, not to implement an efficient queue or complete search strategy.
def expand(graph, path): return [path + [neighbor] for neighbor in graph[path[-1]]]
graph = {"A": {"B": 2, "C": 5}, "B": {"A": 2, "C": 1}, "C": {}, "D": {}}frontier = [["A"]]path = frontier.pop(0)frontier.extend(expand(graph, path))print(frontier)print(expand(graph, ["A", "B"]))The output is [['A', 'B'], ['A', 'C']] followed by [['A', 'B', 'A'], ['A', 'B', 'C']]. The first expansion produces two alternatives. Expanding A-B can revisit A or reach C. These are distinct path histories even when their final state matches an existing node.
Cycles, visited states and growth
A cycle returns to a state already on the current path, such as A-B-A. It can make a search tree unbounded even though the graph contains only four states. A path-local membership check can reject a repeated state on that path. A global visited or reached set records states across paths; the correct time to record them and whether to reopen them depend on the search strategy and cost rules. A cheaper route to an already reached state may matter, so never assume a global Boolean visited flag preserves minimum cost for every weighted search.
Depth counts edges from the initial state (A alone has depth 0). Branching factor describes the number of successors; in this graph the maximum is 2. A tree of maximum branching factor 2 has at most 8 nodes at depth 3, but dead ends and pruning may reduce that number. A finite graph alone does not guarantee an unrestricted tree search terminates.
Compute the cost of any legal path
Path cost is the sum of edge weights, not the number of rooms. This function accepts a complete nonempty list of room labels. It returns None for an empty list, unknown room or missing edge; a single known room has cost zero. Graph values are nonnegative integers by contract. A cost of zero is valid, so test cost is None rather than if not cost.
def path_cost(graph, path): if not path or any(state not in graph for state in path): return None total = 0 for source, destination in zip(path, path[1:]): if destination not in graph[source]: return None total += graph[source][destination] return total
graph = {"A": {"B": 2, "C": 5}, "B": {"A": 2, "C": 1}, "C": {}, "D": {}}print(path_cost(graph, ["A", "C"]))print(path_cost(graph, ["A", "B", "C"]))print(path_cost(graph, ["A", "B", "A"]))print(path_cost(graph, ["C", "A"]))The four outputs are 5, 3, 4 and None. A-B-C uses two edges but costs less than direct A-C. A-B-A is legal and costs 4, but it does not solve the goal C. C-A is illegal because the graph is directed. A solution must start at the specified initial state, follow legal edges, and end in a goal state. To judge minimum cost, compare against an established optimum; finding one legal solution does not prove optimality.
Know when failure is established
No path leads from A to isolated D. A complete search of the finite reachable graph can establish this, but a single failed candidate or an exhausted time budget cannot. Report a budget limit separately from proven unreachability. The next chapter introduces breadth-first, depth-first and uniform-cost strategies and explains the conditions for their guarantees.
Practice
Score A-C, A-B-C, A-B-A-B-C and A-D for the goal C. Mark each as legal or illegal, record its cost when legal, and identify which candidates reach C. Compare by cost rather than by the number of room labels. Then try the same goal with start C; the one-room path is a valid zero-cost solution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Frontier, cycles and costs — understanding check
Apply search foundations to the fictional directed room graph.
Starting with frontier [[A]], what is the frontier immediately after removing and expanding A?
What is the cost of A-B-C?
Why can an unrestricted search tree for the four-room map grow without bound?
Which path solves the task from C to C?
A search from A toward D stops at its time limit without finding a route or exhausting its frontier. What does this run establish?