Traverse from first node to last node using cypher language
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
add a comment |
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
add a comment |
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
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
neo4j cypher nodes treenode
edited Nov 12 at 13:24
chriopp
60249
60249
asked Nov 12 at 8:26
Bhu
133
133
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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;
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
|
show 2 more comments
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
|
show 2 more comments
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;
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
|
show 2 more comments
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;
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;
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
|
show 2 more comments
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
|
show 2 more comments
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;
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
add a comment |
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;
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
add a comment |
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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