Skip to content

Commit 9af4a5f

Browse files
authored
Merge pull request #1054 from apslight/PriMst
[WIP]Documentation of pgr_kruskal
2 parents 4bef0f1 + a65e7b5 commit 9af4a5f

29 files changed

+289
-81
lines changed

configuration.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ vrppdtw | N | Y | Y
3131
withPoints | Y | Y | Y
3232
lineGraph | Y | Y | Y
3333
components | Y | Y | Y
34-
prim | Y | Y | Y
34+
mst | Y | Y | Y
3535
#----------------------
3636
# SQL only directories
3737
#----------------------

doc/prim/CMakeLists.txt renamed to doc/mst/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
SET(LOCAL_FILES
3+
doc-pgr_kruskal.queries
4+
pgr_kruskal.rst
35
doc-pgr_prim.queries
46
pgr_prim.rst
57
)

doc/mst/doc-pgr_kruskal.queries

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
BEGIN;
2+
BEGIN
3+
-- q1
4+
SELECT * FROM pgr_kruskal(
5+
'SELECT id, source, target, cost, reverse_cost
6+
FROM edge_table'
7+
);
8+
seq | component | edge | cost | tree_cost
9+
-----+-----------+------+------+-----------
10+
1 | 1 | 1 | 1 | 1
11+
2 | 1 | 3 | 1 | 2
12+
3 | 1 | 7 | 1 | 3
13+
4 | 1 | 15 | 1 | 4
14+
5 | 1 | 14 | 1 | 5
15+
6 | 1 | 16 | 1 | 6
16+
7 | 1 | 13 | 1 | 7
17+
8 | 1 | 12 | 1 | 8
18+
9 | 1 | 11 | 1 | 9
19+
10 | 1 | 10 | 1 | 10
20+
11 | 1 | 6 | 1 | 11
21+
12 | 1 | 2 | 1 | 12
22+
13 | 14 | 17 | 1 | 1
23+
14 | 16 | 18 | 1 | 1
24+
(14 rows)
25+
26+
-- q2
27+
SELECT * FROM pgr_kruskal(
28+
'SELECT id, source, target, cost, reverse_cost
29+
FROM edge_table WHERE id > 10'
30+
);
31+
seq | component | edge | cost | tree_cost
32+
-----+-----------+------+------+-----------
33+
1 | 4 | 11 | 1 | 1
34+
2 | 4 | 13 | 1 | 2
35+
3 | 4 | 16 | 1 | 3
36+
4 | 4 | 15 | 1 | 4
37+
5 | 4 | 12 | 1 | 5
38+
6 | 4 | 14 | 1 | 6
39+
7 | 14 | 17 | 1 | 1
40+
8 | 16 | 18 | 1 | 1
41+
(8 rows)
42+
43+
-- q3
44+
ROLLBACK;
45+
ROLLBACK
File renamed without changes.

