Skip to main content

random_walk_node2vec

Function random_walk_node2vec 

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

Performs a second-order biased random walk (Node2Vec) starting at start for up to steps transitions.

§Parameters

  • p — Return parameter. Higher p makes it less likely to return to the previously visited vertex. p > 1 discourages backtracking; p < 1 encourages it.
  • q — In-out parameter. Higher q biases towards BFS-like exploration (staying close to t); q < 1 biases towards DFS-like exploration (moving away from t).
  • weights — Optional edge weights (positive). None for unweighted.
  • mode — Direction mode for directed graphs (Out, In, All).
  • seed — Deterministic RNG seed.

§Returns

(vertex_chain, edge_chain) where vertex_chain[0] == start. If the walk gets stuck (no admissible outgoing edges), the result is truncated.

§Errors

Returns IgraphError::InvalidArgument if p or q are not positive finite, if start is out of range, or if weights are invalid.

§Examples

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

let mut g = Graph::with_vertices(6);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 5).unwrap();
g.add_edge(0, 2).unwrap(); // shortcut

// With q < 1: biased towards DFS (exploring further away)
let (vs, es) = random_walk_node2vec(
    &g, None, 0, DijkstraMode::Out, 10, 1.0, 0.5, 42
).unwrap();
assert_eq!(vs[0], 0);
assert!(vs.len() <= 11);