Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Dijkstra's algorithm in Neo4j using Cypher

My question is: is it possible to implement Dijkstra's algorithm using Cypher? the explanation on the neo4j website only talks about REST API and it is very difficult to understand for a beginner like me

Please note that I want to find the shortest path with the shortest distance between two nodes, and not the shortest path (involving least number of relationships) between two nodes. I am aware of the shortestPath algorithm that is very easy to implement using Cypher, but it does not serve my purpose.

Kindly guide me on how to proceed if I have a graph database with nodes, and relationships between the nodes having the property 'distance'. All I want is to write a code with the help of which we will be able to find out the shortest distance between two nodes in the database. Or any tips if I need to change my approach and use some other program for this?

like image 797
Shazu Avatar asked Dec 08 '14 15:12

Shazu


People also ask

What is Dijkstra shortest path algorithm?

The Dijkstra Shortest Path algorithm computes the shortest path between nodes. The algorithm supports weighted graphs with positive relationship weights. The Dijkstra Single-Source algorithm computes the shortest paths between a source node and all nodes reachable from that node.

How to query Neo4j from Python?

How to query Neo4j from Python 1 Using Neo4j in Python: a quick and dirty introduction to Neo4j Python Driver and Cypher Query Language. Neo4j is a graph database management system. ... 2 Install Neo4j Desktop and configure the database. ... 3 Install Neo4j Python Driver. ... 4 Make Queries in Python. ... 5 Run Graph Data Science Algorithms. ...

What is the difference between Dijkstra single-source and GDS?

The Dijkstra Single-Source algorithm computes the shortest paths between a source node and all nodes reachable from that node. To compute the shortest path between a source and a target node, Dijkstra Source-Target can be used. The GDS implementation is based on the original description and uses a binary heap as priority queue.

Is Dijkstra’s algorithm similar to BFS?

But as Dijkstra’s algorithm uses a priority queue for its implementation, it can be viewed as close to BFS. Q #5) Where is the Dijkstra algorithm used?


2 Answers

In this case you can implement the allShortestPaths, ordering the paths in an ascending order based on your distance property of the relationships and return only one, based on your last post it would be something like this :

MATCH (from: Location {LocationName:"x"}), (to: Location {LocationName:"y"}) , 
paths = allShortestPaths((from)-[:CONNECTED_TO*]->(to))
WITH REDUCE(dist = 0, rel in rels(paths) | dist + rel.distance) AS distance, paths
RETURN paths, distance
ORDER BY distance
LIMIT 1
like image 68
Christophe Willemsen Avatar answered Oct 10 '22 03:10

Christophe Willemsen


No, it's not possible in a reasonable way unless you use transactions and basically rewrite the algorhythm. The previous answer is wrong as longer but less expensive paths will not be returned by the allShortestPaths subset. You will be filtering a subset of paths that have been chosen without considering relationship cost.

like image 32
Pempo Avatar answered Oct 10 '22 02:10

Pempo