How to merge non disjoint graphs through their adjacency matrices?

I'm a second year physics undergraduate student and a novice to programming and to Mathematica. I've started self studying graph theory some months ago and I'm getting acquainted with it and how to use Mathematica to work with graphs.

One of the problems I'm trying solve is how to code the operations for merging non disjoint graphs, using their adjacency matrices, for example, if I have these adjacency matrices:

I realized that one way to create the joint adjacency matrix was to first dispose de matrices on the diagonal of a 9x9 matrix, like (1) and then transferring the values of the repeated columns to the first columns where that vertex appeared and finally deleting the duplicated columns, thus generating a matrix like (2), which in turn allows me to use functions like Graph and AdjacencyGraph to create the intended graph.

These operations are relatively simple to perform on a spreadsheet software like Excel, but I found out that I couldn't crack how to do it on Mathematica, even though I know how to do the basic linear algebra and matrix algebra in the Wolfram Language.

I know that this is probably not the kind of answer this community is intended to have, but I don't know anywhere else to ask for help.

Perhaps this could be useful.

(*Adjacency Matrices*)
M1 = {{0, 1, 1}, {1, 0, 0}, {1, 1, 0}};
M2 = {{0, 1, 1}, {1, 0, 0}, {1, 1, 0}};
M3 = {{0, 0, 1}, {1, 0, 1}, {1, 1, 0}};


Then your labels of the "v's"

(*Labels*)
vlabel1 = {1, 2, 3};
vlabel2 = {4, 1, 5};
vlabel3 = {6, 4, 7};


Then using sparse array you can set particular positions in your matrix to be one according to your adjacency matrix, and use MatrixForm to visualise. (The final matrix however is different to yours). Hope this helps

(*Rules for Sparse Array*)

r1 = Map[# -> 1 &, Map[vlabel1[[#]] &, Position[M1, 1]]];
r2 = Map[# -> 1 &, Map[vlabel2[[#]] &, Position[M2, 1]]];
r3 = Map[# -> 1 &, Map[vlabel3[[#]] &, Position[M3, 1]]];
SparseArray[Join[r1, r2, r3]] // MatrixForm

• Thanks @Dunlop for your quick reply, I didn't knew the SparseArray function, this should probably solve the problems I'm having with this kind of operation, at least for a while. The results are different from the table I've posted, because I made a silly mistake while typing the LaTeX code. Commented Mar 30, 2017 at 20:16

You can also use the GraphUnion of the AdjacencyGraphs obtained using the three adjacency matrices and their vertex lists:

am1 = {{0, 1, 1}, {1, 0, 0}, {1, 1, 0}};
am2 = {{0, 1, 1}, {1, 0, 0}, {1, 1, 0}};
am3 = {{0, 0, 1}, {1, 0, 1}, {1, 1, 0}};
vl1 = {1, 2, 3};
vl2 = {4, 1, 5};
vl3 = {6, 4, 7};


Get three AdjacencyGraphs using the vertex lists and adjacency matrices defined above:

{g1, g2, g3} = (AdjacencyGraph @@@ {{vl1, am1}, {vl2, am2}, {vl3, am3}});

Row[Labeled[SetProperty[#, {VertexShapeFunction->"Name", ImageSize->200}],  #2, Top] & @@@
{{g1, "g1"}, {g2, "g2"}, {g3, "g3"}}]


Take the GraphUnion of g1, g2, g31:

gu = GraphUnion[g1, g2, g3];

SetProperty[gu, {VertexShapeFunction -> "Name", ImageSize -> 500}]


Get the AdjacencyMatrix of gu:

am = AdjacencyMatrix[gu]


SparseArray["<"15">",{7,7}]

am // MatrixForm


• Thanks kglr, it took me a little long to comment because I had the first round of tests of the semester and also because your answer gave me a lot to research on Mathematica's graph related functions. You and @Dunlop helped to get a lot of knowledge both about Mathematica and about Graph Theory, that without your help I would probably spend a lot of time to learn on my own. Commented Apr 15, 2017 at 0:26
• @nicholas80, my pleasure. Welcome to mma.se.
– kglr
Commented Apr 15, 2017 at 8:04