Traverse from first node to last node using cypher language










2














I have created nodes and and relationship. I need to traverse from first node to last node. The output i expect to be displayed as
India --> Tamil Nadu --> Chennai --> Kanchipuram --> Vandalur.



Any idea how to display this path?



Below query used to create the nodes and relationships



CREATE (in:place name:"India", continent:"Asia", Language:"English"),
(tn:place name:"Tamil Nadu", continent:"Asia", Language:"Tamil"),
(ap:place name:"Andra Pradesh", continent:"Asia", Language:"Telugu"),
(ch:place name:"Chennai", continent:"Asia", Language:"Tamil"),
(co:place name:"Coimbatore", continent:"Asia", Language:"Tamil"),
(ka:place name:"Kanchipuram", continent:"Asia", Language:"Tamil"),
(th:place name:"Thiruvallur", continent:"Asia", Language:"Tamil"),
(va:place name:"Vandalur", continent:"Asia", Language:"Tamil"),
(pa:place name:"Padapai", continent:"Asia", Language:"Tamil"),
(in)- [:parent] ->(tn),
(in)- [:parent] ->(ap),
(tn)- [:parent] ->(ch),
(tn)- [:parent] ->(co),
(ch)- [:parent] ->(ka),
(ch)- [:parent] ->(th),
(ka)- [:parent] ->(va),
(ka)- [:parent] ->(pa)









