3

I have simple weighted graph. I know how to find shortest route (in terms of hops) and I know how to find shortest route by weight. I need to find shortest route by weight, but I also need to limit how many hops are allowed. Is it possible with neo4j?

Update

current dataset:

MERGE (a:Loc {name:"A"})
MERGE (b:Loc {name:"B"})
MERGE (c:Loc {name:"C"})
MERGE (d:Loc {name:"D"})
MERGE (e:Loc {name:"E"})
MERGE (f:Loc {name:"F"})
MERGE (a)-[:ROAD {cost:50}]->(b)
MERGE (a)-[:ROAD {cost:50}]->(c)
MERGE (a)-[:ROAD {cost:100}]->(d)
MERGE (b)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:80}]->(e)
MERGE (d)-[:ROAD {cost:30}]->(e)
MERGE (d)-[:ROAD {cost:80}]->(f)
MERGE (e)-[:ROAD {cost:40}]->(f);

Current code:

MATCH (start:Loc{name:'A'}), (end:Loc{name:'F'})
CALL algo.kShortestPaths.stream(start, end, 40, 'cost' ,{})
yield nodeIds, costs
RETURN [node in algo.getNodesById(nodeIds) | node.name] AS places,
       costs,
       reduce(acc = 0.0, cost in costs | acc + cost) AS totalCost

Right now the code returns:

results

What I need to do is: I need to get from A to F with max 3 hops. Therefore I actually need only third row from results, first two are not important for me since they both have 4 hops.

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • Just run your dijskra weight algo and if the path is longer than your max value, don't take it. I think your asking this because you some performance issue so you want to cu off the algo as soon as possible. To do that you have to write your own procedure – logisima Feb 28 '19 at 12:15
  • @logisima this is not possible. Say no of max hops is 3, I have route with 4 hops and it costs 7, I also have route that has 3 hops but costs 9. I Right now Dijkstra returns 4 hop route, but I need 3 hop route. – user2728008 Feb 28 '19 at 12:31
  • Could you use the results of [`algo.kShortestPaths`](https://neo4j.com/docs/graph-algorithms/current/algorithms/yen-s-k-shortest-path/) and then constrain/sort/search for the number of acceptable hops? – rickhg12hs Feb 28 '19 at 12:50
  • @rickhg12hs kShortestPaths requires X as a parameter and then returns only first X results. But I don't if the first matching result with set max hops is within X or X+1. – user2728008 Feb 28 '19 at 12:58
  • So you don't know your [`maxDepth`](https://neo4j.com/docs/graph-algorithms/current/algorithms/yen-s-k-shortest-path/#algorithms-yens-k-shortest-path-syntax) parameter? – rickhg12hs Feb 28 '19 at 13:02
  • I do know maxDepth parameter, but I don't whether first result with, say 3 hops, will be 2 or 13 result by distance. Therefore I don't know how many rows should kShortestPaths return and I can't call the procedure. – user2728008 Feb 28 '19 at 13:06
  • could you provide a sample dataset and frame the problem with it? Also your current solution and what it is failing to accomplish. – David A Stumpf Feb 28 '19 at 14:13
  • I'm no expert, but I believe this is a [Resource Constrained Shortest Path](https://en.wikipedia.org/wiki/Constrained_Shortest_Path_First) Problem which according to [K. Mehlhorn and M. Ziegelmann](http://www.ziegelmann.org/ziegelmann/rcsp.pdf) is NP-Complete. Other than a dynamic program, etc., perhaps you could bound the maximum number of results necessary from `algo.kShortestPaths` and then sort those results. Depending on your graph size, max degree, etc., maybe that's good enough. – rickhg12hs Feb 28 '19 at 14:21
  • In your updated example, could `maxDepth:3` reduce your results to more what you're looking for? – rickhg12hs Feb 28 '19 at 14:38
  • @rickhg12hs maxDepth:3 doesn't do anything. It returns "(no changes, no records)". – user2728008 Feb 28 '19 at 14:48
  • Yeah, sorry. It's clear I don't exactly know what `maxDepth` is. You could add `WHERE size(nodeIds) = 4` after the `YIELD` to get your 3 hops... and then `ORDER BY totalCost` after the `RETURN` to sort. – rickhg12hs Feb 28 '19 at 16:13

0 Answers0