Could I use neo4j gds shortest path algorithm with multiplication instead of addition?

392 Views Asked by At

I use neo4j database to calculate the shortest path between nodes.The whole graph include 400K nodes.I could use the shortestPath algorithm like below when I calculate the weight with addition operation, but how should I do if I wants to use multiplication operation to calculate the weight the nodes?

MATCH (sourceNode:entity{name: $name}) 
CALL gds.alpha.shortestPath.deltaStepping.stream({
     startNode: sourceNode, 
     nodeProjection: "*", 
     relationshipProjection: {
         all: {
         type: "*", 
         properties: "weight",
         orientation: "UNDIRECTED" 
         } 
     }, 
     relationshipWeightProperty: "weight", 
     delta: 1.0 
     })
YIELD nodeId, distance 
WHERE gds.util.isFinite(distance) 
RETURN sourceNode.name, 
       gds.util.asNode(nodeId).name AS aim_entity,
       distance 
ORDER BY distance;
1

There are 1 best solutions below

0
On

You could map your weight field to a logWeight field by taking a logarithm - then in logarithmic space addition is multiplication in regular space.

Functions you can use:

https://neo4j.com/docs/cypher-manual/current/functions/mathematical-logarithmic/

Then if the shortest path you calculate (using the logarithmic weights) to a node n is n.ShortestDistance you can get the product of all the weights (pre-logarithm) by doing exp(n.ShortestDistance).