Product of two graphs in MATLAB

9 views (last 30 days)
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?

Accepted Answer

Christine Tobler
Christine Tobler on 12 Apr 2017
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 Comment
puttogether
puttogether on 13 Apr 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!