Skip to main content

random_walks

Function random_walks 

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

Generate multiple random walks from every vertex in the graph.

For each vertex v in 0..graph.vcount(), generates walks_per_vertex walks of length walk_length starting at v. The order of starting vertices is shuffled independently for each round using the deterministic PRNG.

§Parameters

  • graph — The input graph.
  • weights — Optional edge weights (positive). None for unweighted.
  • mode — Direction mode for directed graphs.
  • walks_per_vertex — Number of walks to generate per vertex.
  • walk_length — Number of steps per walk.
  • seed — Deterministic PRNG seed.

§Returns

A Vec<Vec<VertexId>> where each inner vector is a walk (vertex sequence of length ≤ walk_length + 1). Total number of walks is vcount * walks_per_vertex (unless walks get stuck early).

§Examples

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

let g = Graph::from_edges(
    &[(0,1),(1,2),(2,3),(3,0)], false, Some(4)
).unwrap();
let corpus = random_walks(&g, None, DijkstraMode::Out, 2, 5, 42).unwrap();
assert_eq!(corpus.len(), 8); // 4 vertices * 2 walks
for walk in &corpus {
    assert!(walk.len() <= 6); // at most walk_length + 1
    assert!(walk[0] < 4);
}