Description
The pgr_dijkstraCost
function sumarizes of the cost of the shortest path(s)
using Dijkstra Algorithm.
Dijkstra’s algorithm, conceived by Dutch computer scientist Edsger Dijkstra in
1956.
It is a graph search algorithm that solves the shortest path problem for a graph
with non-negative edge path costs, producing a shortest path from a starting
vertex to an ending vertex.
This implementation can be used with a directed graph and an undirected graph.
- Process is done only on edges with positive costs.
- A negative value on a cost column is interpreted as the edge does not exist.
- Values are returned when there is a path.
- When there is no path:
- When the starting vertex and ending vertex are the same.
- The aggregate cost of the non included values \((v, v)\) is
\(0\)
- When the starting vertex and ending vertex are the different and there is
no path:
- The aggregate cost the non included values \((u, v)\) is
\(\infty\)
- For optimization purposes, any duplicated value in the starting vertices or on
the ending vertices are ignored.
- Running time: \(O(| start\ vids | * (V \log V + E))\)
- It does not return a path.
- Returns the sum of the costs of the shortest path of each pair combination of
nodes requested.
- Let be the case the values returned are stored in a table, so the unique index
would be the pair:
(start_vid, end_vid)
.
- Depending on the function and its parameters, the results can be symmetric.
- The aggregate cost of \((u, v)\) is the same as for \((v, u)\).
- Any duplicated value in the start or end vertex identifiers are ignored.
- The returned values are ordered:
start_vid
ascending
end_vid
ascending
Signatures
Summary
pgr_dijkstraCost(Edges SQL, start vid, end vid [, directed])
pgr_dijkstraCost(Edges SQL, start vid, end vids [, directed])
pgr_dijkstraCost(Edges SQL, start vids, end vid [, directed])
pgr_dijkstraCost(Edges SQL, start vids, end vids [, directed])
pgr_dijkstraCost(Edges SQL, Combinations SQL [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
One to One
pgr_dijkstraCost(Edges SQL, start vid, end vid [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example: | From vertex \(6\) to vertex \(10\) on a directed graph |
SELECT * FROM pgr_dijkstraCost(
'SELECT id, source, target, cost, reverse_cost FROM edges',
6, 10, true);
start_vid | end_vid | agg_cost
-----------+---------+----------
6 | 10 | 5
(1 row)
One to Many
pgr_dijkstraCost(Edges SQL, start vid, end vids [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example: | From vertex \(6\) to vertices \(\{10, 17\}\) on a directed
graph |
SELECT * FROM pgr_dijkstraCost(
'SELECT id, source, target, cost, reverse_cost FROM edges',
6, ARRAY[10, 17]);
start_vid | end_vid | agg_cost
-----------+---------+----------
6 | 10 | 5
6 | 17 | 4
(2 rows)
Many to One
pgr_dijkstraCost(Edges SQL, start vids, end vid [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example: | From vertices \(\{6, 1\}\) to vertex \(17\) on a directed
graph |
SELECT * FROM pgr_dijkstraCost(
'SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[6, 1], 17);
start_vid | end_vid | agg_cost
-----------+---------+----------
1 | 17 | 5
6 | 17 | 4
(2 rows)
Many to Many
pgr_dijkstraCost(Edges SQL, start vids, end vids [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example: | From vertices \(\{6, 1\}\) to vertices \(\{10, 17\}\) on an
undirected graph |
SELECT * FROM pgr_dijkstraCost(
'SELECT id, source, target, cost, reverse_cost FROM edges',
ARRAY[6, 1], ARRAY[10, 17],
directed => false);
start_vid | end_vid | agg_cost
-----------+---------+----------
1 | 10 | 4
1 | 17 | 5
6 | 10 | 1
6 | 17 | 4
(4 rows)
Combinations
pgr_dijkstraCost(Edges SQL, Combinations SQL [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example: | Using a combinations table on an undirected graph |
The combinations table:
SELECT source, target FROM combinations;
source | target
--------+--------
5 | 6
5 | 10
6 | 5
6 | 15
6 | 14
(5 rows)
The query:
SELECT * FROM pgr_dijkstraCost(
'SELECT id, source, target, cost, reverse_cost FROM edges',
'SELECT source, target FROM combinations',
false);
start_vid | end_vid | agg_cost
-----------+---------+----------
5 | 6 | 1
5 | 10 | 2
6 | 5 | 1
6 | 15 | 2
(4 rows)