pgr_johnson
- Returns the sum of the costs of the shortest path for each
pair of nodes in the graph using Floyd-Warshall algorithm.
Availability
Support
The Johnson algorithm, is a good choice to calculate the sum of the costs of the shortest path for each pair of nodes in the graph, for sparse graphs. It usees the Boost’s implementation which runs in \(O(V E \log V)\) time,
Summary
pgr_johnson(edges_sql)
pgr johnson(edges_sql [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Using default
pgr_johnson(edges_sql)
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example 1: | For vertices \(\{1, 2, 3, 4\}\) on a directed graph |
---|
SELECT * FROM pgr_johnson(
'SELECT source, target, cost FROM edge_table WHERE id < 5
ORDER BY id'
);
start_vid | end_vid | agg_cost
-----------+---------+----------
1 | 2 | 1
1 | 5 | 2
2 | 5 | 1
(3 rows)
pgr_johnson(edges_sql[, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Example 2: | For vertices \(\{1, 2, 3, 4\}\) on an undirected graph |
---|
SELECT * FROM pgr_johnson(
'SELECT source, target, cost FROM edge_table WHERE id < 5
ORDER BY id',
false
);
start_vid | end_vid | agg_cost
-----------+---------+----------
1 | 2 | 1
1 | 5 | 2
2 | 1 | 1
2 | 5 | 1
5 | 1 | 2
5 | 2 | 1
(6 rows)
Parameter | Type | Description |
---|---|---|
edges_sql | TEXT |
SQL query as described above. |
directed | BOOLEAN |
(optional) Default is true (is directed). When set to false the graph is considered as Undirected |
Description of the edges_sql query (id is not necessary)
edges_sql: | an SQL query, which should return a set of rows with the following columns: |
---|
Column | Type | Default | Description |
---|---|---|---|
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 set of (start_vid, end_vid, agg_cost)
Column | Type | Description |
---|---|---|
start_vid | BIGINT |
Identifier of the starting vertex. |
end_vid | BIGINT |
Identifier of the ending vertex. |
agg_cost | FLOAT |
Total cost from start_vid to end_vid . |
Indices and tables