doc/mst/pgr_kruskal.rst

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
..
2+
****************************************************************************
3+
pgRouting Manual
4+
Copyright(c) pgRouting Contributors
5+
6+
This documentation is licensed under a Creative Commons Attribution-Share
7+
Alike 3.0 License: http://creativecommons.org/licenses/by-sa/3.0/
8+
****************************************************************************
9+
10+
.. _pgr_kruskal:
11+
12+
pgr_kruskal
13+
===============================================================================
14+
15+
``pgr_kruskal`` — Returns the minimum spanning tree of graph using Kruskal algorithm.
16+
In particular, the Kruskal algorithm implemented by Boost.Graph.
17+
18+
.. figure:: images/boost-inside.jpeg
19+
:target: https://www.boost.org/doc/libs/1_64_0/libs/graph/doc/kruskal_min_spanning_tree.html
20+
21+
Boost Graph Inside
22+
23+
Synopsis
24+
-------------------------------------------------------------------------------
25+
26+
Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge
27+
of the least possible weight that connects any two trees in the forest. It is
28+
a greedy algorithm in graph theory as it finds a minimum spanning tree for a
29+
connected weighted undirected graph adding increasing cost arcs at each step.
30+
This means it finds a subset of the edges that forms a tree that includes every
31+
vertex, where the total weight of all the edges in the tree is minimized. If the
32+
graph is not connected, then it finds a minimum spanning forest (a minimum
33+
spanning tree for each connected component).
34+
35+
Characteristics
36+
-------------------------------------------------------------------------------
37+
38+
The main Characteristics are:
39+
40+
- Process is done only on edges with positive costs.
41+
42+
- It's implementation is only on undirected graph.
43+
44+
- Edges are in ascending order by weight for each component.
45+
46+
- Values are returned when there is a minimum spanning tree.
47+
48+
- When there is no edge in graph then EMPTY SET is return.
49+
50+
- Running time: :math:`O(E*log E)`
51+
52+
53+
Signatures
54+
-------------------------------------------------------------------------------
55+
56+
.. index::
57+
single: kruskal
58+
59+
.. code-block:: none
60+
61+
pgr_kruskal(edges_sql)
62+
63+
RETURNS SET OF (seq, component, edge, cost, tree_cost)
64+
or EMPTY SET
65+
66+
The signature is for a **undirected** graph.
67+
68+
:Example:
69+
70+
.. code-block:: none
71+
72+
pgr_kruskal(TEXT edges_sql);
73+
RETURNS SET OF (seq, component, edge, cost, tree_cost) or EMPTY SET
74+
75+
.. literalinclude:: doc-pgr_kruskal.queries
76+
:start-after: -- q1
77+
:end-before: -- q2
78+
79+
Additional example:
80+
81+
.. literalinclude:: doc-pgr_kruskal.queries
82+
:start-after: -- q2
83+
:end-before: -- q3
84+
85+
Description of the edges_sql query for kruskal functions
86+
...............................................................................
87+
88+
:edges_sql: an SQL query, which should return a set of rows with the following columns:
89+
90+
================= =================== ======== =================================================
91+
Column Type Default Description
92+
================= =================== ======== =================================================
93+
**id** ``ANY-INTEGER`` Identifier of the edge.
94+
**source** ``ANY-INTEGER`` Identifier of the first end point vertex of the edge.
95+
**target** ``ANY-INTEGER`` Identifier of the second end point vertex of the edge.
96+
**cost** ``ANY-NUMERICAL`` Weight of the edge `(source, target)`
97+
98+
- When negative: edge `(source, target)` does not exist, therefore it's not part of the graph.
99+
100+
**reverse_cost** ``ANY-NUMERICAL`` -1 Weight of the edge `(target, source)`,
101+
102+
- When negative: edge `(target, source)` does not exist, therefore it's not part of the graph.
103+
104+
================= =================== ======== =================================================
105+
106+
Where:
107+
108+
:ANY-INTEGER: SMALLINT, INTEGER, BIGINT
109+
:ANY-NUMERICAL: SMALLINT, INTEGER, BIGINT, REAL, FLOAT
110+
111+
112+
Description of the parameters of the signatures
113+
...............................................................................
114+
115+
=================== ====================== ========= =================================================
116+
Parameter Type Default Description
117+
=================== ====================== ========= =================================================
118+
**edges_sql** ``TEXT`` SQL query as described above.
119+
=================== ====================== ========= =================================================
120+
121+
Description of the return values for kruskal algorithms
122+
.............................................................................................................................
123+
124+
Returns set of ``(seq, component, edge, cost, tree_cost)``
125+
126+
=============== =========== ====================================================
127+
Column Type Description
128+
=============== =========== ====================================================
129+
**seq** ``INT`` Sequential value starting from **1**.
130+
**component** ``BIGINT`` Lowest number of the node in the component.
131+
**edge** ``BIGINT`` Identifier of the edge of lightest weight.
132+
**cost** ``FLOAT`` Cost to traverse of edge.
133+
**tree_cost** ``FLOAT`` Aggregate cost of edges that is covered in spanning.
134+
=============== =========== ====================================================
135+
136+
See Also
137+
-------------------------------------------------------------------------------
138+
139+
* https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
140+
* The queries use the :doc:`sampledata` network.
141+
142+
.. rubric:: Indices and tables
143+
144+
* :ref:`genindex`
145+
* :ref:`search`
146+
File renamed without changes.

