pgr_maxFlow

pgr_maxFlow — Calculates the maximum flow in a directed graph from the source(s) to the targets(s) using the Push Relabel algorithm.

_images/boost-inside.jpeg

Boost Graph Inside

Availability

  • Version 3.2.0

  • Version 3.0.0

    • Official function

  • Version 2.4.0

    • New Proposed function

Description

The main characteristics are:

  • The graph is directed.

  • Calculates the maximum flow from the source(s) to the target(s).

    • When the maximum flow is 0 then there is no flow and 0 is returned.

    • There is no flow when a source is the same as a target.

  • Any duplicated value in the source(s) or target(s) are ignored.

  • Uses the pgr_pushRelabel algorithm.

  • Running time: \(O( V ^ 3)\)

Signatures

Summary

pgr_maxFlow(Edges SQL, start vid, end vid)
pgr_maxFlow(Edges SQL, start vid, end vids)
pgr_maxFlow(Edges SQL, start vids, end vid)
pgr_maxFlow(Edges SQL, start vids, end vids)
pgr_maxFlow(Edges SQL, Combinations SQL)
RETURNS BIGINT

One to One

pgr_maxFlow(Edges SQL, start vid, end vid)
RETURNS BIGINT
Example

From vertex \(6\) to vertex \(11\)

SELECT * FROM pgr_maxFlow(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  6, 11);
 pgr_maxflow
-------------
         230
(1 row)

One to Many

pgr_maxFlow(Edges SQL, start vid, end vids)
RETURNS BIGINT
Example

From vertex \(6\) to vertices \(\{1, 3, 11\}\)

SELECT * FROM pgr_maxFlow(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  6, ARRAY[1, 3, 11]);
 pgr_maxflow
-------------
         340
(1 row)

Many to One

pgr_maxFlow(Edges SQL, start vids, end vid)
RETURNS BIGINT
Example

From vertices \(\{6, 8, 12\}\) to vertex \(11\)

SELECT * FROM pgr_maxFlow(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  ARRAY[6, 8, 12], 11);
 pgr_maxflow
-------------
         230
(1 row)

Many to Many

pgr_maxFlow(Edges SQL, start vids, end vids)
RETURNS BIGINT
Example

From vertices \(\{6, 8, 12\}\) to vertices \(\{1, 3, 11\}\)

SELECT * FROM pgr_maxFlow(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  ARRAY[6, 8, 12], ARRAY[1, 3, 11]);
 pgr_maxflow
-------------
         360
(1 row)

Combinations

pgr_maxFlow(Edges SQL, Combinations SQL)
RETURNS BIGINT
Example

Using a combinations table, equivalent to calculating result from vertices \(\{1, 2\}\) to vertices \(\{3, 4, 17\}\).

The combinations table:

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

The query:

SELECT * FROM pgr_maxFlow(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  'SELECT * FROM combinations_table
   WHERE target NOT IN (1, 2)');
 pgr_maxflow
-------------
          80
(1 row)

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.

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.

capacity

ANY-INTEGER

Weight of the edge (source, target)

reverse_capacity

ANY-INTEGER

-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

Type

Description

BIGINT

Maximum flow possible from the source(s) to the target(s)

Additional Examples

Example

Manually assigned vertex combinations.

SELECT * FROM pgr_pushRelabel(
  'SELECT id, source, target, capacity, reverse_capacity
  FROM edge_table',
  'SELECT * FROM (VALUES (1, 3), (2, 4), (2, 17)) AS t(source, target)');
 seq | edge | start_vid | end_vid | flow | residual_capacity
-----+------+-----------+---------+------+-------------------
   1 |    4 |         2 |       5 |   80 |                20
   2 |    8 |         5 |       6 |   80 |                20
   3 |    9 |         6 |       9 |   80 |                50
   4 |   16 |         9 |       4 |   80 |                 0
(4 rows)

See Also

Indices and tables