I have a table with 2 columns: column A with the startvertex and column B with endvertex.

How can I do a OD pairs shortest path?

It is with a loop I suppose, but how can this loop be implemented into pgr_Dijkstra? It's impossible do to all the OD pairs manually, so an iteration is necessary.

I have already a working query for pgr_Dijkstra for a single point-to-point, but the problem is to do it for all the OD-pairs one-by-one.


pgr_Dijkstra caan also solve 1->n and n->m shortest path. When you look at the function documentation you will see, that you can pass an array of start or end ID's.

SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', ARRAY[2,11], ARRAY[3,5], FALSE );


| improve this answer | |
  • This is for many×many, not pair to pair. – Guoyang Qin Jul 11 '18 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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