Skip to main content

intersection

Function intersection 

Source
pub fn intersection(left: &Graph, right: &Graph) -> IgraphResult<Graph>
Expand description

Returns the intersection of left and right.

Vertex sets are aligned by index — the result has max(left.vcount(), right.vcount()) vertices. For each endpoint pair (u, v), the multiplicity in the result equals the smaller of the multiplicities in the two inputs (so pairs unique to one side are dropped).

Both inputs must agree on directedness; an undirected edge (u, v) is canonicalised to (min(u, v), max(u, v)) before counting, while directed edges are tallied as-is.

Output edges are emitted in lexicographic (src, tgt) order; two edges sharing the same canonicalised pair appear consecutively.

§Errors

§Examples

use rust_igraph::{Graph, intersection};

// Triangle ∩ path: edges {(0,1), (1,2), (2,0)} ∩ {(0,1), (1,2)}
// → {(0,1), (1,2)} on max(3, 3) = 3 vertices.
let mut a = Graph::with_vertices(3);
a.add_edge(0, 1).unwrap();
a.add_edge(1, 2).unwrap();
a.add_edge(2, 0).unwrap();
let mut b = Graph::with_vertices(3);
b.add_edge(0, 1).unwrap();
b.add_edge(1, 2).unwrap();

let i = intersection(&a, &b).unwrap();
assert_eq!(i.vcount(), 3);
assert_eq!(i.ecount(), 2);