Skip to main content

random_walk

Function random_walk 

Source
pub fn random_walk(
    graph: &Graph,
    weights: Option<&[f64]>,
    start: VertexId,
    mode: DijkstraMode,
    steps: u32,
    seed: u64,
) -> IgraphResult<(Vec<VertexId>, Vec<u32>)>
Expand description

Random walk on graph starting at start, taking up to steps transitions.

weights = None selects neighbours uniformly. weights = Some(_) selects each candidate edge with probability proportional to its weight; a candidate edge with weight 0.0 is never chosen, but at least one outgoing edge of every visited vertex must have positive weight or the walk gets stuck. Negative or NaN weights return IgraphError::InvalidArgument.

Returns (vertex_chain, edge_chain). vertex_chain[0] == start; when the walk completes all steps transitions, the chain has length steps + 1 and edge_chain has length steps. If the walk gets stuck early (no admissible outgoing edges), the result is truncated to the actual prefix (matches upstream’s IGRAPH_RANDOM_WALK_STUCK_RETURN).

seed deterministically initialises the internal SplitMix64 PRNG — same (graph, start, mode, steps, weights, seed) always produces the same chain.

Counterpart of igraph_random_walk(_, &weights, _, _, start, mode, steps, IGRAPH_RANDOM_WALK_STUCK_RETURN).

§Examples

use rust_igraph::{Graph, random_walk, DijkstraMode};

// 4-cycle 0-1-2-3-0: every vertex has two neighbours so the walk
// never gets stuck.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 0).unwrap();
let (vs, es) = random_walk(&g, None, 0, DijkstraMode::Out, 5, 42).unwrap();
assert_eq!(vs.len(), 6);
assert_eq!(es.len(), 5);
assert_eq!(vs[0], 0);