Traversal Module
Graph traversal algorithms - systematic exploration of graph structure.
This module provides fundamental graph traversal algorithms for visiting nodes in a specific order. Traversals are the foundation for most graph algorithms including pathfinding, connectivity analysis, and cycle detection.
Traversal Orders
Order |
Strategy |
Best For |
|---|---|---|
Level-by-level |
Shortest path (unweighted), finding neighbors |
|
Deep exploration |
Cycle detection, topological sort, connectivity |
Core Functions
walkwithBreadthFirst/DepthFirst: Simple traversals returning visited nodes in orderfoldWalk: Generic traversal with custom fold function and metadatatopologicalSort/lexicographicalTopologicalSort: Ordering for DAGsisCyclic/isAcyclic: Cycle detection via Kahn's algorithmimplicitFold/implicitFoldBy/implicitDijkstra: Traversals for implicit graphs
Walk Control
The foldWalk function provides fine-grained control:
- Continue: Explore this node's neighbors normally
- Stop: Skip this node's neighbors but continue traversal
- Halt: Stop the entire traversal immediately
Time Complexity
All traversals run in O(V + E) linear time, visiting each node and edge at most once. Dijkstra-based traversals are O((V + E) log V).
References
Types
| Type | Description |
|
Traversal order for graph walking algorithms. |
|
|
Control flow for foldWalk traversal. |
|
|
Metadata provided during foldWalk / implicitFold traversal. |
Functions and values
| Function or value |
Description
|
||
Full Usage:
foldWalk start order initial folder graph
Parameters:
NodeId
order : Order
initial : 'a
folder : 'a -> NodeId -> WalkMetadata<NodeId> -> WalkControl * 'a
graph : Graph<'n, 'e>
Returns: 'a
|
Folds over nodes during graph traversal, accumulating state with metadata. This function combines traversal with state accumulation, providing metadata about each visited node (depth and parent). The folder function controls the traversal flow:
Time Complexity: O(V + E) for both BFS and DFS
Parameters
Use Cases
|
||
Full Usage:
implicitDijkstra start initial successors folder
Parameters:
'nid
initial : 'a
successors : 'nid -> ('nid * int) list
folder : 'a -> 'nid -> int -> WalkControl * 'a
Returns: 'a
|
Traverses an implicit weighted graph using Dijkstra's algorithm, folding over visited nodes in order of increasing cost. Like
Internally maintains a
Example
Use Cases
Multiple items
module List from Microsoft.FSharp.Collections -------------------- type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T with get member IsEmpty: bool with get member Item: index: int -> 'T with get ... val map: mapping: ('T -> 'U) -> list: 'T list -> 'U list
|
||
Full Usage:
implicitFold start order initial successors folder
Parameters:
'nid
order : Order
initial : 'a
successors : 'nid -> 'nid list
folder : 'a -> 'nid -> WalkMetadata<'nid> -> WalkControl * 'a
Returns: 'a
|
Traverses an implicit graph using BFS or DFS, folding over visited nodes with metadata. Unlike
Example
Use Cases
|
||
Full Usage:
implicitFoldBy start order initial successors visitedBy folder
Parameters:
'nid
order : Order
initial : 'a
successors : 'nid -> 'nid list
visitedBy : 'nid -> 'key
folder : 'a -> 'nid -> WalkMetadata<'nid> -> WalkControl * 'a
Returns: 'a
|
Like This is essential when your node type carries extra state beyond what
defines "identity". For example, in state-space search you might have
The Time Complexity: O(V + E) for both BFS and DFS, where V and E are measured in terms of unique keys (not unique nodes).
Example
Use Cases
Comparison to
|
||
|
Determines if a graph is acyclic (contains no cycles). This is the logical opposite of Time Complexity: O(V + E)
Example
|
||
|
Determines if a graph contains any cycles. For directed graphs, a cycle exists if there is a path from a node back to itself (evaluated efficiently via Kahn's algorithm). For undirected graphs, a cycle exists if there is a path of length >= 3 from a node back to itself, or a self-loop. Time Complexity: O(V + E)
Example
|
||
|
Performs a topological sort that returns the lexicographically smallest sequence. Uses a heap-based version of Kahn's algorithm to ensure that when multiple
nodes have in-degree 0, the smallest one (according to The comparison function operates on node data, not node IDs, allowing intuitive
comparisons like Returns Time Complexity: O(V log V + E) due to heap operations
Example
Use Cases
module String
from Microsoft.FSharp.Core
val compare: e1: 'T -> e2: 'T -> int (requires comparison)
|
||
|
Performs a topological sort on a directed graph using Kahn's algorithm. Returns a linear ordering of nodes such that for every directed edge (u, v), node u comes before node v in the ordering. Returns Time Complexity: O(V + E) where V is vertices and E is edges
Example
Use Cases
|
||
|
Walks the graph starting from the given node, visiting all reachable nodes. Returns a list of NodeIds in the order they were visited. Uses successors to follow directed paths.
Example
|
||
Walks the graph but stops early when a condition is met. Traverses the graph until
Example
|