Adding nodes from a sub-graph to a larger graph

3 views (last 30 days)
I have an adjacency matrix for a graph consisting of 10 nodes and the edges between them. But they are part of a larger graph, and I want to construct the adjacency matrix for that larger graph.
I know what node IDs the nodes 1-10 should correspond to in the larger graph, but I don't know how to set them. Is there an easy way to do this? The function addnode looks helpful, but I think it would keep the original node IDs the same and just add to that, whereas the node IDs of the original set will change when they become part of the larger graph.
Another option might be to do this with a purely array-based approach rather than using graphs. As a simple example, say I have the following adjacency matrix
A =
0 0 1
0 0 1
1 1 0
which corresponds to three nodes and the edges between them. Say these three nodes are actually nodes 1, 5, and 6 of a larger graph with 6 nodes. How would I re-arrange that adjacency matrix in the simplest case where the other nodes form no edges (so would correspond to 0's in the adjacency matrix)?

Accepted Answer

Bruno Luong
Bruno Luong on 21 Apr 2022
Edited: Bruno Luong on 21 Apr 2022
Build Abig as adjacent matrix of empty graph with 6 nodes, then insert A to Abig
A = [0 0 1;
0 0 1;
1 1 0 ];
newpos = [1 5 6];
Abig = sparse(6,6);
Abig(newpos,newpos) = Abig(newpos,newpos) | A;
Gbig = graph(Abig);
plot(Gbig);

More Answers (0)

Categories

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

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!