Skip to main content

rewire

Function rewire 

Source
pub fn rewire(
    graph: &Graph,
    num_trials: usize,
    loops: bool,
    seed: u64,
) -> IgraphResult<Graph>
Expand description

Randomly rewires a graph while preserving its degree sequence.

Performs num_trials switching attempts. Each trial picks two random edges (a, b) and (c, d) and attempts to replace them with (a, d) and (c, b). The swap is rejected if it would create a multi-edge or (when loops is false) a self-loop.

Returns a new graph; the original is not modified.

§Arguments

  • graph — the input graph.
  • num_trials — number of rewiring trials (not all succeed).
  • loops — if true, self-loops may be created; if false, swaps that would create self-loops are rejected.
  • seed — random seed for deterministic output.

§Examples

use rust_igraph::{Graph, rewire};

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();

let rg = rewire(&g, 100, false, 42).unwrap();
assert_eq!(rg.vcount(), 6);
assert_eq!(rg.ecount(), 5);
// Degree sequence is preserved