How to add new nodes in between two existing nodes












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










share|improve this question
























  • 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?
    – Banghua Zhao
    Nov 25 '18 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 '18 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.
    – Banghua Zhao
    Nov 25 '18 at 5:49












  • @BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
    – Natasha
    Nov 25 '18 at 6:09
















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










share|improve this question
























  • 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?
    – Banghua Zhao
    Nov 25 '18 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 '18 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.
    – Banghua Zhao
    Nov 25 '18 at 5:49












  • @BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
    – Natasha
    Nov 25 '18 at 6:09














1












1








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










share|improve this question















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







matlab graph nodes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:13

























asked Nov 23 '18 at 7:15









Natasha

969




969












  • 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?
    – Banghua Zhao
    Nov 25 '18 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 '18 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.
    – Banghua Zhao
    Nov 25 '18 at 5:49












  • @BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
    – Natasha
    Nov 25 '18 at 6:09


















  • 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?
    – Banghua Zhao
    Nov 25 '18 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 '18 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.
    – Banghua Zhao
    Nov 25 '18 at 5:49












  • @BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
    – Natasha
    Nov 25 '18 at 6:09
















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?
– Banghua Zhao
Nov 25 '18 at 5:18




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?
– Banghua Zhao
Nov 25 '18 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 '18 at 5:27






@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 '18 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.
– Banghua Zhao
Nov 25 '18 at 5:49






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.
– Banghua Zhao
Nov 25 '18 at 5:49














@BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
– Natasha
Nov 25 '18 at 6:09




@BanghuaZhao I would like to add 4 new nodes between 6 and 12 too.
– Natasha
Nov 25 '18 at 6:09












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442156%2fhow-to-add-new-nodes-in-between-two-existing-nodes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer


























      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.






      share|improve this answer
























        1












        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 12:24









        Natasha

        969




        969






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442156%2fhow-to-add-new-nodes-in-between-two-existing-nodes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Trompette piccolo

            Slow SSRS Report in dynamic grouping and multiple parameters

            Simon Yates (cyclisme)