Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-17-2021 11:02 PM
https://neo4j.com/docs/graph-data-science/current/algorithms/dijkstra-source-target/?_gl=11ubbr7m_ga...
In this example, all the relationships are unidirectional. How can i tell the algorithm to ignore the directions.
I tried to find the shortest path between 'F' to 'A'.
It is showing no changes, no records.
How can i ignore the directions of relationships?
thanks in advance.
Solved! Go to Solution.
03-21-2021 06:18 AM
Ah wait sorry, I've put it in the wrong place in my reply:
CALL gds.graph.create(
'myGraph',
'Location',
{ ROAD: {type: "ROAD", orientation: "UNDIRECTED"}},
{
relationshipProperties: 'cost'
}
)
03-18-2021 02:47 AM
Can you share the code that you've tried so far?
03-19-2021 03:00 AM
The above figure is the graph of the data.
The code used to create the graph is
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'}),
(a)-[:ROAD {cost: 50}]->(b),
(a)-[:ROAD {cost: 50}]->(c),
(a)-[:ROAD {cost: 100}]->(d),
(b)-[:ROAD {cost: 40}]->(d),
(c)-[:ROAD {cost: 40}]->(d),
(c)-[:ROAD {cost: 80}]->(e),
(d)-[:ROAD {cost: 30}]->(e),
(d)-[:ROAD {cost: 80}]->(f),
(e)-[:ROAD {cost: 40}]->(f);
To find shortest path between "A" and "F" nodes using dijkstra algorithm, the code is as follows.
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
I got the output like below.
This is fine. But when I find shortest path from "F" to "A" by using the same code just reversing the nodes as below,
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
the output is as below.
I think the reason for this is, there are no directed edges from "F" to "A".
Actually i want create bidirectional edges. But it is not possible in Neo4j.
what should be the code to ignore the directions and find dijkstra shortest path between "F" to "A".
03-19-2021 08:01 AM
Could you share the code you used to create myGraph
. You should be able to create a birectional graph when constructing that.
03-19-2021 08:12 PM
I have shared the code in above reply.
03-21-2021 12:19 AM
I think you haven't pasted the bit of code that calls the gds.graph.create
procedure.
03-21-2021 02:08 AM
Yes sir. Actually I used below code to call the graph. But i forget to mention in the above question.
CALL gds.graph.create(
'myGraph',
'Location',
'ROAD',
{
relationshipProperties: 'cost'
}
)
Please help me.
03-21-2021 04:37 AM
Can you try this:
CALL gds.graph.create(
'myGraph',
'Location',
'ROAD',
{
relationshipProperties: 'cost',
orientation: 'UNDIRECTED'
}
)
03-21-2021 06:07 AM
This is saying unexpected configuration key : orientation
03-21-2021 06:18 AM
Ah wait sorry, I've put it in the wrong place in my reply:
CALL gds.graph.create(
'myGraph',
'Location',
{ ROAD: {type: "ROAD", orientation: "UNDIRECTED"}},
{
relationshipProperties: 'cost'
}
)
03-21-2021 06:24 AM
Thank you sir. This works for me.
Thank you so much for helping.
All the sessions of the conference are now available online