This algorithm is designed to return multiple alternative paths, so it not only finds the shortest path, but also K-1 alternative paths. pgRouting implementation is based on Jin J. Yen's works.
The basic usage is as follows:
select * from pgr_ksp('select gid as id, source, target, cost, x1, y1, x2, y2 from pgr.ways', 79240, 9064, 2);
The output is very similar to what we have seen so far; the difference is the presence of an additional column that indicates the path identifier:
seq|path_id|path_seq| node| edge| cost| agg_cost
---+-------+--------+-----+-----+-----------------+-----------------
1| 1| 1|79240|69362| 0.00152459527744| 0
2| 1| 2|52082|44175|0.000969276126804|0.001524595277445
...
When we visualize the preceding output, it looks as follows:
