# pgr_dijkstraNear - Proposed¶

pgr_dijkstraNear — Using Dijkstra’s 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 ANY-INTEGER and ANY-NUMERICAL

• 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|)log|V|)$$

• 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 (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 (seq, path_seq, start_vid, end_vid, node, edge, cost, 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_dijkstraNear(
2  'SELECT id, source, target, cost, reverse_cost FROM edges',
3  6, ARRAY[10, 11, 1]);
4 seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
5-----+----------+-----------+---------+------+------+------+----------
6   1 |        1 |         6 |      11 |    6 |    4 |    1 |        0
7   2 |        2 |         6 |      11 |    7 |    8 |    1 |        1
8   3 |        3 |         6 |      11 |   11 |   -1 |    0 |        2
9(3 rows)
10


The result shows that station at vertex $$11$$ is the nearest.

### Many to One¶

pgr_dijkstraNear(Edges SQL, start vids, end vid
[, directed] [, cap])
RETURNS (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 $$\{ 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_dijkstraNear(
2  'SELECT id, source, target, cost, reverse_cost FROM edges',
3  ARRAY[10, 11, 1], 6,
4  true,
5  cap => 2);
6 seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
7-----+----------+-----------+---------+------+------+------+----------
8   1 |        1 |        10 |       6 |   10 |    2 |    1 |        0
9   2 |        2 |        10 |       6 |    6 |   -1 |    0 |        1
10   3 |        1 |        11 |       6 |   11 |    8 |    1 |        0
11   4 |        2 |        11 |       6 |    7 |    4 |    1 |        1
12   5 |        3 |        11 |       6 |    6 |   -1 |    0 |        2
13(5 rows)
14


The result shows that station at vertex $$10$$ is the nearest and the next best is $$11$$.

### Many to Many¶

pgr_dijkstraNear(Edges SQL, start vids, end vids
[, directed] [, cap], [global])
pgr_dijkstraNear(Edges SQL, Start vids, End vids
[, directed] [, cap], [global])
RETURNS (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 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_dijkstraNear(
2  'SELECT id, source, target, cost, reverse_cost FROM edges',
3  ARRAY[15, 16], ARRAY[10, 11, 1],
4  directed => false);
5 seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
6-----+----------+-----------+---------+------+------+------+----------
7   1 |        1 |        15 |      10 |   15 |    3 |    1 |        0
8   2 |        2 |        15 |      10 |   10 |   -1 |    0 |        1
9(2 rows)
10


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_dijkstraNear(Edges SQL, Combinations SQL
[, directed] [, cap] [, global])
RETURNS (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 $$\{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 first 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_dijkstraNear(
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 seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
10-----+----------+-----------+---------+------+------+------+----------
11   1 |        1 |        11 |      16 |   11 |    9 |    1 |        0
12   2 |        2 |        11 |      16 |   16 |   -1 |    0 |        1
13   3 |        1 |        15 |      10 |   15 |    3 |    1 |        0
14   4 |        2 |        15 |      10 |   10 |   -1 |    0 |        1
15   5 |        1 |        16 |      11 |   16 |    9 |    1 |        0
16   6 |        2 |        16 |      11 |   11 |   -1 |    0 |        1
17   7 |        1 |        10 |      16 |   10 |    5 |    1 |        0
18   8 |        2 |        10 |      16 |   11 |    9 |    1 |        1
19   9 |        3 |        10 |      16 |   16 |   -1 |    0 |        2
20  10 |        1 |         1 |      16 |    1 |    6 |    1 |        0
21  11 |        2 |         1 |      16 |    3 |    7 |    1 |        1
22  12 |        3 |         1 |      16 |    7 |    8 |    1 |        2
23  13 |        4 |         1 |      16 |   11 |    9 |    1 |        3
24  14 |        5 |         1 |      16 |   16 |   -1 |    0 |        4
25(14 rows)
26


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: 11 and 12)

• 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: 13 and 14 and lines: 15 and 16)

## Parameters¶

Column

Type

Description

Edges SQL

TEXT

Edges SQL as described below

Combinations SQL

TEXT

Combinations SQL as described below

start vid

BIGINT

Identifier of the starting vertex of the path.

start vids

ARRAY[BIGINT]

Array of identifiers of starting vertices.

end vid

BIGINT

Identifier of the ending vertex of the path.

end vids

ARRAY[BIGINT]

Array of identifiers of ending vertices.

### Dijkstra optional parameters¶

Column

Type

Default

Description

directed

BOOLEAN

true

• When true the graph is considered Directed

• When false the graph is considered as Undirected.

### Near optional parameters¶

Parameter

Type

Default

Description

cap

BIGINT

1

Find at most cap number of nearest shortest paths

global

BOOLEAN

true

• When true: only cap limit results will be returned

• When false: cap limit per Start vid will be returned

## Inner Queries¶

### Edges SQL¶

Column

Type

Default

Description

id

ANY-INTEGER

Identifier of the edge.

source

ANY-INTEGER

Identifier of the first end point vertex of the edge.

target

ANY-INTEGER

Identifier of the second end point vertex of the edge.

cost

ANY-NUMERICAL

Weight of the edge (source, target)

reverse_cost

ANY-NUMERICAL

-1

Weight of the edge (target, source)

• When negative: edge (target, source) does not exist, therefore it’s not part of the graph.

Where:

ANY-INTEGER:

SMALLINT, INTEGER, BIGINT

ANY-NUMERICAL:

SMALLINT, INTEGER, BIGINT, REAL, FLOAT

### Combinations SQL¶

Parameter

Type

Description

source

ANY-INTEGER

Identifier of the departure vertex.

target

ANY-INTEGER

Identifier of the arrival vertex.

Where:

ANY-INTEGER:

SMALLINT, INTEGER, BIGINT

## Result Columns¶

Returns (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)

Column

Type

Description

seq

INTEGER

Sequential value starting from 1.

path_seq

INTEGER

Relative position in the path. Has value 1 for the beginning of a path.

start_vid

BIGINT

Identifier of the starting vertex of the current path.

end_vid

BIGINT

Identifier of the ending vertex of the current path.

node

BIGINT

Identifier of the node in the path from start_vid to end_vid.

edge

BIGINT

Identifier of the edge used to go from node to the next node in the path sequence. -1 for the last node of the path.

cost

FLOAT

Cost to traverse from node using edge to the next node in the path sequence.

agg_cost

FLOAT

Aggregate cost from start_vid to node.