Product of two graphs in MATLAB

12 次查看(过去 30 天)
I have two undirected graphs G1 and G2, they are both node and -edge weighted graphs. I want to create a graph G which is the Cartesian product of G1 and G2. G1 has 4 nodes and G2 has 15 nodes. How can I do this using MATLAB?

采纳的回答

Christine Tobler
Christine Tobler 2017-4-12
Without weights, you can compute the graph product quite quickly:
A1 = adjacency(G1);
A2 = adjacency(G2);
I1 = speye(numnodes(G1));
I2 = speye(numnodes(G2));
Aprod = kron(A1, I2) + kron(I1, A2);
Gprod = graph(Aprod);
With weights, you need to construct the weighted adjacency matrix, which unfortunately is a bit cumbersome:
>> [i, j] = findedge(G1);
>> A1 = sparse(i, j, G1.Edges.Weight, numnodes(G1), numnodes(G1));
>> [i, j] = findedge(G2);
>> A2 = sparse(i, j, G2.Edges.Weight, numnodes(G2), numnodes(G2));
>> I1 = speye(numnodes(G1));
>> I2 = speye(numnodes(G2));
>> Aprod = kron(A1, I2) + kron(I1, A2);
>> Gprod = graph(Aprod, 'upper');
The node weights should be simpler again, but I'm not sure how you would want to treat these: Is the weight of a node representing the product of node i from G1 and node j from G2 going to be the product of these node's weights, or the sum?
  1 个评论
puttogether
puttogether 2017-4-13
Thank you, this is what I was looking for. Yes the weight of a node in Gprod will be the product of weight at node i from G1 and node j from G2. I figured the nodes out. Thanks again

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by