Skip to content

dfs

dfs<TNode>(root, traits): boolean

Defined in: graph/dfs.ts:54

Walks a graph or tree of unknown shape depth-first, in pre-order.

The structure is not required to conform to any shared node interface: childrenOf is the only thing that has to know how a given TNode exposes its successors, which lets one generic walker drive discriminated unions whose node kinds carry children under entirely different field names.

Time complexity: O(V + E). Without DfsTraits.seen, a cyclic graph does not terminate — pass a Set whenever a cycle is possible.

TNode

TNode

The node to start walking from.

DfsTraits<TNode>

How to traverse, and what to do at each node.

boolean

true if the walk was stopped early via DfsWalkStop, false if it ran to completion.