Skip to main content

strong_product

Function strong_product 

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

Computes the strong product of two graphs.

The strong product is the union of the Cartesian product and the tensor product. 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, OR
  • (i, k) is an edge in g1 AND (j, l) is an edge in g2.

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, strong_product};

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 = strong_product(&g1, &g2).unwrap();
assert_eq!(p.vcount(), 4);
// Cartesian: 4 edges + Tensor: 2 edges = 6 edges (K4 minus one edge... actually it's 5 for the strong product of K2 x K2... let me check)
// Actually: K2 ⊠ K2 gives 5 edges (it's a complete graph minus one edge? No.)
// Cartesian of K2×K2 = C4 (4 edges), tensor of K2×K2 = 2 edges. Combined = 6? But some may overlap... no, they don't overlap for this case.
// Wait - let me recompute. C4 has edges: (0,0)-(0,1), (1,0)-(1,1), (0,0)-(1,0), (0,1)-(1,1) = 4 edges
// Tensor: (0,0)-(1,1), (0,1)-(1,0) = 2 edges. Total = 6.
// But K2 ⊠ K2 = K4 has 6 edges. Yes!
assert_eq!(p.ecount(), 6);