Hello,
I am trying to add new nodes between two nodes that already exist in a graph.
For example, I have a graph with two nodes labelled '1' and '2'
If I have to add 4 nodes between nodes labelled '1' and '2' , the edge between nodes 1 and 2 is deleted, new nodes are added to the graph,
new edges are added.
The following code performs the above-mentioned steps.
NNode = 2;tail = 1:NNode-1;head = 2:NNode;Graph = graph(tail,head);plot(Graph);Graph.Nodes.Name = cellstr(string(1:height(Graph.Nodes))');% Add new nodes
nnew = 4 % number of new nodes
Graph = rmedge(Graph,1,2)Graph = addnode(Graph, nnew)Graph.Nodes.Name to_add = vertcat('1',Graph.Nodes.Name(end-nnew+1:end),'2')for node = 1:length(to_add)-1 head = to_add(node) tail = to_add(node+1) Graph = addedge(Graph,head,tail);endplot(Graph)Graph.NodesGraph.EdgesI_inv = full(incidence(Graph))'
The following is the output of Graph.Edges
EndNodes __________________ '1' 'Node3' '2' 'Node6' 'Node3' 'Node4' 'Node4' 'Node5' 'Node5' 'Node6'
There is a problem that occurs while adding new edges. I am adding the last edge as addedge(Graph, 'Node6', '2').
However, in the above table we can see the head node is '2' and tail node is 'Node6'. Whereas, the edge was added the other way round.
Because of this the way in which incidence matrix is computed differs.
For example, the incidencematrix of the following graph is different from the above,
NNode = 6;tail = 1:NNode-1;head = 2:NNode;Graph = graph(tail,head);
Although both graphs are the same and have 6 nodes.
Any suggestions on how to aviod this problem ? I would like to retain the result of Graph.Edges to be in the order in which the head and tail nodes are added to the Graph.
Best Answer