Skip to main content

get_stochastic

Function get_stochastic 

Source
pub fn get_stochastic(
    graph: &Graph,
    column_wise: bool,
    weights: Option<&[f64]>,
) -> IgraphResult<Vec<Vec<f64>>>
Expand description

Compute the stochastic adjacency matrix of a graph.

The stochastic matrix is the adjacency matrix normalized so that each row (or column) sums to 1. Row-normalized = right-stochastic = random walk following edge directions. Column-normalized = left-stochastic = random walk against edge directions.

When a vertex has zero out-degree (or in-degree for column-wise), the corresponding row (or column) will be all zeros.

  • column_wise: if false, row-normalize (right-stochastic); if true, column-normalize (left-stochastic).
  • weights: optional edge weights. If None, each edge counts as 1.

Returns an n×n matrix as Vec<Vec<f64>>.

§Examples

use rust_igraph::{Graph, get_stochastic};

// Path 0-1-2: row-stochastic
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
let s = get_stochastic(&g, false, None).unwrap();
// Row 0 sums to 1: only edge to vertex 1
assert!((s[0][1] - 1.0).abs() < 1e-10);
// Row 1: edges to 0 and 2, each 0.5
assert!((s[1][0] - 0.5).abs() < 1e-10);
assert!((s[1][2] - 0.5).abs() < 1e-10);