A Visited Set in a Search

A visited set remembers explored states so a search can avoid repeatedly expanding the same state.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a small cycle
  2. Pick the right search identity
  3. Mark a discovered state at a consistent point
  4. Include all state needed for future moves

Trace a small cycle

From A you can reach B, and B links back to A. Marking A before exploring B prevents repeated expansion of A on return. Set membership stores whether the search has seen a key.

Pick the right search identity

If a game state contains location and remaining steps, visiting the same location with more remaining steps may represent new work. Use a composite key for all components that affect future actions. Include a test where two paths reach one location with different budgets.[1]

Mark a discovered state at a consistent point

In a graph with edges A to B, A to C, B to D and C to D, a breadth-first search can queue D once by marking it when enqueued from B. Marking only when removed from the queue can enqueue D from C as well. A separate check on removal can still give a correct traversal, with more queue work. Test this diamond along with a self-loop. See adjacency lists.

Include all state needed for future moves

If a route can traverse a barrier only after finding a key, reaching location L with and without the key represents two different states. A visited set keyed only by L can discard a viable route because a prior visit lacked the key. Use a stable pair of location and inventory condition. Test a route requiring a return to L after the key is found. See composite keys.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