cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How return list of node id from shortest path

hello,
I would like to return a list of nodeId in the path.

MATCH (m),(o),
p = shortestPath((m)-[*..15]-(o)) 
WHERE id(m) = 11 AND id(o) = 23
RETURN nodes(p)

How to return just id of nodes from this path?

output I expected
[11,4,9,27,0,20,23]

5 REPLIES 5

You could use a list comprehension, which is like using map to transform an array of somethings into a an array of something else.

For example, modifying your example to use the movie graph:

MATCH (m:Movie {title:"The Matrix"}),(o:Person {name:"Tom Hanks"}),
p = shortestPath((m)-[*..15]-(o)) 
RETURN [x in nodes(p) | id(x)]

Best,
ABK

Reference: Lists - Neo4j Cypher Manual

Thank you abk,
I need one more help,
How to return their weight (distance) value in a list

I'm unsure what you mean by weight or distance. Is that a property on the nodes?

In general, you can pluck whatever you need to get from the object in the second part of the list comprehension, following the | character.

Walking through the syntax of the list comprehension:

RETURN [x in nodes(p) | id(x)]
  1. x in means that x will receive a sequence of values
  2. nodes(p) provides the sequence, which here will be the nodes of the path p
  3. | is a separator. The left-hand side of will "bind" values that are passed to the right-hand side
  4. id(x) could be any expression. Here we know x is a node, so we're asking for that node's id

So, to get a property from the node instead, do this:

RETURN [x in nodes(p)) | x.name]

Does that help?

-ABK

weight(distance/cost) is a property Keys of the graph,
here I will explain a example:

CREATE (a:Location {name: 'A'}),
(b:Location {name: 'B'}),
(c:Location {name: 'C'}),
(h:Location {name: 'H'}),
 (j:Location {name: 'J'}),
(a)-[:ROAD {cost: 50}]->(b),
(a)-[:ROAD {cost: 50}]->(c),
(c)-[:ROAD {cost: 40}]->(j),
(j)-[:ROAD {cost: 30}]->(h),
(h)-[:ROAD {cost: 50}]->(b);

Query

MATCH p=(o{name:"A"})-[r*]->(x{name:"B"})

RETURN [x in nodes(p) | id(x)]

path

  1. [A,C,J,H,B]
  2. [A,B]

expected output

path                                  cost
1. [A,C,J,H,B]               [0,50,90,130,180]
2. [A,B]                        [0,50]

Thank you in advance

OK, so you'd like to get values from the relationships rather than the nodes, and accumulate them along the way? That's more of a reduce/fold on an array which produces a new array, rather than a map function.

So let's try reduce:

RETURN reduce(costs=[0], x in relationships(p) | costs + [costs[size(costs) - 1] + x.cost])
  1. initialize an accumulator with an array containing a single number 0
  2. bind the relationships in p to x
  3. concatenate the accumulator with a single element array containing a value calculated by combinging the last element of the accumulator with the current cost

Though if you just want the total cost of the path you could simplify to:

RETURN reduce(total=0, x in relationships(p) | total + x.cost)

Closer?

Best,
ABK

ps. The cypher manual has a lot of good information about lists. Check out: