import rustworkx as rx
def tensor_product_of_graphs(graphs):
"""
Compute the tensor product of a list of graphs.
Args:
graphs: A list of rustworkx PyGraph objects.
Returns:
A tuple containing:
- The tensor product graph (a rustworkx PyGraph).
- A dictionary mapping tuples of original node indices to the new indices.
The keys are tuples where each element represents a node index from the
corresponding graph in the input list. The values are the node indices
in the product graph. For example:
{
(0, 0, 0): 0,
(0, 0, 1): 1,
...
}
Raises:
TypeError: If input graphs are not rustworkx PyGraph objects.
ValueError: If the input list of graphs is empty.
"""
if not graphs:
raise ValueError("Input graph list cannot be empty")
for graph in graphs:
if not isinstance(graph, rx.PyGraph):
raise TypeError("Input graphs must be rustworkx PyGraph objects.")
product_graph = graphs[0].copy()
node_map = {(i,): i for i in range(graphs[0].num_nodes())}
for graph in graphs[1:]:
product_graph, new_node_map = rx.graph_tensor_product(product_graph, graph)
updated_node_map = {}
for (old_indices, old_product_index) in node_map.items():
for [Redacted Person] in range(graph.num_nodes()):
updated_node_map[old_indices + (j,)] = new_node_map[(old_product_index, j)]
node_map = updated_node_map
return product_graph, node_map
# Example usage:
graph1 = rx.generators.path_graph(2)
graph2 = rx.generators.path_graph(3)
graph3 = rx.generators.path_graph(2)
product_graph, node_map = tensor_product_of_graphs([graph1, graph2, graph3])
print(f"Product Graph Nodes: {product_graph.nodes()}")
print(f"Product Graph Edges: {product_graph.edge_list()}")
print(f"Node Map: {node_map}")