In a previous question on how to calculate many shortest paths, @dkastl pointed to the pgr_kdijkstra function, which allows to calculate each time the total cost of one source-to-many targets shortest paths, by giving an array of target nodes.

What I'd like to do is, to calculate the total cost of many sources-to-one target shortest paths. A solution could be to switch source and target, but then I'm actually calculating the total cost of a different chain of paths, given the fact I'm using a directed topology network.

@UnderDark already provided a solution for this problem, but, out of curiosity, my question is:

Is there a way of modifying the pgr_kdijkstra function such that you're able to give an 'array of sources' and 'one target' as input?

Something like (given the example of pgRouting manual):

SELECT seq, id1 AS source, id2 AS target, cost FROM pgr_kdijkstraCost(
'SELECT id, source, target, cost FROM edge_table WHERE cost >= 0',
array[4,12], 10, false, false);

 seq | source | target | cost
   0 |      4 |     10 |    4
   1 |     12 |     10 |    2


1 Answer 1


If you swap all oneway streets, then you can still get the same routes as a many-to-one calculation. Much faster than doing many one-to-one routes. If you don't care about speed, but just want to do it as simple as possible, use the query from underdark which I guess is something about doing all-to-all and selecting the needed routes from that.

  • Thanks for your response @Uffe. Swapping the one-way streets is a nice trick which I didn't think of; I'll consider that. The query given by Underdark is not really an all-to-all, at least if you create a table with the sources and targets defining the routes to be calculated on beforehand.
    – kadéem
    Jul 8, 2014 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.