cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 1: replicate users.

Use direction of relationship in Yen's algorithm

remon
Node

In Neo4j 3.5.* when using algo.kShortestPaths.stream() for calculating a number of shortest paths using weights between two nodes, there was the option to set the direction by using {direction:'OUT'}.

Currently I am using Neo4j 4.2.4 and calling gds.beta.shortestPath.yens.stream(), but the algorithm returns paths in all directions, so it's ignoring the directed relationships between nodes. I want to the algorithm to use only (outgoing) directions from the startnode to the endnode.

Is there a possibility to use the direction of the paths in Yen's K-shortest paths algorithm? Or should I switch to another algorithm with this functionality?

Old cypher query:
MATCH (start:GENE{name:'<startnode_name>'}), (end:GENE{name:'<endnode_name>'})
CALL algo.kShortestPaths.stream(start, end, 10, 'weight' ,{direction:'OUT'})
YIELD index, nodeIds, costs
RETURN [node in algo.getNodesById(nodeIds) | node.name] AS places,costs,reduce(acc = 0.0, cost in costs | acc + cost) AS totalCost

New cypher query:
MATCH (source:GENE {name: '<startnode_name>'}), (target:GENE {name: '<endnode_name>'})
CALL gds.beta.shortestPath.yens.stream('full_graph', { sourceNode: id(source), targetNode: id(target), k: 10, relationshipWeightProperty: 'weight'})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN index, gds.util.asNod

1 REPLY 1

Cobra
Ninja
Ninja

Hello @remon and welcome to the Neo4j community

You should have a look at this post and try for the direction parameter:

  • Outgoing
  • Incoming
  • Both

Regards,
Cobra