image thumbnail

K-Shortest Path- Yen's algorithm

version (11.6 KB) by Meral Sh.
Based on Yen’s algorithm, returns the K shortest paths between a source and a destination.


Updated 12 Aug 2011

View License

This function is based on Yen's k-Shortest Path algorithm:
J. Y. Yen, "Finding the K shortest loopless paths in a network", Management Science 17:712–716, 1971.

It returns:
1) [shortestPaths]: the list of K shortest paths (in cell array 1xK)
2) [totalCosts] : costs of the K shortest paths (in array 1xK)
Yen's algorithm prevents loops.

This function calls a slightly modified/simplified function dijkstra() (submitted by Xiaodong Wang, 2004)

The Network/Graph of N nodes is fed in the form of a NXN netCostMatrix which must have positive weights/costs.

IMPORTANT: see 'TestKShortestPath.m' and 'Test graph (case 1).pdf' for netCostMatrix format.

Cite As

Meral Sh. (2022). K-Shortest Path- Yen's algorithm (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!