Skip to main content

tensor_product

Function tensor_product 

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

Computes the tensor (categorical/direct) product of two graphs.

The result has |V1| * |V2| vertices. Vertex (i, j) is identified by i * |V2| + j. An edge exists between (i, j) and (k, l) iff (i, k) is an edge in g1 AND (j, l) is an edge in g2.

For undirected graphs, each pair of edges generates two product edges (one for each orientation).

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

let mut g1 = Graph::with_vertices(3);
g1.add_edge(0, 1).unwrap();
g1.add_edge(1, 2).unwrap();

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

let p = tensor_product(&g1, &g2).unwrap();
assert_eq!(p.vcount(), 6);
// 2 edges × 1 edge × 2 (undirected) = 4 edges
assert_eq!(p.ecount(), 4);