rustworkx.graph_cartesian_product#

graph_cartesian_product(first, second, /)#

Return a new PyGraph by forming the cartesian product from two input PyGraph objects

Parameters:
  • first (PyGraph) – The first undirected graph object

  • second (PyGraph) – The second undirected graph object

Returns:

A new PyGraph object that is the cartesian product of first and second. It’s worth noting the weight/data payload objects are passed by reference from first and second to this new object. A read-only dictionary of the product of nodes is also returned. The keys are a tuple where the first element is a node of the first graph and the second element is a node of the second graph, and the values are the map of those elements to node indices in the product graph. For example:

{
    (0, 0): 0,
    (0, 1): 1,
}

Return type:

Tuple[PyGraph, ProductNodeMap]

import rustworkx.generators
from rustworkx.visualization import mpl_draw

graph_1 = rustworkx.generators.path_graph(2)
graph_2 = rustworkx.generators.path_graph(3)
graph_product, _ = rustworkx.graph_cartesian_product(graph_1, graph_2)
mpl_draw(graph_product)
../_images/rustworkx.graph_cartesian_product_0_0.png