pgr_bdDijkstraCost

pgr_bdDijkstraCost — Returns the shortest path(s)’s cost using Bidirectional Dijkstra algorithm.

_images/boost-inside.jpeg

Boost Graph Inside

Availability

  • Version 3.2.0

  • Version 3.0.0

    • Official function

  • Version 2.5.0

    • New proposed function

Description

The pgr_bdDijkstraCost function sumarizes of the cost of the shortest path(s) using the bidirectional Dijkstra Algorithm.

  • Process is done only on edges with positive costs.

    • A negative value on a cost column is interpreted as the edge does not exist.

  • Values are returned when there is a path.

  • When there is no path:

    • When the starting vertex and ending vertex are the same.

      • The aggregate cost of the non included values \((v, v)\) is \(0\)

    • When the starting vertex and ending vertex are the different and there is no path:

      • The aggregate cost the non included values \((u, v)\) is \(\infty\)

  • For optimization purposes, any duplicated value in the starting vertices or on the ending vertices are ignored.

  • Running time (worse case scenario): \(O((V \log V + E))\)

  • For large graphs where there is a path bewtween the starting vertex and ending vertex:

    • It is expected to terminate faster than pgr_dijkstra

  • It does not return a path.

  • Returns the sum of the costs of the shortest path of each pair combination of nodes requested.

  • Let be the case the values returned are stored in a table, so the unique index would be the pair: (start_vid, end_vid).

  • Depending on the function and its parameters, the results can be symmetric.

    • The aggregate cost of \((u, v)\) is the same as for \((v, u)\).

  • Any duplicated value in the start or end vertex identifiers are ignored.

  • The returned values are ordered:

    • start_vid ascending

    • end_vid ascending

Signatures

Summary

pgr_bdDijkstraCost(Edges SQL, start vid, end vid  [, directed])
pgr_bdDijkstraCost(Edges SQL, start vid, end vids [, directed])
pgr_bdDijkstraCost(Edges SQL, start vids, end vid  [, directed])
pgr_bdDijkstraCost(Edges SQL, start vids, end vids [, directed])
pgr_bdDijkstraCost(Edges SQL, Combinations SQL [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET

One to One

pgr_bdDijkstraCost(Edges SQL, start vid, end vid  [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example

From vertex \(2\) to vertex \(3\) on a directed graph

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  2, 3, true);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         2 |       3 |        5
(1 row)

One to Many

pgr_bdDijkstraCost(Edges SQL, start vid, end vids [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example

From vertex \(2\) to vertices \(\{3, 12\}\) on a directed graph

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  2, ARRAY[3, 12]);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         2 |       3 |        5
         2 |      12 |        4
(2 rows)

Many to One

pgr_bdDijkstraCost(Edges SQL, start vids, end vid  [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example

From vertices \(\{2, 7\}\) to vertex \(12\) on a directed graph

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  ARRAY[2, 7], 12);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         2 |      12 |        4
         7 |      12 |        5
(2 rows)

Many to Many

pgr_bdDijkstraCost(Edges SQL, start vids, end vids [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example

From vertices \(\{2, 7\}\) to vertices \(\{3, 12\}\) on an undirected graph

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  ARRAY[2, 7], ARRAY[3, 12],
  directed => false);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         2 |       3 |        1
         2 |      12 |        4
         7 |       3 |        4
         7 |      12 |        5
(4 rows)

Combinations

pgr_bdDijkstraCost(Edges SQL, Combinations SQL [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example

Using a combinations table on an undirected graph

The combinations table:

SELECT source, target FROM combinations_table;
 source | target
--------+--------
      1 |      2
      1 |      3
      2 |      1
      2 |      4
      2 |     17
(5 rows)

The query:

SELECT * FROM pgr_bdDijkstraCost(
  'SELECT id, source, target, cost, reverse_cost FROM edge_table',
  'SELECT source, target FROM combinations_table',
  false);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         1 |       2 |        1
         1 |       3 |        2
         2 |       1 |        1
         2 |       4 |        2
(4 rows)

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.

Optional parameters

Column

Type

default

Description

directed

BOOLEAN

true

  • When true the 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

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 (start_vid, end_vid, agg_cost)

Column

Type

Description

start_vid

BIGINT

Identifier of the starting vertex.

end_vid

BIGINT

Identifier of the ending vertex.

agg_cost

FLOAT

Aggregate cost from start_vid to end_vid.

Additional Examples

Example 1

Demonstration of repeated values are ignored, and result is sorted.

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  ARRAY[5, 3, 4, 3, 3, 4], ARRAY[3, 5, 3, 4]);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         3 |       4 |        3
         3 |       5 |        2
         4 |       3 |        1
         4 |       5 |        3
         5 |       3 |        4
         5 |       4 |        3
(6 rows)

Example 2

Making start_vids the same as end_vids.

SELECT * FROM pgr_bdDijkstraCost(
  'select id, source, target, cost, reverse_cost from edge_table',
  ARRAY[5, 3, 4], ARRAY[5, 3, 4]);
 start_vid | end_vid | agg_cost
-----------+---------+----------
         3 |       4 |        3
         3 |       5 |        2
         4 |       3 |        1
         4 |       5 |        3
         5 |       3 |        4
         5 |       4 |        3
(6 rows)

Example 3

Manually assigned vertex combinations.

SELECT * FROM pgr_bdDijkstraCost(
  'SELECT id, source, target, cost, reverse_cost FROM edge_table',
  'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3)) AS combinations (source, target)');
 start_vid | end_vid | agg_cost
-----------+---------+----------
         2 |       3 |        5
         2 |       5 |        1
        11 |       3 |        4
(3 rows)

See Also

Indices and tables