I'm looking for a robust way to add vertices to a graph by modifying its AdjacencyMatrix
.
Here's what I have so far:
addNode[matrix_,in_,out_]:=Module[{mod},
mod=ArrayPad[matrix,1];
mod[[1,2]]=1;
mod[[2,1]]=1;
mod[[Length@mod,-out]]=1;
mod[[-out,Length@mod]]=1;
mod
]
This will add a vertex connected to vertex 1
and another vertex connected to vertex out
. Notice that I only add 1x1 0's to my matrix. This is because at the moment I only need to add 2 vertices to my graphs.
Examples:
I've tried implementing a similar function that accepts the in
argument, but I couldn't get it to work properly. It seems as if I have to program special cases for it to work.
Is there a better way to do this?