pgr_depthFirstSearch - Experimental

pgr_depthFirstSearch — Returns a depth first search traversal of the graph. The graph can be directed or undirected.

_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

  • Version 3.2.0

    • New experimental function

Description

Depth First Search algorithm is a traversal algorithm which starts from a root vertex, goes as deep as possible, and backtracks once a vertex is reached with no adjacent vertices or with all visited adjacent vertices. The traversal continues until all the vertices reachable from the root vertex are visited.

The main Characteristics are:

  • The implementation works for both directed and undirected graphs.

  • Provides the Depth First Search traversal order from a root vertex or from a set of root vertices.

  • An optional non-negative maximum depth parameter to limit the results up to a particular depth.

  • For optimization purposes, any duplicated values in the Root vids are ignored.

  • It does not produce the shortest path from a root vertex to a target vertex.

  • The aggregate cost of traversal is not guaranteed to be minimal.

  • The returned values are ordered in ascending order of start_vid.

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

Signatures

Summary

pgr_depthFirstSearch(Edges SQL, Root vid [, directed] [, max_depth]) -- Experimental on v3.2
pgr_depthFirstSearch(Edges SQL, Root vids [, directed] [, max_depth]) -- Experimental on v3.2

RETURNS SET OF (seq, depth, start_vid, node, edge, cost, agg_cost)

Using defaults

Example

From root vertex \(2\) on a directed graph

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

Single vertex

pgr_depthFirstSearch(Edges SQL, Root vid [, directed] [, max_depth])

RETURNS SET OF (seq, depth, start_vid, node, edge, cost, agg_cost)
Example

From root vertex \(2\) on an undirected graph, with \(depth <= 2\)

SELECT * FROM pgr_depthFirstSearch(
    'SELECT id, source, target, cost, reverse_cost FROM edge_table
    ORDER BY id',
    2, directed => false, max_depth => 2
);
 seq | depth | start_vid | node | edge | cost | agg_cost
-----+-------+-----------+------+------+------+----------
   1 |     0 |         2 |    2 |   -1 |    0 |        0
   2 |     1 |         2 |    1 |    1 |    1 |        1
   3 |     1 |         2 |    3 |    2 |    1 |        1
   4 |     2 |         2 |    4 |    3 |    1 |        2
   5 |     2 |         2 |    6 |    5 |    1 |        2
   6 |     1 |         2 |    5 |    4 |    1 |        1
   7 |     2 |         2 |    8 |    7 |    1 |        2
   8 |     2 |         2 |   10 |   10 |    1 |        2
(8 rows)

Multiple vertices

pgr_depthFirstSearch(Edges SQL, Root vids [, directed] [, max_depth])

RETURNS SET OF (seq, depth, start_vid, node, edge, cost, agg_cost)
Example

From root vertices \(\{11, 2\}\) on an undirected graph with \(depth <= 2\)

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

Parameters

Parameter

Type

Description

Edges SQL

TEXT

SQL query described in Inner query.

Root vid

BIGINT

Identifier of the root vertex of the tree.

Root vids

ARRAY[ANY-INTEGER]

Array of identifiers of the root vertices.

  • Used on Multiple Vertices.

  • For optimization purposes, any duplicated value is ignored.

Optional Parameters

Parameter

Type

Default

Description

directed

BOOLEAN

true

  • When true Graph is Directed

  • When false the graph is Undirected.

max_depth

BIGINT

\(9223372036854775807\)

Upper limit for the depth of traversal

  • When value is Negative then throws error

Inner query

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

  • When positive: edge (source, target) exist on the graph.

  • When negative: edge (source, target) does not exist on the graph.

reverse_cost

ANY-NUMERICAL

-1

  • When positive: edge (target, source) exist on the graph.

  • When negative: edge (target, source) does not exist on the graph.

Where:

ANY-INTEGER

SMALLINT, INTEGER, BIGINT

ANY-NUMERICAL

SMALLINT, INTEGER, BIGINT, REAL, FLOAT

Result Columns

Returns SET OF (seq, depth, start_vid, node, edge, cost, agg_cost)

Column

Type

Description

seq

BIGINT

Sequential value starting from \(1\).

depth

BIGINT

Depth of the node.

  • \(0\) when node = start_vid.

start_vid

BIGINT

Identifier of the root vertex.

node

BIGINT

Identifier of node reached using edge.

edge

BIGINT

Identifier of the edge used to arrive to node.

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

cost

FLOAT

Cost to traverse edge.

agg_cost

FLOAT

Aggregate cost from start_vid to node.

Additional Examples

The examples of this section are based on the Sample Data network.

Example: No internal ordering on traversal

In the following query, the inner query of the example: “Using defaults” is modified so that the data is entered into the algorithm is given in the reverse ordering of the id.

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

The resulting traversal is different.

The left image shows the result with ascending order of ids and the right image shows with descending order of ids:

ascending descending

See Also

Indices and tables