Lawfare Group → Knowledge Institute → Lawfare Academy
← CS 201 — Data Structures, Algorithms and Systems
Weeks 12–14
Graphs, search and planning
Learning objectives
- Explain and trace the mechanisms in graphs, search and planning.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
A graph models entities as vertices and relationships as edges. The interpretation of an edge determines the problem. A road can have a distance or travel time; a course prerequisite edge indicates dependency rather than physical proximity. Directed and undirected graphs answer different questions.
Breadth-first search explores a graph by increasing number of edges from a start vertex. With equal edge costs, its first discovered path to a vertex has minimum edge count. For unequal positive costs, minimum hops need not mean minimum distance; use a suitable weighted-search algorithm. Track visited states when enqueuing to prevent repeated expansion and cycles. The identity of a state must include all relevant information: location alone is insufficient if fuel or permissions change possible actions.
A heuristic estimates remaining cost. An optimistic admissible heuristic supports certain optimality arguments for A*, but implementation choices, reopening nodes and consistency assumptions matter. A plausible-looking estimate is not automatically admissible. Before adopting sophisticated search, construct a tiny counterexample and establish a baseline. A correct BFS on an unweighted graph is more useful than an improperly specified heuristic search.
Worked reasoning The initial state contains A in both seen and the queue. Expanding A discovers B. Expanding B ignores already-seen A and discovers C. C has no listed outgoing edges. The result includes A because a zero-edge path reaches the start. The function returns a reachable set, not actual paths or weighted distances; producing those requires additional parent or cost records.
Extended teaching and laboratory
Breadth-first search discovers vertices in nondecreasing number of edges from the start when all edges have equal cost. A visited set prevents a cycle from causing repeated exploration. Mark on enqueue so several parents do not schedule the same vertex repeatedly. A parent mapping can reconstruct a discovered path, while a distance mapping records its edge count.
A reachable set does not contain enough information to reconstruct every path. A BFS shortest-path guarantee concerns edge count, not arbitrary travel cost. Introducing weights changes the optimization problem and may require a different algorithm and assumptions about edge signs.
Worked problem Graph edges are A→B, A→C, B→D, C→D and D→A. Trace BFS from A, enqueueing B before C, and reconstruct one shortest path to D.
Step-by-step reasoning 1. Begin queue [A], seen {A}, distance[A]=0. Expanding A enqueues B and C with distance 1 and parent A.
- Expand B to discover D at distance 2 with parent B. Expanding C sees D already discovered and does not enqueue it again.
- D’s edge back to A does not repeat A. Following parents gives D←B←A, hence path A,B,D.
- A,C,D is another path with the same edge count. The chosen tie order does not make it uniquely optimal.
Apply the idea
Graph edges are A→B, A→C, B→D, C→D and D→A. Trace BFS from A, enqueueing B before C, and reconstruct one shortest path to D.
Assign weights A→B=100, B→D=1, A→C=2, C→D=2. Does the earlier BFS parent path minimize cost?
Read the worked reasoning
No. A,B,D costs 101 while A,C,D costs 4, although both use two edges. BFS remains correct for minimum edge count but does not solve this weighted-cost objective. A suitable nonnegative-weight shortest-path algorithm can compare cumulative costs; the objective and weight assumptions must be documented.
Check understanding
When does BFS provide minimum-cost paths?
- When every edge has the same nonnegative cost
Feedback
Equal costs make hop count proportional to total cost.
- For arbitrary positive edge weights
Feedback
Unequal weights break the hop-count equivalence.
- Whenever vertices have descriptive names
Feedback
Labels do not establish an optimality guarantee.
Further reading
Preparation: study 4.1–4.2 and 4.5: graphs, trees and traversals in the complete on-site Levin text. Attempt the investigation, work at least four exercises of increasing difficulty in each assigned section, and compare the selected hints/solutions only after a written attempt. Then complete the algorithm reading and implement the unit laboratory. For each algorithm give a specification, trace, correctness argument and time/space analysis.