include/c_types/pgr_kruskal_t.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*PGR-GNU*****************************************************************
22
File: pgr_kruskal_t.h
3+
34
Copyright (c) 2015 Aditya Pratap Singh
45
Mail: adityapratap.singh28@gmail.com
56
------
@@ -51,7 +52,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5152

5253
typedef struct {
5354
int seq;
54-
int sub_graph;
55+
int64_t component;
5556
int64_t edge;
5657
double cost;
5758
double tree_cost;

include/c_types/pgr_prim_t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*PGR-GNU*****************************************************************
22
File: pgr_prim_t.h
3+
34
Copyright (c) 2015 Aditya Pratap Singh
45
Mail: adityapratap.singh28@gmail.com
56
------

include/drivers/prim/kruskal_driver.h renamed to include/drivers/mst/kruskal_driver.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
2828
********************************************************************PGR-GNU*/
2929

30-
#ifndef INCLUDE_DRIVERS_PRIM_KRUSKAL_DRIVER_H_
31-
#define INCLUDE_DRIVERS_PRIM_KRUSKAL_DRIVER_H_
30+
#ifndef INCLUDE_DRIVERS_MST_KRUSKAL_DRIVER_H_
31+
#define INCLUDE_DRIVERS_MST_KRUSKAL_DRIVER_H_
3232
#pragma once
3333

3434
#include "c_types/pgr_edge_t.h"
@@ -52,4 +52,4 @@ extern "C" {
5252
}
5353
#endif
5454

55-
#endif // INCLUDE_DRIVERS_PRIM_KRUSKAL_DRIVER_H_
55+
#endif // INCLUDE_DRIVERS_MST_KRUSKAL_DRIVER_H_

include/drivers/prim/prim_driver.h renamed to include/drivers/mst/prim_driver.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2727
2828
********************************************************************PGR-GNU*/
2929

30-
#ifndef INCLUDE_DRIVERS_PRIM_PRIM_DRIVER_H_
31-
#define INCLUDE_DRIVERS_PRIM_PRIM_DRIVER_H_
30+
#ifndef INCLUDE_DRIVERS_MST_PRIM_DRIVER_H_
31+
#define INCLUDE_DRIVERS_MST_PRIM_DRIVER_H_
3232
#pragma once
3333

3434
#include "c_types/pgr_edge_t.h"
@@ -58,4 +58,4 @@ extern "C" {
5858
}
5959
#endif
6060

61-
#endif // INCLUDE_DRIVERS_PRIM_PRIM_DRIVER_H_
61+
#endif // INCLUDE_DRIVERS_MST_PRIM_DRIVER_H_

include/prim/pgr_kruskal.hpp renamed to include/mst/pgr_kruskal.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ along with this program; if not, write to the Free Software
2121
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2222
********************************************************************PGR-GNU*/
2323

24-
#ifndef INCLUDE_PRIM_PGR_KRUSKAL_HPP_
25-
#define INCLUDE_PRIM_PGR_KRUSKAL_HPP_
24+
#ifndef INCLUDE_MST_PGR_KRUSKAL_HPP_
25+
#define INCLUDE_MST_PGR_KRUSKAL_HPP_
2626
#pragma once
2727

2828
#include <boost/config.hpp>
@@ -82,7 +82,12 @@ class Pgr_kruskal {
8282
std::vector< std::vector< int64_t > > component;
8383
component.resize(num_comps);
8484
for (size_t i = 0; i < totalNodes; i++)
85-
component[components[i]].push_back(i);
85+
component[components[i]].push_back(graph[i].id);
86+
for (size_t i = 0; i < num_comps; i++) {
87+
std::sort(component[i].begin(), component[i].end());
88+
}
89+
sort(component.begin(), component.end());
90+
8691

8792
size_t size = component.size();
8893
for (size_t i = 0; i < size; i++){
@@ -94,7 +99,7 @@ class Pgr_kruskal {
9499

95100
pgr_kruskal_t tmp;
96101

97-
tmp.sub_graph = static_cast< int > (i+1);
102+
tmp.component = component[i][0];;
98103
tmp.cost = graph[*ei].cost; // cost
99104

100105
auto start_node = graph.graph[source(*ei, graph.graph)].id;
@@ -126,4 +131,4 @@ Pgr_kruskal< G >::kruskal(
126131
}
127132

128133

129-
#endif // INCLUDE_PRIM_PGR_KRUSKAL_HPP_
134+
#endif // INCLUDE_MST_PGR_KRUSKAL_HPP_

include/prim/pgr_prim.hpp renamed to include/mst/pgr_prim.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ along with this program; if not, write to the Free Software
2121
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2222
********************************************************************PGR-GNU*/
2323

24-
#ifndef INCLUDE_PRIM_PGR_PRIM_HPP_
25-
#define INCLUDE_PRIM_PGR_PRIM_HPP_
24+
#ifndef INCLUDE_MST_PGR_PRIM_HPP_
25+
#define INCLUDE_MST_PGR_PRIM_HPP_
2626
#pragma once
2727

2828
#include <boost/config.hpp>
@@ -225,4 +225,4 @@ Pgr_prim< G >::prim(
225225
}
226226

227227

228-
#endif // INCLUDE_PRIM_PGR_PRIM_HPP_
228+
#endif // INCLUDE_MST_PGR_PRIM_HPP_
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

sql/prim/kruskal.sql renamed to sql/mst/kruskal.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CREATE OR REPLACE FUNCTION pgr_kruskal(
3131
edges_sql TEXT, -- Edge sql
3232

3333
OUT seq INTEGER, -- Seq
34-
OUT sub_graph INTEGER, -- Subgraph
34+
OUT component BIGINT, -- the lowest number of the node in the component
3535
OUT edge BIGINT, -- Edge linked to that node
3636
OUT cost FLOAT, -- Cost of edge
3737
OUT tree_cost FLOAT) -- Spanning tree cost
File renamed without changes.

src/prim/CMakeLists.txt renamed to src/mst/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ADD_LIBRARY(prim OBJECT
1+
ADD_LIBRARY(mst OBJECT
22
kruskal.c
33
kruskal_driver.cpp
44
prim.c

src/prim/kruskal.c renamed to src/mst/kruskal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4949
/* for functions to get edges information */
5050
#include "c_common/edges_input.h"
5151

52-
#include "drivers/prim/kruskal_driver.h" // the link to the C++ code of the function
52+
#include "drivers/mst/kruskal_driver.h" // the link to the C++ code of the function
5353

5454
PGDLLEXPORT Datum kruskal(PG_FUNCTION_ARGS);
5555
PG_FUNCTION_INFO_V1(kruskal);
@@ -183,7 +183,7 @@ PGDLLEXPORT Datum kruskal(PG_FUNCTION_ARGS) {
183183

184184
// postgres starts counting from 1
185185
values[0] = Int32GetDatum(funcctx->call_cntr + 1);
186-
values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].sub_graph);
186+
values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].component);
187187
values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
188188
values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
189189
values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].tree_cost);

src/prim/kruskal_driver.cpp renamed to src/mst/kruskal_driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ along with this program; if not, write to the Free Software
2222
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2323
********************************************************************PGR-GNU*/
2424

25-
#include "drivers/prim/kruskal_driver.h"
25+
#include "drivers/mst/kruskal_driver.h"
2626

2727
#include <sstream>
2828
#include <deque>
2929
#include <vector>
3030

31-
#include "prim/pgr_kruskal.hpp"
31+
#include "mst/pgr_kruskal.hpp"
3232

3333
#include "cpp_common/pgr_alloc.hpp"
3434
#include "cpp_common/pgr_assert.h"

0 commit comments

Comments
 (0)