Skip to main content

induced_subgraph

Function induced_subgraph 

Source
pub fn induced_subgraph(
    graph: &Graph,
    vids: &[VertexId],
) -> IgraphResult<InducedSubgraphResult>
Expand description

Creates the subgraph induced by the specified vertices.

Collects the specified vertices and all edges between them into a new graph. Vertex IDs in the result are contiguous starting from 0 and follow the order of the original graph (sorted by increasing old ID).

Duplicate vertex IDs in vids are silently ignored (only the first occurrence counts). The vertex order in the output always matches the ascending order of original IDs regardless of the order in vids.

§Arguments

  • graph — the input graph.
  • vids — slice of vertex IDs to include in the subgraph.

§Errors

Returns InvalidArgument if any vertex ID in vids is >= graph.vcount().

§Examples

use rust_igraph::{Graph, induced_subgraph};

let mut g = Graph::with_vertices(5);
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(0, 4).unwrap();

let result = induced_subgraph(&g, &[0, 1, 2]).unwrap();
assert_eq!(result.graph.vcount(), 3);
assert_eq!(result.graph.ecount(), 2); // edges 0-1 and 1-2
assert_eq!(result.invmap, vec![0, 1, 2]);