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.

Return a relationship Label in shortest Path

I and trying to parse the result of a shortest path

i have projected a graph - set all relationships to "UNDIRECTED"

run the following algorithm

CALL gds.shortestPath.dijkstra.stream(

the result is a list of node ids. I am new to cypher - and cannot figure out  - how to translate from 

list of nodes: 1,56,2

to 1 (name of node) - relationship label - 56 (name of node)

56 (name of node) - relationship label - 2 (name of node)

 

the current output gives me the path - and i can hobble node ids out, but the relationships are all cost:1.  how do i get the name of edge traversed?

 

Thank you

5 REPLIES 5

The method produces a number of outputs. These are shown below for the example I captured from the documentation. 

CALL gds.shortestPath.dijkstra.stream(
  graphName: String,
  configuration: Map
)
YIELD
  index: Integer,
  sourceNode: Integer,
  targetNode: Integer,
  totalCost: Float,
  nodeIds: List of Integer,
  costs: List of Float,
  path: Path

 You can get the relationship information from the path, using 'relationships(path)'.  This returns a list of relationships. Each relationships contains its: id, startNode, endNode, properties, and type. You can extract them as follows: id(path), startNode(path), endNode(path), properties(path), and type(path), respectively.

I took the example from the documentation and altered it to return the list of relationships along the resulting path. I returned the relationship information individually. You can alter it to return what you need.

MATCH (source:Location {name: 'A'}), (target:Location {name: 'F'})
CALL gds.shortestPath.dijkstra.stream('myGraph', {
    sourceNode: source,
    targetNode: target,
    relationshipWeightProperty: 'cost'
})
YIELD path
UNWIND relationships(path) as rel
RETURN id(rel), startNode(rel), endNode(rel), properties(rel), type(rel)

Thank you for the quick reply!

I am certainly on the right path based on your feedback..  but I think i am still missing something

When i do "type(rel)"  the output for all items in the path is "PATH 0" or (1,2,3 depending on the algorithm)

in my example:

list of nodes: 1,56,2

 1 (name of node) - "Path 0" - 56 (name of node)
56 (name of node) - "Path 0" - 2 (name of node)

what i would hope to see is the relationship label or type.. something like:

 1 (name of node) - "Shipped by Train" - 56 (name of node)
56 (name of node) - "Shipped by Truck" - 2 (name of node)

Thank you!

type(rel) should give you that.  Can you post your query and some sample data!

I mocked up a scenario with the same outcome:

merge (C:Person {name:'Mary'})
merge (D:Person {name:'Bob'})
merge (E:Person {name:'Jenny'})
merge (F:Ride {type:'Coaster'})
merge (G:Ride {type:'Spinny Thing'})
merge (H:Ride {type:'Ring Toss'})
merge (L:Located {city:'LA'})
merge (M:Located {city:'Chicago'})
Merge (C) - [:INVENTED] -> (G)
Merge (D) - [:RODE] -> (G)
Merge (D) - [:RODE] -> (H)
Merge (E) - [:BANNED] -> (F)
merge (G) - [:ONLY_WORKS] -> (L)
merge (H) - [:ONLY_WORKS] -> (F)
merge (F) - [:ONLY_WORKS] -> (L)
merge (M) - [:HIGHEST_RATED] -> (H);


CALL gds.graph.project(
'mygraph',
'*',
{INVENTED: {orientation:'UNDIRECTED'}
, BANNED: {orientation:'UNDIRECTED'}
, HIGHEST_RATED: {orientation:'UNDIRECTED'}
, ONLY_WORKS: {orientation:'UNDIRECTED'}
, RODE: {orientation:'UNDIRECTED'}

}
);


match(n:Person {name:"Mary"}) , (p:Ride {type:"Ring Toss"})
with ID(n) as st, ID(p) as en

CALL gds.shortestPath.dijkstra.stream(
'mygraph',{
sourceNode: st,
targetNode: en}
)
YIELD index, sourceNode, targetNode, totalCost,
nodeIds, costs, path

unwind relationships(path) as rel

return index ,type(rel), path

-------------

if i view the graph - i see the names of the relationships i want, but the only thing i can output is type(rel) which is the pathname....

Any help appreciated.  thank you!

It turns out the path relationships returned are not the actual relationships; they are virtual relationships with a type equal to the path, i.e. PATH_0.  You can see this if you look at the path relationships, as they have negative identifiers. 

Anyway, I added stuff to the query to take the start and end nodes of each path relationship and get the true relationship between the nodes, so its type can be returned.

Try this:

 

 

match(n:Person {name:"Mary"}) , (p:Ride {type:"Ring Toss"}) 
with ID(n) as st, ID(p) as en
CALL gds.shortestPath.dijkstra.stream(
'mygraph',{
sourceNode: st,
targetNode: en}
)
yield path
with relationships(path) as p
unwind range(0,size(p)-1) as segment
with segment, startNode(p[segment]) as startNode, endNode(p[segment]) as endNode
with segment, startNode, endNode, id(startNode) as start_id, id(endNode) as end_id
match(n where id(n) = start_id)
match(m where id(m) = end_id)
match(n)-[r]-(m)
return segment as segment, start_id as startNodeId, type(r) as type, end_id as endNodeId