pgr_dijkstraNearCost
 Proposed¶
pgr_dijkstraNearCost
— Using dijkstra algorithm, finds the route that leads
to the nearest vertex.
Warning
Proposed functions for next mayor release.
They are not officially in the current release.
They will likely officially be part of the next mayor release:
The functions make use of ANYINTEGER and ANYNUMERICAL
Name might not change. (But still can)
Signature might not change. (But still can)
Functionality might not change. (But still can)
pgTap tests have being done. But might need more.
Documentation might need refinement.
Availability
Version 3.3.0
Promoted to proposed function
Version 3.2.0
New experimental function
Description¶
Given a graph, a starting vertex and a set of ending vertices, this function finds the shortest path from the starting vertex to the nearest ending vertex.
Characteristics¶
Uses Dijkstra algorithm.
Works for directed and undirected graphs.
When there are more than one path to the same vertex with same cost:
The algorithm will return just one path
Optionally allows to find more than one path.
When more than one path is to be returned:
Results are sorted in increasing order of:
aggregate cost
Within the same value of aggregate costs:
results are sorted by (source, target)
Running time: Dijkstra running time: \(drt = O((E + V)logV)\)
One to Many; \(drt\)
Many to One: \(drt\)
Many to Many: \(drt * Starting vids\)
Combinations: \(drt * Starting vids\)
Signatures¶
Summary
pgr_dijkstraNearCost(Edges SQL, start vid, end vids [, directed] [, cap]) pgr_dijkstraNearCost(Edges SQL, start vids, end vid [, directed] [, cap]) pgr_dijkstraNearCost(Edges SQL, start vids, end vids [, directed] [, cap], [global]) pgr_dijkstraNearCost(Edges SQL, Combinations SQL [, directed] [, cap] [, global]) RETURNS SET OF (start_vid, end_vid, agg_cost) OR EMPTY SET
One to Many¶
pgr_dijkstraNearCost(Edges SQL, start vid, end vids [, directed] [, cap]) RETURNS SET OF (start_vid, end_vid, agg_cost) OR EMPTY SET
 Example:
Departing on car from vertex \(6\) find the nearest subway station.
Using a directed graph for car routing.
The subway stations are on the following vertices \(\{1, 10, 11\}\)
The defaults used:
directed => true
cap => 1
1SELECT * FROM pgr_dijkstraNearCost(
2 'SELECT id, source, target, cost, reverse_cost FROM edges',
3 6, ARRAY[10, 11, 1]);
4 start_vid  end_vid  agg_cost
5++
6 6  11  2
7(1 row)
8
The result shows that station at vertex \(11\) is the nearest.
Many to One¶
pgr_dijkstraNearCost(Edges SQL, start vids, end vid [, directed] [, cap]) RETURNS SET OF (start_vid, end_vid, agg_cost) OR EMPTY SET
 Example:
Departing on a car from a subway station find the nearest two stations to vertex \(6\)
Using a directed graph for car routing.
The subway stations are on the following vertices \(\{1, 10, 11\}\)
On line 4: using the positional parameter: directed set to
true
In line 5: using named parameter cap => 2
1SELECT * FROM pgr_dijkstraNearCost(
2 'SELECT id, source, target, cost, reverse_cost FROM edges',
3 ARRAY[10, 11, 1], 6,
4 true,
5 cap => 2) ORDER BY agg_cost;
6 start_vid  end_vid  agg_cost
7++
8 10  6  1
9 11  6  2
10(2 rows)
11
The result shows that station at vertex \(10\) is the nearest and the next best is \(11\).
Many to Many¶
pgr_dijkstraNearCost(Edges SQL, start vids, end vids [, directed] [, cap], [global]) RETURNS SET OF (start_vid, end_vid, agg_cost) OR EMPTY SET
 Example:
Find the best pedestrian connection between two lines of buses
Unsing an undirected graph for pedestrian routing
The first subway line stations are at \(\{15, 16\}\)
The second subway line stations stops are at \(\{1, 10, 11\}\)
On line 4: using the named parameter: directed => false
The defaults used:
cap => 1
global => true
1SELECT * FROM pgr_dijkstraNearCost(
2 'SELECT id, source, target, cost, reverse_cost FROM edges',
3 ARRAY[15, 16], ARRAY[10, 11, 1],
4 directed => false);
5 start_vid  end_vid  agg_cost
6++
7 15  10  1
8(1 row)
9
For a pedestrian the best connection is to get on/off is at vertex \(15\) of the first subway line and at vertex \(10\) of the second subway line.
Only one route is returned because global is true
and cap is 1
Combinations¶
pgr_dijkstraNearCost(Edges SQL, Combinations SQL [, directed] [, cap] [, global]) RETURNS SET OF (start_vid, end_vid, agg_cost) OR EMPTY SET
 Example:
