Skip to main content

connect_neighborhood

Function connect_neighborhood 

Source
pub fn connect_neighborhood(graph: &Graph, order: u32) -> IgraphResult<Graph>
Expand description

Returns a new graph where each vertex is connected to all vertices reachable within order steps in the original graph.

Existing edges are preserved. Only new edges (not already present) are added. Self-loops are never added. For undirected graphs, only one edge per pair is created.

This is equivalent to computing the k-th power of a graph and simplifying (removing multi-edges and self-loops).

§Arguments

  • graph — the input graph (undirected).
  • order — the maximum distance within which vertices are connected. Order < 2 leaves the graph unchanged.

§Examples

use rust_igraph::{Graph, connect_neighborhood};

// Path graph: 0-1-2-3
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();

let cg = connect_neighborhood(&g, 2).unwrap();
assert_eq!(cg.vcount(), 4);
// Original 3 edges + new edges: (0,2), (1,3) = 5 total
assert_eq!(cg.ecount(), 5);