pgr_dagShortestPath - Experimental¶
pgr_dagShortestPath
— Returns the shortest path(s) for weighted directed acyclic graphs(DAG).
In particular, the DAG shortest paths algorithm implemented by Boost.Graph.
Warning
Possible server crash
These functions might create a server crash
Warning
Experimental functions
They are not officially of the current release.
They likely will not be officially be part of the next release:
The functions might not make use of ANY-INTEGER and ANY-NUMERICAL
Name might change.
Signature might change.
Functionality might change.
pgTap tests might be missing.
Might need c/c++ coding.
May lack documentation.
Documentation if any might need to be rewritten.
Documentation examples might need to be automatically generated.
Might need a lot of feedback from the comunity.
Might depend on a proposed function of pgRouting
Might depend on a deprecated function of pgRouting
Availability
Version 3.2.0
New experimental function:
pgr_dagShortestPath(Combinations)
Version 3.0.0
New experimental function
Description¶
Shortest Path for Directed Acyclic Graph(DAG) is a graph search algorithm that solves the shortest path problem for
weighted directed acyclic graph, producing a shortest path from
a starting vertex (start_vid
) to an ending vertex (end_vid
).
This implementation can only be used with a directed graph with no cycles i.e. directed acyclic graph.
The algorithm relies on topological sorting the dag to impose a linear ordering on the vertices, and thus is more efficient for DAG’s than either the Dijkstra or Bellman-Ford algorithm.
- The main characteristics are:
Process is valid for weighted directed acyclic graphs only. otherwise it will throw warnings.
Values are returned when there is a path.
When the starting vertex and ending vertex are the same, there is no path.
The agg_cost the non included values (v, v) is 0
When the starting vertex and ending vertex are the different and there is no path:
The agg_cost the non included values (u, v) is \(\infty\)
For optimization purposes, any duplicated value in the start_vids or end_vids are ignored.
The returned values are ordered:
start_vid ascending
end_vid ascending
Running time: \(O(| start\_vids | * (V + E))\)
Signatures¶
Summary
pgr_dagShortestPath(Edges SQL, from_vid, to_vid)
pgr_dagShortestPath(Edges SQL, from_vid, to_vids)
pgr_dagShortestPath(Edges SQL, from_vids, to_vid)
pgr_dagShortestPath(Edges SQL, from_vids, to_vids)
pgr_dagShortestPath(Edges SQL, Combinations) -- Experimental on v3.2
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
One to One¶
pgr_dagShortestPath(Edges SQL, from_vid, to_vid)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
- Example
From vertex \(1\) to vertex \(6\)
SELECT * FROM pgr_dagShortestPath(
'SELECT id, source, target, cost FROM edge_table',
1, 6
);
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 1 | 1 | 1 | 0
2 | 2 | 2 | 4 | 1 | 1
3 | 3 | 5 | 8 | 1 | 2
4 | 4 | 6 | -1 | 0 | 3
(4 rows)
One to Many¶
pgr_dagShortestPath(Edges SQL, from_vid, to_vids)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
- Example
From vertex \(1\) to vertices \(\{ 5, 6\}\)
SELECT * FROM pgr_dagShortestPath(
'SELECT id, source, target, cost FROM edge_table',
1, ARRAY[5,6]
);
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 1 | 1 | 1 | 0
2 | 2 | 2 | 4 | 1 | 1
3 | 3 | 5 | -1 | 0 | 2
4 | 1 | 1 | 1 | 1 | 0
5 | 2 | 2 | 4 | 1 | 1
6 | 3 | 5 | 8 | 1 | 2
7 | 4 | 6 | -1 | 0 | 3
(7 rows)
Many to One¶
pgr_dagShortestPath(Edges SQL, from_vids, to_vid)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
- Example
From vertices \(\{1, 3\}\) to vertex \(6\)
SELECT * FROM pgr_dagShortestPath(
'SELECT id, source, target, cost FROM edge_table',
ARRAY[1,3], 6
);
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 1 | 1 | 1 | 0
2 | 2 | 2 | 4 | 1 | 1
3 | 3 | 5 | 8 | 1 | 2
4 | 4 | 6 | -1 | 0 | 3
5 | 1 | 3 | 5 | 1 | 0
6 | 2 | 6 | -1 | 0 | 1
(6 rows)
Many to Many¶
pgr_dagShortestPath(Edges SQL, from_vids, to_vids)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
- Example
From vertices \(\{1, 4\}\) to vertices \(\{12, 6\}\)
SELECT * FROM pgr_dagShortestPath(
'SELECT id, source, target, cost FROM edge_table',
ARRAY[1, 4],ARRAY[12,6]
);
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 1 | 1 | 1 | 0
2 | 2 | 2 | 4 | 1 | 1
3 | 3 | 5 | 8 | 1 | 2
4 | 4 | 6 | -1 | 0 | 3
5 | 1 | 1 | 1 | 1 | 0
6 | 2 | 2 | 4 | 1 | 1
7 | 3 | 5 | 10 | 1 | 2
8 | 4 | 10 | 12 | 1 | 3
9 | 5 | 11 | 13 | 1 | 4
10 | 6 | 12 | -1 | 0 | 5
11 | 1 | 4 | 16 | 1 | 0
12 | 2 | 9 | 15 | 1 | 1
13 | 3 | 12 | -1 | 0 | 2
(13 rows)
Combinations¶
pgr_dagShortestPath(Edges SQL, Combinations)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
- Example
Using a combinations table on a Directed Acyclic Graph.
SELECT * FROM pgr_dagShortestPath(
'SELECT id, source, target, cost FROM edge_table',
'SELECT * FROM ( VALUES (1, 6), (4, 12) ) AS t(source, target)'
);
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 1 | 1 | 1 | 0
2 | 2 | 2 | 4 | 1 | 1
3 | 3 | 5 | 8 | 1 | 2
4 | 4 | 6 | -1 | 0 | 3
5 | 1 | 4 | 16 | 1 | 0
6 | 2 | 9 | 15 | 1 | 1
7 | 3 | 12 | -1 | 0 | 2
(7 rows)
Parameters¶
Description of the parameters of the signatures
Parameter |
Type |
Default |
Description |
---|---|---|---|
Edges SQL |
|
Edges query as described below. |
|
Combinations SQL |
|
Combinations query as described above. |
|
start_vid |
|
Identifier of the starting vertex of the path. |
|
start_vids |
|
Array of identifiers of starting vertices. |
|
end_vid |
|
Identifier of the ending vertex of the path. |
|
end_vids |
|
Array of identifiers of ending vertices. |
Inner Queries¶
Edges query¶
Column |
Type |
Default |
Description |
---|---|---|---|
id |
|
Identifier of the edge. |
|
source |
|
Identifier of the first end point vertex of the edge. |
|
target |
|
Identifier of the second end point vertex of the edge. |
|
cost |
|
Weight of the edge (source, target)
|
|
reverse_cost |
|
-1 |
Weight of the edge (target, source),
|
Where:
- ANY-INTEGER
SMALLINT, INTEGER, BIGINT
- ANY-NUMERICAL
SMALLINT, INTEGER, BIGINT, REAL, FLOAT
Combinations query¶
Column |
Type |
Default |
Description |
---|---|---|---|
source |
|
Identifier of the first end point vertex of the edge. |
|
target |
|
Identifier of the second end point vertex of the edge. |
Where:
- ANY-INTEGER
SMALLINT, INTEGER, BIGINT
Results Columns¶
Returns set of (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost)
Column |
Type |
Description |
---|---|---|
seq |
|
Sequential value starting from 1. |
path_seq |
|
Relative position in the path. Has value 1 for the beginning of a path. |
start_vid |
|
Identifier of the starting vertex. Returned when multiple starting vetrices are in the query. |
end_vid |
|
Identifier of the ending vertex. Returned when multiple ending vertices are in the query. |
node |
|
Identifier of the node in the path from |
edge |
|
Identifier of the edge used to go from |
cost |
|
Cost to traverse from |
agg_cost |
|
Aggregate cost from |
See Also¶
The queries use the Sample Data network.
Indices and tables