Graph Data Science

Yen's algorithm

An algorithm that finds the k cheapest routes between two nodes, rather than only the cheapest.

Example

Three routes run from Ashford to Croydon. Two of them stop once on the way. The third stops twice.

cypher
MATCH (source:Stop {name: 'Ashford'}), (target:Stop {name: 'Croydon'})
CALL gds.shortestPath.yens.stream('roads', {
  sourceNode: source,
  targetNode: target,
  k: 2
})
YIELD index, totalCost, path
RETURN index, totalCost, path

k sets how many routes come back. At k: 2 the call returns the route through Barnet and the route through Dover. The route through Epsom and Fulham is one road longer, so it is left out.

Lessons that use this term

The lesson and course links below open in a new tab.

No published lesson uses this term yet.

All glossary terms