pgr_kruskalDFS
¶
pgr_kruskalDFS
— Kruskal’s algorithm for Minimum Spanning Tree with Depth
First Search ordering.
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 Depth First Search ordering of the Minimum Spanning Tree created using Kruskal’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.
The total weight of all the edges in the tree or forest is minimized.
Kruskal’s running time: \(O(E * log E)\)
Returned tree nodes from a root vertex are on Depth First Search order
Depth First Search Running time: \(O(E + V)\)
Signatures¶
(seq, depth, start_vid, pred, node, edge, cost, agg_cost)
Single vertex¶
max_depth
])(seq, depth, start_vid, pred, node, edge, cost, agg_cost)
- Example:
The Minimum Spanning Tree having as root vertex \(6\)
SELECT * FROM pgr_kruskalDFS(
'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 | 2 | 6 | 10 | 15 | 3 | 1 | 2
5 | 3 | 6 | 15 | 16 | 16 | 1 | 3
6 | 4 | 6 | 16 | 17 | 15 | 1 | 4
7 | 5 | 6 | 17 | 12 | 13 | 1 | 5
8 | 6 | 6 | 12 | 11 | 11 | 1 | 6
9 | 6 | 6 | 12 | 8 | 12 | 1 | 6
10 | 7 | 6 | 8 | 7 | 10 | 1 | 7
11 | 8 | 6 | 7 | 3 | 7 | 1 | 8
12 | 9 | 6 | 3 | 1 | 6 | 1 | 9
13 | 7 | 6 | 8 | 9 | 14 | 1 | 7
(13 rows)
Multiple vertices¶
max_depth
])(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_kruskalDFS(
'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 | 2 | 6 | 10 | 15 | 3 | 1 | 2
5 | 3 | 6 | 15 | 16 | 16 | 1 | 3
6 | 0 | 9 | 9 | 9 | -1 | 0 | 0
7 | 1 | 9 | 9 | 8 | 14 | 1 | 1
8 | 2 | 9 | 8 | 7 | 10 | 1 | 2
9 | 3 | 9 | 7 | 3 | 7 | 1 | 3
10 | 2 | 9 | 8 | 12 | 12 | 1 | 2
11 | 3 | 9 | 12 | 11 | 11 | 1 | 3
12 | 3 | 9 | 12 | 17 | 13 | 1 | 3
(12 rows)
Parameters¶
Parameter |
Type |
Description |
---|---|---|
|
Edges SQL as described below. |
|
Root vid |
|
Identifier of the root vertex of the tree. |
Root vids |
|
Array of identifiers of the root vertices.
|
distance |
|
Upper limit for the inclusion of a node in the result. |
Where:
- ANY-NUMERIC:
SMALLINT
,INTEGER
,BIGINT
,REAL
,FLOAT
DFS optional parameters¶
Parameter |
Type |
Default |
Description |
---|---|---|---|
|
|
\(9223372036854775807\) |
Upper limit of the depth of the tree.
|
Inner Queries¶
Edges SQL¶
Column |
Type |
Default |
Description |
---|---|---|---|
|
ANY-INTEGER |
Identifier of the edge. |
|
|
ANY-INTEGER |
Identifier of the first end point vertex of the edge. |
|
|
ANY-INTEGER |
Identifier of the second end point vertex of the edge. |
|
|
ANY-NUMERICAL |
Weight of the edge ( |
|
|
ANY-NUMERICAL |
-1 |
Weight of the edge (
|
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 |
---|---|---|
|
|
Sequential value starting from \(1\). |
|
|
Depth of the
|
|
|
Identifier of the root vertex. |
|
|
Predecessor of
|
|
|
Identifier of |
|
|
Identifier of the
|
|
|
Cost to traverse |
|
|
Aggregate cost from |
See Also¶
Indices and tables