Skip to main content

rooted_product

Function rooted_product 

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

Computes the rooted product of two graphs.

The result has |V1| * |V2| vertices. Vertex (i, j) is identified by i * |V2| + j. An edge exists between (i1, j1) and (i2, j2) iff:

  • i1 == i2 and (j1, j2) is an edge in g2, OR
  • j1 == j2 == root and (i1, i2) is an edge in g1.

Intuitively, this replaces each vertex of g1 with a copy of g2, connecting the copies through their root vertices according to the edges of g1.

The number of edges in the product is |V1| * |E2| + |E1|.

Both graphs must have the same directedness.

§Arguments

  • g1 — the first factor graph (whose vertices are “replaced”).
  • g2 — the second factor graph (the “replacement” graph).
  • root — a vertex in g2 used as the connection point.

§Errors

Returns InvalidArgument if the graphs differ in directedness, if the product vertex count overflows u32, or if root >= g2.vcount().

§Examples

use rust_igraph::{Graph, rooted_product};

// P3 with K2 rooted at vertex 0
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 = rooted_product(&g1, &g2, 0).unwrap();
assert_eq!(p.vcount(), 6); // 3 * 2
assert_eq!(p.ecount(), 5); // 3 * 1 + 2