Skip to main content

subgraph_from_edges

Function subgraph_from_edges 

Source
pub fn subgraph_from_edges(
    graph: &Graph,
    eids: &[u32],
    delete_vertices: bool,
) -> IgraphResult<SubgraphFromEdgesResult>
Expand description

Create a subgraph containing only the specified edges.

Given a set of edge IDs, builds a new graph that keeps exactly those edges. When delete_vertices is true, vertices that have no incident edges in the selection are removed and IDs are renumbered. When false, all original vertices are kept.

Duplicate edge IDs are silently ignored. Edge ordering in the result follows the ascending order of original edge IDs.

§Arguments

  • graph — the input graph.
  • eids — slice of edge IDs to retain.
  • delete_vertices — if true, remove isolated vertices.

§Errors

Returns InvalidArgument if any edge ID in eids is out of range.

§Examples

use rust_igraph::{Graph, subgraph_from_edges};

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

// Keep only edges 0 and 2: (0,1) and (2,3)
let result = subgraph_from_edges(&g, &[0, 2], true).unwrap();
assert_eq!(result.graph.ecount(), 2);
assert_eq!(result.graph.vcount(), 4); // vertices 0,1,2,3 (4 removed)