Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created June 8, 2014 21:30
Show Gist options
  • Select an option

  • Save mattdeboard/22d8f9acc06a73f4a96a to your computer and use it in GitHub Desktop.

Select an option

Save mattdeboard/22d8f9acc06a73f4a96a to your computer and use it in GitHub Desktop.
match (c1:City {name: "New York"}),
(c2:City {name: "Seattle"}),
p = allShortestPaths((c2)-[*]-(c1))
return distinct reduce(routeDesc = "", n in nodes(p) |
routeDesc + n.name + " -> ") as routeDesc,
reduce(totalCost = 0, r in relationships(p) |
totalCost + r.cost) as routeCost;
/*
+---------------------------------------------------------------------------------------------+
| routeDesc | routeCost |
+---------------------------------------------------------------------------------------------+
| "Seattle -> Helena -> Duluth -> Toronto -> Montreal -> New York -> " | 24 |
| "Seattle -> Helena -> Winnipeg -> Sault St. Marie -> Montreal -> New York -> " | 24 |
| "Seattle -> Calgary -> Winnipeg -> Sault St. Marie -> Montreal -> New York -> " | 24 |
| "Seattle -> Helena -> Duluth -> Sault St. Marie -> Montreal -> New York -> " | 23 |
| "Seattle -> Helena -> Omaha -> Chicago -> Pittsburgh -> New York -> " | 20 |
| "Seattle -> Helena -> Duluth -> Chicago -> Pittsburgh -> New York -> " | 20 |
+---------------------------------------------------------------------------------------------+
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment