Skip to main content

reachable_from

Function reachable_from 

Source
pub fn reachable_from(
    graph: &SqliteGraph,
    start: i64,
) -> Result<AHashSet<i64>, SqliteGraphError>
Expand description

Computes forward reachability from a start node.

Returns the set of all nodes reachable from start via outgoing edges. This answers “what does this node affect?” - the forward slice from start. The start node itself is included in the result (self-reachability).

§Arguments

  • graph - The graph to analyze
  • start - The starting node ID

§Returns

Set of all node IDs reachable from start (including start itself).

§Complexity

  • Time: O(|V| + |E|) - visits each node and edge at most once
  • Space: O(|V|) for visited set and BFS queue

§Algorithm

Uses BFS traversal following outgoing edges:

  1. Mark start as visited and add to queue
  2. While queue not empty:
    • Pop node from queue
    • For each outgoing neighbor: if not visited, mark and enqueue
  3. Return visited set

§Edge Cases

  • Empty graph: Returns empty set
  • Start not in graph: Returns set containing only start (no edges to traverse)
  • Disconnected components: Only returns nodes in start’s component
  • Cycles: Handled correctly by visited set (no infinite loops)

§Example

use sqlitegraph::{SqliteGraph, algo::reachable_from};

let graph = SqliteGraph::open_in_memory()?;
// ... build graph: 0 -> 1 -> 2 -> 3 ...

// What does node 0 affect?
let reachable = reachable_from(&graph, 0)?;
// Returns {0, 1, 2, 3, ...} - all nodes downstream from 0

// What does node 3 affect?
let reachable = reachable_from(&graph, 3)?;
// Returns {3} - only itself (no outgoing edges)