1

I have the following Graph created in MATLAB

function dummyGraph()
tail = [1 2 3 4 5 6 6 7 8 9 10 10 12 13 14 15];
head = [2 3 4 5 6 7 12 8 9 10 15 11 13 14 15 16];
Graph = graph(tail,head)
plot(Graph)
NodeNumber = 1:16
Neighbor={};
for NodeIdx =NodeNumber
    Neighbor{NodeIdx} = neighbors(Graph,NodeIdx);
end
end

I want to add nodes in between two consecutive nodes.

I'm trying the following approach

1.Obtain the neighbor nodes of all the nodes present in Graph. 2. Add four new nodes between two consecutive neighbors.

I could implement the first step in the code. I couldn't find MATLAB commands for adding nodes between existing nodes. A command addnode(G,nodeIDs) is given in MATLAB documentation. But, I am not sure how to make use of this command to implement for my case.

I would like to ask for suggestions on how to proceed

4
  • I want to clarify the meaning of adding four new nodes in between two consecutive neighbors. Does it mean that, for example, adding node 17, 18, 19, and 20 in between node 1 and 2. Adding node 21, 22, 23, and 24 in between node 2 an 3 and so on? Nov 25, 2018 at 5:18
  • @BanghuaZhao You are absolutely right. I want to proceed in the same way.I am not really particular about doing this task in MATLAB. Is there any other platform(python?) in which this can be achieved?
    – Natasha
    Nov 25, 2018 at 5:27
  • I see. What about node number that is not consecutive but connected? For example, node 6 and 12, they are connected but the number is not consecutive. Nov 25, 2018 at 5:49
  • @BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
    – Natasha
    Nov 25, 2018 at 6:09

1 Answer 1

1

Here is the solution ,

function AddNodes()
tail = [1 2];
head = [2 3];
Graph = graph(tail,head)
NVertex = size(Graph.Nodes);
NVertex = NVertex(1);

%% Name Nodes

NNode = string(1:NVertex)';
Graph.Nodes.Name = cellstr(NNode);


%% Adding nodes
GraphEdges = table2cell(Graph.Edges(:,1));
NEdges = size(GraphEdges);
NEdges = NEdges(1);
Source=[];
Sink =[];
for edge = 1:NEdges
    Graph  = addnode(Graph,4);
    source = GraphEdges{edge}(1);
    sink = GraphEdges{edge}(2);
    Graph  = rmedge(Graph,source,sink); 
    Add4Musk = Graph.Nodes.Name(end-3:end);
    TobeAdded  = vertcat(source,Add4Musk,sink);
    for node = 1:length(TobeAdded)-1
        source = TobeAdded(node);
        sink = TobeAdded(node+1);
        Graph = addedge(Graph,source,sink);
    end 
end

%% Plot graph
plot(Graph,'Layout','subspace3')

I would be happy to know if there are alternate ways of doing this task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.