pgr_binaryBreadthFirstSearch - Experimental

pgr_binaryBreadthFirstSearch - Returns the shortest path(s) in a binary graph. Any graph whose edge-weights belongs to the set {0,X}, where ‘X’ is any non-negative real integer, is termed as a ‘binary graph’.

images/boost-inside.jpeg

Boost Graph Inside

Warning

Possible server crash

  • These functions might create a server crash

Warning

Experimental functions

  • They are not officially of the current release.

  • They likely will not be officially be part of the next release:

    • The functions might not make use of ANY-INTEGER and ANY-NUMERICAL

    • Name might change.

    • Signature might change.

    • Functionality might change.

    • pgTap tests might be missing.

    • Might need c/c++ coding.

    • May lack documentation.

    • Documentation if any might need to be rewritten.

    • Documentation examples might need to be automatically generated.

    • Might need a lot of feedback from the comunity.

    • Might depend on a proposed function of pgRouting

    • Might depend on a deprecated function of pgRouting

Availability

  • To-be experimental on v3.0.0

Description

It is well-known that the shortest paths between a single source and all other vertices can be found using Breadth First Search in \(O(E)\) in an unweighted graph, i.e. the distance is the minimal number of edges that you need to traverse from the source to another vertex. We can interpret such a graph also as a weighted graph, where every edge has the weight 1. If not all edges in graph have the same weight, that we need a more general algorithm, like Dijkstra’s Algorithm which runs in \(O(ElogV)\) time.

However if the weights are more constrained, we can use a faster algorithm. This algorithm, termed as ‘Binary Breadth First Search’ as well as ‘0-1 BFS’, is a variation of the standard Breadth First Search problem to solve the SSSP (single-source shortest path) problem in \(O(E)\) , if the weights of each edge belongs to the set {0,X}, where ‘X’ is any non-negative real integer.

The main Characteristics are:

  • Process is done only on ‘binary graphs’. (‘Binary Graph’: Any graph whose edge-weights belongs to the set {0,X}, where ‘X’ is any non-negative real integer.)

  • For optimization purposes, any duplicated value in the start_vids or end_vids are ignored.

  • The returned values are ordered:

    • start_vid ascending

    • end_vid ascending

  • Running time: \(O( start\_vids * E)\)

Signatures

pgr_binaryBreadthFirstSearch(edges_sql, start_vid,  end_vid  [, directed])
pgr_binaryBreadthFirstSearch(edges_sql, start_vid,  end_vids [, directed])
pgr_binaryBreadthFirstSearch(edges_sql, start_vids, end_vid  [, directed])
pgr_binaryBreadthFirstSearch(edges_sql, start_vids, end_vids [, directed])
RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost)
OR EMPTY SET
pgr_binaryBreadthFirstSearch(TEXT edges_sql, BIGINT start_vid, BIGINT end_vid)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost) or EMPTY SET
Example :

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

SELECT * FROM pgr_binaryBreadthFirstSearch(
    'SELECT id, source, target, road_work as cost, reverse_road_work as reverse_cost FROM roadworks',
    2, 3
);
 seq  path_seq  node  edge  cost  agg_cost
-----+----------+------+------+------+----------
   1         1     2     4     0         0
   2         2     5     8     1         0
   3         3     6     9     1         1
   4         4     9    16     0         2
   5         5     4     3     0         2
   6         6     3    -1     0         2
(6 rows)

One to One

pgr_binaryBreadthFirstSearch(TEXT edges_sql, BIGINT start_vid, BIGINT end_vid,
BOOLEAN directed:=true);
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost)
OR EMPTY SET
Example :

From vertex \(2\) to vertex \(3\) on an undirected binary graph

SELECT * FROM pgr_binaryBreadthFirstSearch(
    'SELECT id, source, target, road_work as cost, reverse_road_work as reverse_cost FROM roadworks',
    2, 3,
    FALSE
);
 seq  path_seq  node  edge  cost  agg_cost
-----+----------+------+------+------+----------
   1         1     2     2     1         0
   2         2     3    -1     0         1
(2 rows)

One to many

