Most algorithms have two functions:

  • A normal one: like dijkstra()
  • A Cost-version: like DijkstraCost()

I want the result of the cost version: A fast computation of the total seconds. In mine edges table I have the following fields:

  • Seconds: Time to walk
  • Score: More a 'road class'
  • Penalty: Take the escalator above the stairs (which is not faster if you stand still)

The field I use in SQL for DijkstraCost(): cost = Score * Penalty

In many cases the costs are in seconds, but not here. So DijkstraCost() returns the real costs (in my case for every edge: score*penalty into an aggregate)

My question is :

How do I get the total sum of seconds with a Cost-version like DijkstraCost()

My SQL looks like this:

SELECT * FROM pgr_dijkstraCost(
    'SELECT id,
    score* penalty  AS cost,
    score* penalty  AS reverse_cost,

    geom AS the_geom
    FROM network'

The cost of pgRouting is used for the cost function. In your case the cost is "score * penalty".

The pgr_dijkstraCost() function is mainly for convenience, for example when you request multiple origins/destinations at the same time but are just interested in the total cost. However, if the actual aggregated cost should be a different one than the one used for the cost function, then you need to use pg_dijkstra() and join the shortest path result with your original table to use other attributes as costs.


SELECT dijkstra.*, network.my_cost, ST_AsText(network.the_geom)
  FROM pgr_dijkstra(<...>) AS dijkstra
  LEFT JOIN network
  ON (edge = gid) ORDER BY seq;
  • Okay, and if I am only interested in the 'score*cost' I aggregate that. I just assumed that the dijkstraCost() function would be faster.
    – GeoDev
    Feb 18 '20 at 6:54
  • 1
    You could write your own function to use different costs for aggregation, and a good starting point might be this file, that defines the dijkstraCost "wrapper" functions: github.com/pgRouting/pgrouting/blob/master/sql/dijkstra/…
    – dkastl
    Feb 19 '20 at 7: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.