Skip to main content

compose

Function compose 

Source
pub fn compose(g1: &Graph, g2: &Graph) -> IgraphResult<Graph>
Expand description

Computes the composition of two graphs.

The composed graph has max(|V1|, |V2|) vertices. An edge (i, j) exists in the result if and only if there is some vertex k such that (i, k) is an edge in g1 and (k, j) is an edge in g2.

Both graphs must have the same directedness. The result may contain multi-edges; use crate::simplify to remove them if needed.

§Arguments

  • g1 — the first operand (left side of composition).
  • g2 — the second operand (right side of composition).

§Errors

Returns InvalidArgument if g1 and g2 differ in directedness.

§Examples

use rust_igraph::{Graph, compose};

// g1: 0→1→2, g2: 0→1→2 (directed paths)
let mut g1 = Graph::new(3, true).unwrap();
g1.add_edge(0, 1).unwrap();
g1.add_edge(1, 2).unwrap();

let mut g2 = Graph::new(3, true).unwrap();
g2.add_edge(0, 1).unwrap();
g2.add_edge(1, 2).unwrap();

let c = compose(&g1, &g2).unwrap();
assert_eq!(c.vcount(), 3);
// (0,1) in g1 + (1,2) in g2 → (0,2) in result
// (1,2) in g1 + ... vertex 2 has no outgoing in g2 → nothing
assert_eq!(c.ecount(), 1);
assert_eq!(c.edge(0).unwrap(), (0, 2));