pgr_binaryBreadthFirstSearch(TEXT edges_sql, BIGINT start_vid, ARRAY[ANY_INTEGER] end_vids,
BOOLEAN directed:=true);
RETURNS SET OF (seq, path_seq, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example :

From vertex \(2\) to vertices \(\{3, 5\}\) on an undirected binary graph

SELECT * FROM pgr_binaryBreadthFirstSearch(
    'SELECT id, source, target, road_work as cost FROM roadworks',
    2, ARRAY[3,5],
    FALSE
);
 seq  path_seq  end_vid  node  edge  cost  agg_cost
-----+----------+---------+------+------+------+----------
   1         1        3     2     4     0         0
   2         2        3     5     8     1         0
   3         3        3     6     5     1         1
   4         4        3     3    -1     0         2
   5         1        5     2     4     0         0
   6         2        5     5    -1     0         0
(6 rows)

Many to One

pgr_binaryBreadthFirstSearch(TEXT edges_sql, ARRAY[ANY_INTEGER] start_vids, BIGINT end_vid,
    BOOLEAN directed:=true);
RETURNS SET OF (seq, path_seq, start_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example :

From vertices \(\{2, 11\}\) to vertex \(5\) on a directed binary graph

SELECT * FROM pgr_binaryBreadthFirstSearch(
    'SELECT id, source, target, road_work as cost, reverse_road_work as reverse_cost FROM roadworks',
    ARRAY[2,11], 5
);
 seq  path_seq  start_vid  node  edge  cost  agg_cost
-----+----------+-----------+------+------+------+----------
   1         1          2     2     4     0         0
   2         2          2     5    -1     0         0
   3         1         11    11    13     1         0
   4         2         11    12    15     0         1
   5         3         11     9    16     0         1
   6         4         11     4     3     0         1
   7         5         11     3     2     1         1
   8         6         11     2     4     0         2
   9         7         11     5    -1     0         2
(9 rows)

Many to Many

pgr_binaryBreadthFirstSearch(TEXT edges_sql, ARRAY[ANY_INTEGER] start_vids, ARRAY[ANY_INTEGER] end_vids,
    BOOLEAN directed:=true);
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET
Example :

From vertices \(\{2, 11\}\) to vertices \(\{3, 5\}\) on an undirected binary graph

SELECT * FROM pgr_binaryBreadthFirstSearch(
    'SELECT id, source, target, road_work as cost, reverse_road_work as reverse_cost FROM roadworks',
    ARRAY[2,11], ARRAY[3,5],
    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        5     2     4     0         0
   4         2          2        5     5    -1     0         0
   5         1         11        3    11    13     1         0
   6         2         11        3    12    15     0         1
   7         3         11        3     9    16     0         1
   8         4         11        3     4     3     0         1
   9         5         11        3     3    -1     0         1
  10         1         11        5    11    12     0         0
  11         2         11        5    10    10     1         0
  12         3         11        5     5    -1     0         1
(12 rows)

Parameters

Parameter

Type

Default

Description

edges_sql

TEXT

Inner SQL query 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 query

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)

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

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

Return Columns

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

Column

Type

Description

seq

INT

Sequential value starting from 1 .

path_id

INT

Path identifier. Has value 1 for the first of a path. Used when there are multiple paths for the same start_vid to end_vid combination.

path_seq

INT

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_v to node .

Example Data

This type of data is used on the examples of this page.

Edwards-Moore Algorithm is best applied when trying to answer queries such as the following: "Find the path with the minimum number from Source to Destination" Here: * Source = Source Vertex, Destination = Any arbitrary destination vertex * X is an event/property * Each edge in the graph is either " X " or " Not X " .

Example: "Find the path with the minimum number of road works from Source to Destination"

Here, a road under work(aka road works ) means that part of the road is occupied for construction work/maintenance.

Here: * Edge ( u , v ) has weight = 0 if no road work is ongoing on the road from u to v . * Edge ( u , v ) has weight = 1 if road work is ongoing on the road from u to v .

Then, upon running the algorithm, we obtain the path with the minimum number of road works from the given source and destination.

Thus, the queries used in the previous section can be interpreted in this manner.

Table Data

The queries in the previous sections use the table ‘roadworks’. The data of the table:

DROP TABLE IF EXISTS roadworks CASCADE;
NOTICE:  table "roadworks" does not exist, skipping
DROP TABLE
CREATE table roadworks (
    id BIGINT not null primary key,
    source BIGINT,
    target BIGINT,
    road_work FLOAT,
    reverse_road_work FLOAT
);
CREATE TABLE
INSERT INTO roadworks(
  id, source, target, road_work, reverse_road_work) VALUES
  (1,  1,  2,  0,  0),
  (2,  2,  3,  -1,  1),
  (3,  3,  4,  -1,  0),
  (4,  2,  5,  0,  0),
  (5,  3,  6,  1,  -1),
  (6,  7,  8,  1,  1),
  (7,  8,  5,  0,  0),
  (8,  5,  6,  1,  1),
  (9,  6,  9,  1,  1),
  (10,  5,  10,  1,  1),
  (11,  6,  11,  1,  -1),
  (12,  10,  11,  0,  -1),
  (13,  11,  12,  1,  -1),
  (14,  10,  13,  1,  1),
  (15,  9,  12,  0,  0),
  (16,  4,  9,  0,  0),
  (17,  14,  15,  0,  0),
  (18,  16,  17,  0,  0);
INSERT 0 18

See Also

Indices and tables