Find the best car connection between all the stations of two subway lines
Using a directed graph for car routing.
The first subway line stations stops are at \(\{1, 10, 11\}\)
The second subway line stations are at \(\{15, 16\}\)
The combinations contents:
SELECT unnest(ARRAY[10, 11, 1]) as source, target
FROM (SELECT unnest(ARRAY[15, 16]) AS target) a
UNION
SELECT unnest(ARRAY[15, 16]), target
FROM (SELECT unnest(ARRAY[10, 11, 1]) AS target) b ORDER BY source, target;
source  target
+
1  15
1  16
10  15
10  16
11  15
11  16
15  1
15  10
15  11
16  1
16  10
16  11
(12 rows)
The query:
lines 3~4 sets the start vertices to be from the fisrt subway line and the ending vertices to be from the second subway line
lines 6~7 sets the start vertices to be from the first subway line and the ending vertices to be from the first subway line
On line 8: using the named parameter is global => false
The defaults used:
directed => true
cap => 1
1SELECT * FROM pgr_dijkstraNearCost(
2 'SELECT id, source, target, cost, reverse_cost FROM edges',
3 'SELECT unnest(ARRAY[10, 11, 1]) as source, target
4 FROM (SELECT unnest(ARRAY[15, 16]) AS target) a
5 UNION
6 SELECT unnest(ARRAY[15, 16]), target
7 FROM (SELECT unnest(ARRAY[10, 11, 1]) AS target) b',
8 global => false);
9 start_vid  end_vid  agg_cost
10++
11 11  16  1
12 15  10  1
13 16  11  1
14 10  16  2
15 1  16  4
16(5 rows)
17
From the results:
making a connection from the first subway line \(\{1, 10, 11\}\) to the second \(\{15, 16\}\):
The best connections from all the stations from the first line are: \({(1 \rightarrow 16) (10 \rightarrow 16) (11 \rightarrow 16)}\)
The best one is \((11 \rightarrow 16)\) with a cost of \(1\) (lines: 1)
making a connection from the second subway line \(\{15, 16\}\) to the first \(\{1, 10, 11\}\):
The best connections from all the stations from the second line are: \({(15 \rightarrow 10) (16 \rightarrow 11)}\)
Both are equaly good as they have the same cost. (lines: 12 and 13)
Parameters¶
Column 
Type 
Description 


Edges SQL as described below 


Combinations SQL as described below 

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. 
Dijkstra optional parameters¶
Column 
Type 
Default 
Description 





Near optional parameters¶
Parameter 
Type 
Default 
Description 




Find at most 




Inner Queries¶
Edges SQL¶
Column 
Type 
Default 
Description 


ANYINTEGER 
Identifier of the edge. 


ANYINTEGER 
Identifier of the first end point vertex of the edge. 


ANYINTEGER 
Identifier of the second end point vertex of the edge. 


ANYNUMERICAL 
Weight of the edge ( 


ANYNUMERICAL 
1 
Weight of the edge (

Where:
 ANYINTEGER:
SMALLINT
,INTEGER
,BIGINT
 ANYNUMERICAL:
SMALLINT
,INTEGER
,BIGINT
,REAL
,FLOAT
Combinations SQL¶
Parameter 
Type 
Description 


ANYINTEGER 
Identifier of the departure vertex. 

ANYINTEGER 
Identifier of the arrival vertex. 
Where:
 ANYINTEGER:
SMALLINT
,INTEGER
,BIGINT
Result Columns¶
Set of (start_vid, end_vid, agg_cost)
Column 
Type 
Description 



Identifier of the starting vertex. 


Identifier of the ending vertex. 


Aggregate cost from 
See Also¶
Sample Data network.
boost: https://www.boost.org/libs/graph/doc/table_of_contents.html
Wikipedia: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Indices and tables