Skip to main content

cartesian_product

Function cartesian_product 

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

Computes the Cartesian product of two graphs.

The result has |V1| * |V2| vertices. Vertex (i, j) in the product is identified by index i * |V2| + j. An edge exists between (i, j) and (k, l) iff:

  • i == k and (j, l) is an edge in g2, OR
  • j == l and (i, k) is an edge in g1.

Both graphs must have the same directedness.

§Arguments

  • g1 — the first factor graph.
  • g2 — the second factor graph.

§Errors

Returns InvalidArgument if the graphs differ in directedness, or if the product vertex count overflows u32.

§Examples

use rust_igraph::{Graph, cartesian_product};

// P2 □ P2 = C4 (path of 2 vertices □ path of 2 vertices = 4-cycle)
let mut g1 = Graph::with_vertices(2);
g1.add_edge(0, 1).unwrap();
let mut g2 = Graph::with_vertices(2);
g2.add_edge(0, 1).unwrap();

let p = cartesian_product(&g1, &g2).unwrap();
assert_eq!(p.vcount(), 4);
assert_eq!(p.ecount(), 4);