pgr_dijkstraNear  Proposed¶
pgr_dijkstraNear
— 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_dijkstraNear(Edges SQL, Start vid, End vids [, directed] [, cap])
pgr_dijkstraNear(Edges SQL, Start vids, End vid [, directed] [, cap])
pgr_dijkstraNear(Edges SQL, Start vids, End vids [, directed] [, cap], [global])
pgr_dijkstraNear(Edges SQL, Combinations SQL [, directed] [, cap], [global])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
One to Many¶
pgr_dijkstraNear(Edges SQL, Start vid, End vids [, directed] [, cap])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
 Example
Departing on car from vertex \(2\) find the nearest subway station.
Using a directed graph for car routing.
The subway stations are on the following vertices \(\{ 3, 6, 7\}\)
The defaults used:
directed => true
cap => 1
1SELECT * FROM pgr_dijkstraNear(
2 'SELECT id, source, target, cost, reverse_cost FROM edge_table',
3 2, ARRAY[3, 6, 7]
4);
5 seq  path_seq  start_vid  end_vid  node  edge  cost  agg_cost
6+++++++
7 1  1  2  6  2  4  1  0
8 2  2  2  6  5  8  1  1
9 3  3  2  6  6  1  0  2
10(3 rows)
11
The result shows that station at vertex \(6\) is the nearest.
Many to One¶
pgr_dijkstraNear(Edges SQL, Start vids, End vid [, directed] [, cap])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
 Example
Departing on a car from a subway station find the nearest two stations to vertex \(2\)
Using a directed graph for car routing.
The subway stations are on the following vertices \(\{ 3, 6, 7\}\)
On line 4: using the positional parameter: directed set to
true
In line 5: using named parameter cap => 2
1SELECT * FROM pgr_dijkstraNear(
2 'SELECT id, source, target, cost, reverse_cost FROM edge_table',
3 ARRAY[3, 6, 7], 2,
4 true,
5 cap => 2
6);
7 seq  path_seq  start_vid  end_vid  node  edge  cost  agg_cost
8+++++++
9 1  1  3  2  3  2  1  0
10 2  2  3  2  2  1  0  1
11 3  1  6  2  6  8  1  0
12 4  2  6  2  5  4  1  1
13 5  3  6  2  2  1  0  2
14(5 rows)
15
The result shows that station at vertex \(3\) is the nearest and the next best is \(6\).
Many to Many¶
pgr_dijkstraNear(Edges SQL, Start vids, End vids [, directed] [, cap], [global])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, 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 stops are at \(\{3, 6, 7\}\)
The second subway line stations are at \(\{4, 9\}\)
On line 4: using the named parameter: directed => false
The defaults used:
cap => 1
global => true
1SELECT * FROM pgr_dijkstraNear(
2 'SELECT id, source, target, cost, reverse_cost FROM edge_table',
3 ARRAY[4, 9], ARRAY[3, 6, 7],
4 directed => false
5);
6 seq  path_seq  start_vid  end_vid  node  edge  cost  agg_cost
7+++++++
8 1  1  4  3  4  3  1  0
9 2  2  4  3  3  1  0  1
10(2 rows)
11
For a pedestrian the best connection is to get on/off is at vertex \(3\) of the first subway line and at vertex \(4\) of the second subway line.
Only one route is returned because global is true
and cap is 1
Combinations¶
pgr_dijkstraNear(Edges SQL, Combinations SQL [, directed] [, cap], [global])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, 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 \(\{3, 6, 7\}\)
The second subway line stations are at \(\{4, 9\}\)
line 3 sets the start vertices to be from the fisrt subway line and the ending vertices to be from the second subway line
line 5 sets the start vertices to be from the first subway line and the ending vertices to be from the first subway line
On line 6: using the named parameter is global => false
The defaults used:
directed => true
cap => 1
1SELECT * FROM pgr_dijkstraNear(
2 'SELECT id, source, target, cost, reverse_cost FROM edge_table',
3 'SELECT unnest(ARRAY[3, 6, 7]) as source, target FROM (SELECT unnest(ARRAY[4, 9]) AS target) a
4 UNION
5 SELECT unnest(ARRAY[4, 9]), target FROM (SELECT unnest(ARRAY[3, 6, 7]) AS target) b',
6 global => false
7);
8 seq  path_seq  start_vid  end_vid  node  edge  cost  agg_cost
9+++++++
10 1  1  4  3  4  3  1  0
11 2  2  4  3  3  1  0  1
12 3  1  6  9  6  9  1  0
13 4  2  6  9  9  1  0  1
14 5  1  9  6  9  9  1  0
15 6  2  9  6  6  1  0  1
16 7  1  3  9  3  5  1  0
17 8  2  3  9  6  9  1  1
18 9  3  3  9  9  1  0  2
19 10  1  7  9  7  6  1  0
20 11  2  7  9  8  7  1  1
21 12  3  7  9  5  8  1  2
22 13  4  7  9  6  9  1  3
23 14  5  7  9  9  1  0  4
24(14 rows)
25
From the results:
making a connection from the first subway line to the second:
\({(3 > 9) (6 > 9) (7 > 9)}\) and the best one is \((6 > 9)\) with a cost of \(1\) (lines: 12 and 13)
making a connection from the second subway line to the first:
\({(4 > 3) (9 > 6)}\) and both are equaly good as they have the same cost. (lines: 10 and 11 and lines: 14 and 15)
Parameters¶
Parameter 
Type 
Default 
Description 

Edges SQL 

Edges query as described below 

Combinations SQL 

Combinations query 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. 

directed 



cap 

1 
Find at most 
global 



Inner query¶
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:
 ANYINTEGER
SMALLINT, INTEGER, BIGINT
 ANYNUMERICAL
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:
 ANYINTEGER
SMALLINT, INTEGER, BIGINT
Return Columns¶
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Column 
Type 
Description 

seq 

Sequential value starting from 1. 
path_seq 

Sequential value starting from 1 for each \((start\_vid \to end\_vid)\) path. 
start_vid 

Identifier of the starting vertex of the path. 
end_vid 

Identifier of the ending vertex of the path. 
node 

Identifier of the node at position 
edge 

Identifier of the edge used to go from node at

cost 

Cost to traverse from

agg_cost 

Total cost of traversing \((start\_vid \to node)\) section of the \((start\_vid \to end\_vid)\) path. 
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