# pgr_trsp - Proposed¶

pgr_trsp - routing vertices with restrictions.

Availability

• Version 3.4.0

• New Proposed signatures

• Signature pgr_trsp(text,integer,integer,boolean,boolean,text) is deprecated

• Signature pgr_trsp(text,integer,float,integer,float,boolean,boolean,text) is deprecated

• Signature pgr_trspViaVertices(text,anyarray,boolean,boolean,text) is deprecated

• Signature pgr_trspviaedges(text,integer[],double precision[],boolean,boolean,text) is deprecated

• Version 2.1.0

• New Via prototypes

• pgr_trspViaVertices

• pgr_trspViaEdges

• Version 2.0.0

• Official function

## Description¶

Turn restricted shortest path (TRSP) is an algorithm that receives turn restrictions in form of a query like those found in real world navigable road networks.

The main characteristics are:

• Automatic detection of reverse_cost column

• Accepts ANY-INTEGER and ANY-NUMERICAL on input columns

• All variations give as result the same columns

• It does no guarantee the shortest path as it might contain restriction paths

The general algorithm is as follows:

• Execute a Dijkstra

• If the solution passes thru a restriction then

• Execute the TRSP algorithm with restrictions

## Signatures¶

Summary

Proposed

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.

pgr_trsp(Edges SQL, Restrictions SQL, start vid,  end vid  [, directed]) -- Proposed on v3.4
pgr_trsp(Edges SQL, Restrictions SQL, start vid,  end vids [, directed]) -- Proposed on v3.4
pgr_trsp(Edges SQL, Restrictions SQL, start vids, end vid  [, directed]) -- Proposed on v3.4
pgr_trsp(Edges SQL, Restrictions SQL, start vids, end vids [, directed]) -- Proposed on v3.4
pgr_trsp(Edges SQL, Restrictions SQL, Combinations SQL, [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET

### One to One¶

pgr_trsp(Edges SQL, Restrictions SQL, start vid,  end vid  [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example

From vertex $$2$$ to vertex $$3$$ on an undirected graph

SELECT * FROM pgr_trsp(
$$SELECT id, source, target, cost, reverse_cost FROM edge_table$$,
$$SELECT path, cost FROM restrictions$$,
2, 3,
false
);
seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
-----+----------+-----------+---------+------+------+------+----------
1 |        1 |         2 |       3 |    2 |    2 |    1 |        0
2 |        2 |         2 |       3 |    3 |   -1 |    0 |        1
(2 rows)



### One to Many¶

pgr_trsp(Edges SQL, Restrictions SQL, start vid,  end vids [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example

From vertex $$2$$ to vertices $$\{3, 7\}$$ on an undirected graph

SELECT * FROM pgr_trsp(
$$SELECT id, source, target, cost FROM edge_table$$,
$$SELECT * FROM restrictions$$,
2, ARRAY[3,7],
false
);
seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
-----+----------+-----------+---------+------+------+------+----------
1 |        1 |         2 |       3 |    2 |    4 |    1 |        0
2 |        2 |         2 |       3 |    5 |    8 |    1 |        1
3 |        3 |         2 |       3 |    6 |    5 |    1 |        2
4 |        4 |         2 |       3 |    3 |   -1 |    0 |        3
5 |        1 |         2 |       7 |    2 |    4 |    1 |        0
6 |        2 |         2 |       7 |    5 |   10 |    1 |        1
7 |        3 |         2 |       7 |   10 |   12 |    1 |        2
8 |        4 |         2 |       7 |   11 |   11 |    1 |        3
9 |        5 |         2 |       7 |    6 |    8 |    1 |        4
10 |        6 |         2 |       7 |    5 |    7 |    1 |        5
11 |        7 |         2 |       7 |    8 |    6 |    1 |        6
12 |        8 |         2 |       7 |    7 |   -1 |    0 |        7
(12 rows)



### Many to One¶

pgr_trsp(Edges SQL, Restrictions SQL, start vids, end vid  [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example

From vertices $$\{2, 7\}$$ to vertex $$10$$ on a directed graph

SELECT * FROM pgr_trsp(
$$SELECT id, source, target, cost, reverse_cost FROM edge_table$$,
$$SELECT path, cost FROM restrictions$$,
ARRAY[2,7], 10
);
seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
-----+----------+-----------+---------+------+------+------+----------
1 |        1 |         2 |      10 |    2 |    4 |    1 |        0
2 |        2 |         2 |      10 |    5 |   10 |    1 |        1
3 |        3 |         2 |      10 |   10 |   -1 |    0 |        2
4 |        1 |         7 |      10 |    7 |    6 |    1 |        0
5 |        2 |         7 |      10 |    8 |    7 |    1 |        1
6 |        3 |         7 |      10 |    5 |   10 |  101 |        2
7 |        4 |         7 |      10 |   10 |   -1 |    0 |      103
(7 rows)



### Many to Many¶

pgr_trsp(Edges SQL, Restrictions SQL, start vids, end vids [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example

From vertices $$\{2, 7\}$$ to vertices $$\{3, 10\}$$ on an undirected graph

SELECT * FROM pgr_trsp(
$$SELECT id, source, target, cost, reverse_cost FROM edge_table$$,
$$SELECT path, cost FROM restrictions$$,
ARRAY[2,7], ARRAY[3,10],
false
);
seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
-----+----------+-----------+---------+------+------+------+----------
1 |        1 |         2 |       3 |    2 |    2 |    1 |        0
2 |        2 |         2 |       3 |    3 |   -1 |    0 |        1
3 |        1 |         2 |      10 |    2 |    4 |    1 |        0
4 |        2 |         2 |      10 |    5 |   10 |    1 |        1
5 |        3 |         2 |      10 |   10 |   -1 |    0 |        2
6 |        1 |         7 |       3 |    7 |    6 |    1 |        0
7 |        2 |         7 |       3 |    8 |    7 |    1 |        1
8 |        3 |         7 |       3 |    5 |    4 |    1 |        2
9 |        4 |         7 |       3 |    2 |    2 |    1 |        3
10 |        5 |         7 |       3 |    3 |   -1 |    0 |        4
11 |        1 |         7 |      10 |    7 |    6 |    1 |        0
12 |        2 |         7 |      10 |    8 |    7 |    1 |        1
13 |        3 |         7 |      10 |    5 |    8 |    1 |        2
14 |        4 |         7 |      10 |    6 |    5 |    1 |        3
15 |        5 |         7 |      10 |    3 |    2 |    1 |        4
16 |        6 |         7 |      10 |    2 |    4 |    1 |        5
17 |        7 |         7 |      10 |    5 |   10 |    1 |        6
18 |        8 |         7 |      10 |   10 |   -1 |    0 |        7
(18 rows)



### Combinations¶

pgr_trsp(Edges SQL, Restrictions SQL, Combinations SQL, [, directed]) -- Proposed on v3.4
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example

Using a combinations table on an undirected graph

SELECT * FROM pgr_trsp(
$$SELECT id, source, target, cost, reverse_cost FROM edge_table$$,
$$SELECT path, cost FROM restrictions$$,
$$SELECT * FROM (VALUES (2, 3), (2, 7), (2, 10), (7, 10)) AS combinations (source, target)$$
);
seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
-----+----------+-----------+---------+------+------+------+----------
1 |        1 |         2 |       3 |    2 |    4 |    1 |        0
2 |        2 |         2 |       3 |    5 |   10 |    1 |        1
3 |        3 |         2 |       3 |   10 |   12 |    1 |        2
4 |        4 |         2 |       3 |   11 |   13 |    1 |        3
5 |        5 |         2 |       3 |   12 |   15 |    1 |        4
6 |        6 |         2 |       3 |    9 |   16 |    1 |        5
7 |        7 |         2 |       3 |    4 |    3 |    1 |        6
8 |        8 |         2 |       3 |    3 |   -1 |    0 |        7
9 |        1 |         2 |       7 |    2 |    4 |    1 |        0
10 |        2 |         2 |       7 |    5 |   10 |    1 |        1
11 |        3 |         2 |       7 |   10 |   12 |    1 |        2
12 |        4 |         2 |       7 |   11 |   13 |    1 |        3
13 |        5 |         2 |       7 |   12 |   15 |    1 |        4
14 |        6 |         2 |       7 |    9 |    9 |    1 |        5
15 |        7 |         2 |       7 |    6 |    8 |    1 |        6
16 |        8 |         2 |       7 |    5 |    7 |    1 |        7
17 |        9 |         2 |       7 |    8 |    6 |    1 |        8
18 |       10 |         2 |       7 |    7 |   -1 |    0 |        9
19 |        1 |         2 |      10 |    2 |    4 |    1 |        0
20 |        2 |         2 |      10 |    5 |   10 |    1 |        1
21 |        3 |         2 |      10 |   10 |   -1 |    0 |        2
22 |        1 |         7 |      10 |    7 |    6 |    1 |        0
23 |        2 |         7 |      10 |    8 |    7 |    1 |        1
24 |        3 |         7 |      10 |    5 |   10 |  101 |        2
25 |        4 |         7 |      10 |   10 |   -1 |    0 |      103
(25 rows)



## Parameters¶

Parameter

Type

Default

Description

Edges SQL

TEXT

Edges SQL as described below

Restrictions SQL

TEXT

Restrictions 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.

directed

BOOLEAN

true

• When true Graph is considered Directed

• When false the graph is considered as Undirected.

## 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

### Restrictions SQL¶

Column

Type

Description

path

ARRAY[ ANY-INTEGER ]

Sequence of edge identifiers that form a path that is not allowed to be taken. - Empty arrays or NULL arrays are ignored. - Arrays that have a NULL element will raise an exception.

Cost

ANY-NUMERICAL

Cost of taking the forbidden path.

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

## Return Columns¶

Set of (seq, path_id, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost)

Column

Type

Description

seq

INTEGER

Sequential value starting from 1.

path_id

INTEGER

Path identifier.

• Has value 1 for the first of a path from start_vid to end_vid.

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. Returned when multiple starting vetrices are in the query.

end_vid

BIGINT

Identifier of the ending vertex. Returned when multiple ending vertices are in the query.

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.