pgr_transitiveClosure
— Returns the transitive closure graph of the input graph.
In particular, the transitive closure algorithm implemented by Boost.Graph.
Warning
Possible server crash
Warning
Experimental functions
Availability
Support
The transitive_closure() function transforms the input graph g into the transitive closure graph tc.
This implementation can only be used with a directed graph with no cycles i.e. directed acyclic graph.
Summary
The pgr_transitiveClosure function has the following signature:
pgr_transitiveClosure(Edges SQL)
RETURNS SETOF (id, vid, target_array)
Example: | Complete Graph of 3 vertexs |
---|
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table1'
);
seq | vid | target_array
-----+-----+--------------
1 | 0 | {1,3,2}
2 | 1 | {3,2}
3 | 3 | {2}
4 | 2 | {}
(4 rows)
Column | Type | Description |
---|---|---|
Edges SQL | TEXT |
SQL query as described in 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)
|
|
reverse_cost | ANY-NUMERICAL |
-1 | Weight of the edge (target, source),
|
Where:
ANY-INTEGER: | SMALLINT, INTEGER, BIGINT |
---|---|
ANY-NUMERICAL: | SMALLINT, INTEGER, BIGINT, REAL, FLOAT |
RETURNS SETOF (seq, vid, target_array)
The function returns a single row. The columns of the row are:
Column | Type | Description |
---|---|---|
seq | INTEGER |
Sequential value starting from 1. |
vid | BIGINT |
Identifier of the vertex. |
target_array | ARRAY[BIGINT] |
Array of identifiers of the vertices that are reachable from vertex v. |
Example: | Some sub graphs of the sample data |
---|
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2'
);
seq | vid | target_array
-----+-----+--------------
1 | 2 | {}
2 | 3 | {2}
(2 rows)
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=3'
);
seq | vid | target_array
-----+-----+--------------
1 | 3 | {}
2 | 4 | {3}
(2 rows)
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2 or id=3'
);
seq | vid | target_array
-----+-----+--------------
1 | 2 | {}
2 | 3 | {2}
3 | 4 | {3,2}
(3 rows)
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=11'
);
seq | vid | target_array
-----+-----+--------------
1 | 6 | {11}
2 | 11 | {}
(2 rows)
-- q3
SELECT * FROM pgr_transitiveclosure(
'SELECT id,source,target,cost,reverse_cost FROM edge_table where cost=-1 or reverse_cost=-1'
);
seq | vid | target_array
-----+-----+---------------
1 | 2 | {}
2 | 3 | {11,12,6,2}
3 | 4 | {11,12,3,6,2}
4 | 6 | {11,12}
5 | 11 | {12}
6 | 10 | {11,12}
7 | 12 | {}
(7 rows)
Indices and tables