pgr_primBFS

pgr_primBFS - Prim’s algorithm for Minimum Spanning Tree with Depth First Search ordering.

images/boost-inside.jpeg

Boost Graph Inside

Availability

Version 3.7.0 :

  • Standarizing output columns to (seq, depth, start_vid, pred, node, edge, cost, agg_cost)

    • Added pred result columns.

Version 3.0.0 :
  • New Official function

Description

Visits and extracts the nodes information in Breath First Search ordering of the Minimum Spanning Tree created using Prims’s algorithm.

The main Characteristics are:

  • It’s implementation is only on undirected graph.

  • Process is done only on edges with positive costs.

  • When the graph is connected

    • The resulting edges make up a tree

  • When the graph is not connected,

    • Finds a minimum spanning tree for each connected component.

    • The resulting edges make up a forest.

  • Prim’s running time: \(O(E * log V)\)

  • Returned tree nodes from a root vertex are on Breath First Search order

  • Breath First Search Running time: \(O(E + V)\)

Signatures

pgr_primBFS( Edges SQL , root vid , [ max_depth ])
pgr_primBFS( Edges SQL , root vids , [ max_depth ])
Returns set of (seq, depth, start_vid, pred, node, edge, cost, agg_cost)

Single vertex

pgr_primBFS( Edges SQL , root vid , [ max_depth ])
Returns set of (seq, depth, start_vid, pred, node, edge, cost, agg_cost)
Example :

The Minimum Spanning Tree having as root vertex \(6\)

SELECT * FROM pgr_primBFS(
  'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
  6);
 seq  depth  start_vid  pred  node  edge  cost  agg_cost
-----+-------+-----------+------+------+------+------+----------
   1      0          6     6     6    -1     0         0
   2      1          6     6     5     1     1         1
   3      1          6     6    10     2     1         1
   4      1          6     6     7     4     1         1
   5      2          6    10    15     3     1         2
   6      2          6    10    11     5     1         2
   7      2          6     7     3     7     1         2
   8      2          6     7     8    10     1         2
   9      3          6    11    16     9     1         3
  10      3          6    11    12    11     1         3
  11      3          6     3     1     6     1         3
  12      3          6     8     9    14     1         3
  13      4          6    12    17    13     1         4
(13 rows)

Multiple vertices

pgr_primBFS( Edges SQL , root vids , [ max_depth ])
Returns set of (seq, depth, start_vid, pred, node, edge, cost, agg_cost)
Example :

The Minimum Spanning Tree starting on vertices \(\{9, 6\}\) with \(depth \leq 3\)

SELECT * FROM pgr_primBFS(
  'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
  ARRAY[9, 6], max_depth => 3);
 seq  depth  start_vid  pred  node  edge  cost  agg_cost
-----+-------+-----------+------+------+------+------+----------
   1      0          6     6     6    -1     0         0
   2      1          6     6     5     1     1         1
   3      1          6     6    10     2     1         1
   4      1          6     6     7     4     1         1
   5      2          6    10    15     3     1         2
   6      2          6    10    11     5     1         2
   7      2          6     7     3     7     1         2
   8      2          6     7     8    10     1         2
   9      3          6    11    16     9     1         3
  10      3          6    11    12    11     1         3
  11      3          6     3     1     6     1         3
  12      3          6     8     9    14     1         3
  13      0          9     9     9    -1     0         0
  14      1          9     9     8    14     1         1
  15      2          9     8     7    10     1         2
  16      3          9     7     6     4     1         3
  17      3          9     7     3     7     1         3
(17 rows)

Parameters

Parameter

Type

Description

Edges SQL

TEXT

Edges SQL as described below.

Root vid

BIGINT

Identifier of the root vertex of the tree.

Root vids

ARRAY[ANY-INTEGER]

Array of identifiers of the root vertices.

  • \(0\) values are ignored

  • For optimization purposes, any duplicated value is ignored.

distance

FLOAT

Upper limit for the inclusion of a node in the result.

Where:

ANY-NUMERIC :

SMALLINT , INTEGER , BIGINT , REAL , FLOAT

BFS optional parameters

Parameter

Type

Default

Description

max_depth

BIGINT

\(9223372036854775807\)

Upper limit of the depth of the tree.

  • When negative throws an error.

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

Result columns

Returns set of (seq, depth, start_vid, pred, node, edge, cost, agg_cost)

Parameter

Type

Description

seq

BIGINT

Sequential value starting from \(1\) .

depth

BIGINT

Depth of the node .

  • \(0\) when node = start_vid .

  • \(depth-1\) is the depth of pred

start_vid

BIGINT

Identifier of the root vertex.

pred

BIGINT

Predecessor of node .

  • When node = start_vid then has the value node .

node

BIGINT

Identifier of node reached using edge .

edge

BIGINT

Identifier of the edge used to arrive from pred to node .

  • \(-1\) when node = start_vid .

cost

FLOAT

Cost to traverse edge .

agg_cost

FLOAT

Aggregate cost from start_vid to node .

See Also

Indices and tables