Planning & Search in Rust
Esc

Loading…

↑↓ move ↵ open Esc close
Colour theme
All algorithms

DFS

Also known as Depth-first search

search heuristic-search

Depth-first search always follows the newest successor first, so it goes deep before it goes wide. dfs keeps every state it has seen, so it expands each state at most once and is complete on finite graphs (it finds a path if one exists). It returns some path, not a short or cheap one. Goals are tested when they are generated. Use it to check if a goal can be reached; use breadth-first search or Dijkstra's algorithm when path length or cost matters.

Pseudocode

algorithm DFS(s₀, goal)input: start state s₀, goal test goaloutput: some path to a goal, or failureif goal(s₀) thenreturn ⟨s₀⟩SEEN ← {s₀}STACK ← ⟨s₀⟩while STACK ≠ ∅ dos ← pop(STACK)NEW ← ⟨⟩for each s′ in successors(s), in the order the graph reports them doif s′ ∉ SEEN thenSEEN ← SEEN ∪ {s′}parent[s′] ← sif goal(s′) then                       ▷ goals are tested on generationreturn path-to(s′)append(NEW, s′)push NEW onto STACK in reverse order       ▷ the first reported successor is expanded firstreturn failure

In the crates

Lineage

Comes from

Nothing on this site.