To implement a tensor product for a list of graphs, you can modify the existing tensor_product function in the tensor_product.rs file to accept a list of graphs instead of just two graphs.
Here's a possible implementation in Rust:
[Redacted
fn tensor_product_list<Ty: EdgeType>(
py: Python,
graphs: Vec<&StablePyGraph<Ty>>,
undirected: bool,
) -> (StablePyGraph<Ty>, ProductNodeMap) {
if graphs.is_empty() {
// Handle the case where the list of graphs is empty
// For example, return an empty graph
return (StablePyGraph::<Ty>::new(), ProductNodeMap { node_map: HashMap::new() });
}
let mut final_graph = graphs[0].clone();
let mut hash_nodes = HashMap::new();
for (i, node) in final_graph.node_indices().enumerate() {
hash_nodes.insert((i,), node);
}
for graph in graphs.iter().skip(1) {
let mut new_hash_nodes = HashMap::new();
let mut new_graph = StablePyGraph::<Ty>::with_capacity(
final_graph.node_count() * graph.node_count(),
final_graph.edge_count() * graph.edge_count(),
);
for (i, node1) in final_graph.node_indices().enumerate() {
for (j, node2) in graph.node_indices().enumerate() {
let weight_x = &final_graph[node1];
let weight_y = &graph[node2];
let n0 = new_graph.add_node((weight_x, weight_y).into_py(py));
new_hash_nodes.insert((i, j), n0);
}
}
for edge_first in final_graph.edge_references() {
for edge_second in graph.edge_references() {
let source = new_hash_nodes
.get(&(edge_first.source().index(), edge_second.source().index()))
.unwrap();
let target = new_hash_nodes
.get(&(edge_first.target().index(), edge_second.target().index()))
.unwrap();
new_graph.add_edge(
*source,
*target,
(
edge_first.weight().clone_ref(py),
edge_second.weight().clone_ref(py),
)
.into_py(py),
);
}
}
if undirected {
for edge_first in final_graph.edge_references() {
for edge_second in graph.edge_references() {
if edge_first.source() == edge_first.target()
|| edge_second.source() == edge_second.target()
{
continue;
}
let source = new_hash_nodes
.get(&(edge_first.source().index(), edge_second.target().index()))
.unwrap();
let target = new_hash_nodes
.get(&(edge_first.target().index(), edge_second.source().index()))
.unwrap();
new_graph.add_edge(
*source,
*target,
(
edge_first.weight().clone_ref(py),
edge_second.weight().clone_ref(py),
)
.into_py(py),
);
}
}
}
final_graph = new_graph;
hash_nodes = new_hash_nodes;
}
let out_node_map = ProductNodeMap {
node_map: hash_nodes
.into_iter()
.map(|([Redacted Person], n)| (key, n.index()))
.collect(),
};
(final_graph, out_node_map)
}
This function takes a list of graphs and computes the tensor product of all the graphs in the list. It does this by iteratively computing the tensor product of the current graph with the next graph in the list.
You can then call this function from Python using the pyfunction macro:
This function takes a list of PyGraph objects and a boolean indicating whether the tensor product should be undirected or not. It returns a new PyGraph object that is the tensor product of all the input graphs, along with a ProductNodeMap object that maps the nodes of the input graphs to the nodes of the output graph.