cancel
Showing results forΒ
Did you mean:Β

## How to ignore the directions in dijkstra's algorithm

Node Clone

The code I used to create the graph is as below.

``````CREATE (a:Location {name: 'A'}),
(b:Location {name: 'B'}),
(c:Location {name: 'C'}),
(d:Location {name: 'D'}),
(e:Location {name: 'E'}),
(f:Location {name: 'F'}),
``````

And the graph is as below.

Actually I want to create bidirectional relationships. since it is not possible, I have created like this.
To find the shortest path between "A" node and "F" node using dijkstra's algorithm, I used the following code.

``````CALL gds.graph.create(
'myGraph',
'Location',
{
relationshipProperties: 'cost'
}
)
``````

``````MATCH (source:Location {name: 'A'}), (target:Location {name: 'F'})
CALL gds.beta.shortestPath.dijkstra.stream('myGraph', {
sourceNode: id(source),
targetNode: id(target),
relationshipWeightProperty: 'cost'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN
index,
gds.util.asNode(sourceNode).name AS sourceNodeName,
gds.util.asNode(targetNode).name AS targetNodeName,
totalCost,
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
costs
ORDER BY index
``````

Output is as below.

This is fine. But when i tried to find the shortest path between "F" to "A" by using same code just by reversing the source and target nodes

``````MATCH (source:Location {name: 'F'}), (target:Location {name: 'A'})
CALL gds.beta.shortestPath.dijkstra.stream('myGraph', {
sourceNode: id(source),
targetNode: id(target),
relationshipWeightProperty: 'cost'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs
RETURN
index,
gds.util.asNode(sourceNode).name AS sourceNodeName,
gds.util.asNode(targetNode).name AS targetNodeName,
totalCost,
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
costs
ORDER BY index
``````

Output is as below

I think the reason is because there is no directed relation ships from "F" to "A".
I want to ignore the directions of relationships while calculating the dijkstra's shortest path.
Please tell me how can i ignore the directions of relationships in dijkstra's algorithm.
If this is not the reason, please tell me the code to find dijksra's shortest path between "F" to "A".

1 ACCEPTED SOLUTION
Neo4j

If you want to reverse the direction of your relationships you'll need to use the `orientation` configuration option when you load your data, not just swap source/target in the algo call:

``````CALL gds.graph.create(
'myGraph',
'Location',
{
relationshipProperties: 'cost',
orientation:'REVERSE'
}
)
``````

you can also set `orientation:'UNDIRECTED'` to load a graph without directed edges (inmemory, we duplicate the edges).

Neo4j

If you want to reverse the direction of your relationships you'll need to use the `orientation` configuration option when you load your data, not just swap source/target in the algo call:

``````CALL gds.graph.create(
'myGraph',
'Location',
you can also set `orientation:'UNDIRECTED'` to load a graph without directed edges (inmemory, we duplicate the edges).