share|improve this question




























    2














    I have created nodes and and relationship. I need to traverse from first node to last node. The output i expect to be displayed as
    India --> Tamil Nadu --> Chennai --> Kanchipuram --> Vandalur.



    Any idea how to display this path?



    Below query used to create the nodes and relationships



    CREATE (in:place name:"India", continent:"Asia", Language:"English"),
    (tn:place name:"Tamil Nadu", continent:"Asia", Language:"Tamil"),
    (ap:place name:"Andra Pradesh", continent:"Asia", Language:"Telugu"),
    (ch:place name:"Chennai", continent:"Asia", Language:"Tamil"),
    (co:place name:"Coimbatore", continent:"Asia", Language:"Tamil"),
    (ka:place name:"Kanchipuram", continent:"Asia", Language:"Tamil"),
    (th:place name:"Thiruvallur", continent:"Asia", Language:"Tamil"),
    (va:place name:"Vandalur", continent:"Asia", Language:"Tamil"),
    (pa:place name:"Padapai", continent:"Asia", Language:"Tamil"),
    (in)- [:parent] ->(tn),
    (in)- [:parent] ->(ap),
    (tn)- [:parent] ->(ch),
    (tn)- [:parent] ->(co),
    (ch)- [:parent] ->(ka),
    (ch)- [:parent] ->(th),
    (ka)- [:parent] ->(va),
    (ka)- [:parent] ->(pa)









    share|improve this question


























      2












      2








      2







      I have created nodes and and relationship. I need to traverse from first node to last node. The output i expect to be displayed as
      India --> Tamil Nadu --> Chennai --> Kanchipuram --> Vandalur.



      Any idea how to display this path?



      Below query used to create the nodes and relationships



      CREATE (in:place name:"India", continent:"Asia", Language:"English"),
      (tn:place name:"Tamil Nadu", continent:"Asia", Language:"Tamil"),
      (ap:place name:"Andra Pradesh", continent:"Asia", Language:"Telugu"),
      (ch:place name:"Chennai", continent:"Asia", Language:"Tamil"),
      (co:place name:"Coimbatore", continent:"Asia", Language:"Tamil"),
      (ka:place name:"Kanchipuram", continent:"Asia", Language:"Tamil"),
      (th:place name:"Thiruvallur", continent:"Asia", Language:"Tamil"),
      (va:place name:"Vandalur", continent:"Asia", Language:"Tamil"),
      (pa:place name:"Padapai", continent:"Asia", Language:"Tamil"),
      (in)- [:parent] ->(tn),
      (in)- [:parent] ->(ap),
      (tn)- [:parent] ->(ch),
      (tn)- [:parent] ->(co),
      (ch)- [:parent] ->(ka),
      (ch)- [:parent] ->(th),
      (ka)- [:parent] ->(va),
      (ka)- [:parent] ->(pa)









      share|improve this question















      I have created nodes and and relationship. I need to traverse from first node to last node. The output i expect to be displayed as
      India --> Tamil Nadu --> Chennai --> Kanchipuram --> Vandalur.



      Any idea how to display this path?



      Below query used to create the nodes and relationships



      CREATE (in:place name:"India", continent:"Asia", Language:"English"),
      (tn:place name:"Tamil Nadu", continent:"Asia", Language:"Tamil"),
      (ap:place name:"Andra Pradesh", continent:"Asia", Language:"Telugu"),
      (ch:place name:"Chennai", continent:"Asia", Language:"Tamil"),
      (co:place name:"Coimbatore", continent:"Asia", Language:"Tamil"),
      (ka:place name:"Kanchipuram", continent:"Asia", Language:"Tamil"),
      (th:place name:"Thiruvallur", continent:"Asia", Language:"Tamil"),
      (va:place name:"Vandalur", continent:"Asia", Language:"Tamil"),
      (pa:place name:"Padapai", continent:"Asia", Language:"Tamil"),
      (in)- [:parent] ->(tn),
      (in)- [:parent] ->(ap),
      (tn)- [:parent] ->(ch),
      (tn)- [:parent] ->(co),
      (ch)- [:parent] ->(ka),
      (ch)- [:parent] ->(th),
      (ka)- [:parent] ->(va),
      (ka)- [:parent] ->(pa)






      neo4j cypher nodes treenode






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 13:24









      chriopp

      60249




      60249










      asked Nov 12 at 8:26









      Bhu

      133




      133






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Neo4j offers the shortest path algorithm, other graph databases may differ (see documentation).



          MATCH (start:place name: "India" ), (end:place name: "Vandalur" ), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;


          If you have a clean tree structure you could also start on the target node and find all incoming parent relationships, which would eventually reach the root node, like this:



          MATCH (start:place name: "Vandalur" )<-[:parent*]-(root:place) 
          RETURN start, root;





          share|improve this answer






















          • Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
            – Bhu
            Nov 12 at 11:18











          • A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
            – chriopp
            Nov 12 at 11:29










          • Updated my answer to handle your use case.
            – chriopp
            Nov 12 at 12:33










          • There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
            – Bhu
            Nov 13 at 5:35










          • do you know any query to handle this scenario
            – Bhu
            Nov 13 at 5:37


















          0














          MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
          RETURN a,b,c,d,e;





          share|improve this answer




















          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 12 at 10:46










          • Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
            – Bhu
            Nov 12 at 11:07










          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%2f53258270%2ftraverse-from-first-node-to-last-node-using-cypher-language%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Neo4j offers the shortest path algorithm, other graph databases may differ (see documentation).



          MATCH (start:place name: "India" ), (end:place name: "Vandalur" ), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;


          If you have a clean tree structure you could also start on the target node and find all incoming parent relationships, which would eventually reach the root node, like this:



          MATCH (start:place name: "Vandalur" )<-[:parent*]-(root:place) 
          RETURN start, root;





          share|improve this answer






















          • Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
            – Bhu
            Nov 12 at 11:18











          • A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
            – chriopp
            Nov 12 at 11:29










          • Updated my answer to handle your use case.
            – chriopp
            Nov 12 at 12:33










          • There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
            – Bhu
            Nov 13 at 5:35










          • do you know any query to handle this scenario
            – Bhu
            Nov 13 at 5:37















          0














          Neo4j offers the shortest path algorithm, other graph databases may differ (see documentation).



          MATCH (start:place name: "India" ), (end:place name: "Vandalur" ), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;


          If you have a clean tree structure you could also start on the target node and find all incoming parent relationships, which would eventually reach the root node, like this:



          MATCH (start:place name: "Vandalur" )<-[:parent*]-(root:place) 
          RETURN start, root;





          share|improve this answer






















          • Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
            – Bhu
            Nov 12 at 11:18











          • A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
            – chriopp
            Nov 12 at 11:29










          • Updated my answer to handle your use case.
            – chriopp
            Nov 12 at 12:33










          • There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
            – Bhu
            Nov 13 at 5:35










          • do you know any query to handle this scenario
            – Bhu
            Nov 13 at 5:37













          0












          0








          0






          Neo4j offers the shortest path algorithm, other graph databases may differ (see documentation).



          MATCH (start:place name: "India" ), (end:place name: "Vandalur" ), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;


          If you have a clean tree structure you could also start on the target node and find all incoming parent relationships, which would eventually reach the root node, like this:



          MATCH (start:place name: "Vandalur" )<-[:parent*]-(root:place) 
          RETURN start, root;





          share|improve this answer














          Neo4j offers the shortest path algorithm, other graph databases may differ (see documentation).



          MATCH (start:place name: "India" ), (end:place name: "Vandalur" ), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;


          If you have a clean tree structure you could also start on the target node and find all incoming parent relationships, which would eventually reach the root node, like this:



          MATCH (start:place name: "Vandalur" )<-[:parent*]-(root:place) 
          RETURN start, root;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 7:08

























          answered Nov 12 at 8:56









          chriopp

          60249




          60249











          • Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
            – Bhu
            Nov 12 at 11:18











          • A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
            – chriopp
            Nov 12 at 11:29










          • Updated my answer to handle your use case.
            – chriopp
            Nov 12 at 12:33










          • There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
            – Bhu
            Nov 13 at 5:35










          • do you know any query to handle this scenario
            – Bhu
            Nov 13 at 5:37
















          • Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
            – Bhu
            Nov 12 at 11:18











          • A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
            – chriopp
            Nov 12 at 11:29










          • Updated my answer to handle your use case.
            – chriopp
            Nov 12 at 12:33










          • There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
            – Bhu
            Nov 13 at 5:35










          • do you know any query to handle this scenario
            – Bhu
            Nov 13 at 5:37















          Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
          – Bhu
          Nov 12 at 11:18





          Thanks alot.. it works , but with little constrain is that my input should be only the last node. my output should able to travel to the source node
          – Bhu
          Nov 12 at 11:18













          A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
          – chriopp
          Nov 12 at 11:29




          A graph per se does not have a root node, you are talking about a tree. You could give the root node ('India') a label to always reference it as start point of any shortest path query.
          – chriopp
          Nov 12 at 11:29












          Updated my answer to handle your use case.
          – chriopp
          Nov 12 at 12:33




          Updated my answer to handle your use case.
          – chriopp
          Nov 12 at 12:33












          There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
          – Bhu
          Nov 13 at 5:35




          There should a possible way to retrieve the path to the root node, isnt it? in this scenario my input should be vandalur, and it should give me the path till india ie my start or source node.
          – Bhu
          Nov 13 at 5:35












          do you know any query to handle this scenario
          – Bhu
          Nov 13 at 5:37




          do you know any query to handle this scenario
          – Bhu
          Nov 13 at 5:37













          0














          MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
          RETURN a,b,c,d,e;





          share|improve this answer




















          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 12 at 10:46










          • Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
            – Bhu
            Nov 12 at 11:07















          0














          MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
          RETURN a,b,c,d,e;





          share|improve this answer




















          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 12 at 10:46










          • Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
            – Bhu
            Nov 12 at 11:07













          0












          0








          0






          MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
          RETURN a,b,c,d,e;





          share|improve this answer












          MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
          RETURN a,b,c,d,e;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 9:08









          raf

          405




          405











          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 12 at 10:46










          • Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
            – Bhu
            Nov 12 at 11:07
















          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 12 at 10:46










          • Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
            – Bhu
            Nov 12 at 11:07















          While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
          – Nick
          Nov 12 at 10:46




          While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
          – Nick
          Nov 12 at 10:46












          Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
          – Bhu
          Nov 12 at 11:07




          Thank you.. but is there any method to find the parent or first node by inputting only the last node. Irrespective of any type of relation, can i get the path of first and last node?
          – Bhu
          Nov 12 at 11:07

















          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%2f53258270%2ftraverse-from-first-node-to-last-node-using-cypher-language%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin