Skip to content

Commit bdc65d4

Browse files
authored
Merge pull request #30 from nike0good/closure1
pgr_transitiveClosure GSOC-2019 week 11
2 parents 8cb1c96 + a6f5b6c commit bdc65d4

File tree

20 files changed

+441
-237
lines changed

20 files changed

+441
-237
lines changed

configuration.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ chinese | Y | Y | Y
3737
spanningTree | Y | Y | Y
3838
mincut | Y | Y | Y
3939
version | Y | Y | Y
40-
topologicalSort | Y | Y | Y
41-
#transitiveClosure | Y | Y | N
40+
topologicalSort | Y | Y | Y
41+
transitiveClosure | Y | Y | Y
4242

4343

4444
#----------------------

doc/topologicalSort/pgr_topologicalSort.rst

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ In particular, the topological sort algorithm implemented by Boost.Graph.
2424

2525
.. rubric:: Availability
2626

27+
* Version 3.0.0
28+
29+
* New **experimental** function
30+
31+
.. rubric:: Support
32+
33+
* **Supported versions:**
34+
current(`3.0 <https://docs.pgrouting.org/dev/en/pgr_topologicalSort.html>`__)
35+
2736
* **TBD**
2837

2938
Description
@@ -35,8 +44,7 @@ in the graph, then v comes before u in the ordering.
3544
This implementation can only be used with a **directed** graph with no cycles i.e. directed acyclic graph.
3645

3746
The main characteristics are:
38-
- Process is valid for weighted directed acyclic graphs only. otherwise it will throw warnings.
39-
- Values are returned when there is a path.
47+
- Process is valid for directed acyclic graphs only. otherwise it will throw warnings.
4048

4149
- For optimization purposes, if there are more than one answer, the function will return one of them.
4250

@@ -57,22 +65,11 @@ Signatures
5765
RETURNS SET OF (seq, sorted_v)
5866
OR EMPTY SET
5967
60-
61-
.. index::
62-
single: topologicalSort(One to One) - Experimental
63-
64-
One to One
65-
...............................................................................
66-
67-
.. code-block:: none
68-
69-
pgr_dagShortestPath(edges_sql)
70-
RETURNS SET OF (seq, sorted_v)
71-
OR EMPTY SET
72-
73-
:Example: From vertex :math:`1` to vertex :math:`6`
68+
:Example: For a **directed** graph
7469

7570
.. literalinclude:: doc-pgr_topologicalSort.queries
71+
:start-after: -- q1
72+
:end-before: -- q2
7673

7774
Parameters
7875
-------------------------------------------------------------------------------

doc/transitiveClosure/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
SET(LOCAL_FILES
3+
pgr_transitiveClosure.rst
4+
)
5+
6+
foreach (f ${LOCAL_FILES})
7+
configure_file(${f} "${PGR_DOCUMENTATION_SOURCE_DIR}/${f}")
8+
list(APPEND LOCAL_DOC_FILES ${PGR_DOCUMENTATION_SOURCE_DIR}/${f})
9+
endforeach()
10+
11+
set(PGROUTING_DOC_FILES ${PGROUTING_DOC_FILES} ${LOCAL_DOC_FILES} PARENT_SCOPE)
12+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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: https://creativecommons.org/licenses/by-sa/3.0/
8+
****************************************************************************
9+
10+
pgr_transitiveClosure
11+
===============================================================================
12+
13+
``pgr_transitiveClosure`` — Returns the transitive closure graph of the input graph.
14+
In particular, the transitive closure algorithm implemented by Boost.Graph.
15+
16+
.. figure:: images/boost-inside.jpeg
17+
:target: https://www.boost.org/doc/libs/1_70_0/libs/graph/doc/transitive_closure.html
18+
19+
Boost Graph Inside
20+
21+
.. include:: proposed.rst
22+
:start-after: begin-warn-expr
23+
:end-before: end-warn-expr
24+
25+
.. rubric:: Availability
26+
27+
* Version 3.0.0
28+
29+
* New **experimental** function
30+
31+
.. rubric:: Support
32+
33+
* **Supported versions:**
34+
current(`3.0 <https://docs.pgrouting.org/dev/en/pgr_transitiveClosure.html>`__)
35+
36+
Description
37+
-------------------------------------------------------------------------------
38+
39+
The transitive_closure() function transforms the input graph g into the transitive closure graph tc.
40+
41+
This implementation can only be used with a **directed** graph with no cycles i.e. directed acyclic graph.
42+
43+
The main characteristics are:
44+
- Process is valid for directed acyclic graphs only. otherwise it will throw warnings.
45+
46+
- The returned values are not ordered:
47+
48+
* Running time: :math:`O(|V||E|)`
49+
50+
51+
Signatures
52+
-------------------------------------------------------------------------------
53+
54+
.. rubric:: Summary
55+
56+
The pgr_transitiveClosure function has the following signature:
57+
58+
.. index::
59+
single: transitiveClosure
60+
61+
.. code-block:: none
62+
63+
pgr_transitiveClosure(Edges SQL)
64+
RETURNS SETOF (id, vid, target_array)
65+
66+
:Example: Complete Graph of 3 vertexs
67+
68+
.. literalinclude:: doc-pgr_transitiveClosure.queries
69+
:start-after: -- q1
70+
:end-before: -- q2
71+
72+
Parameters
73+
-------------------------------------------------------------------------------
74+
75+
======================= ====================== =================================================
76+
Column Type Description
77+
======================= ====================== =================================================
78+
**Edges SQL** ``TEXT`` SQL query as described in `Inner query`_
79+
======================= ====================== =================================================
80+
81+
Inner query
82+
-------------------------------------------------------------------------------
83+
84+
.. include:: pgRouting-concepts.rst
85+
:start-after: basic_edges_sql_start
86+
:end-before: basic_edges_sql_end
87+
88+
Result Columns
89+
-------------------------------------------------------------------------------
90+
91+
RETURNS SETOF (seq, vid, target_array)
92+
93+
The function returns a single row. The columns of the row are:
94+
95+
============================ ================= ===================================================================
96+
Column Type Description
97+
============================ ================= ===================================================================
98+
**seq** ``INTEGER`` Sequential value starting from **1**.
99+
**vid** ``BIGINT`` Identifier of the vertex.
100+
**target_array** ``ARRAY[BIGINT]`` Array of identifiers of the vertices that are reachable from vertex v.
101+
============================ ================= ===================================================================
102+
103+
Additional Examples
104+
-------------------------------------------------------------------------------
105+
106+
:Example: Some sub graphs of the sample data
107+
108+
.. literalinclude:: doc-pgr_transitiveClosure.queries
109+
:start-after: -- q2
110+
:end-before: -- q4
111+
112+
113+
See Also
114+
-------------------------------------------------------------------------------
115+
116+
* https://en.wikipedia.org/wiki/Transitive_closure
117+
* The queries use the :doc:`sampledata` network.
118+
119+
.. rubric:: Indices and tables
120+
121+
* :ref:`genindex`
122+
* :ref:`search`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Do not use extensions
2+
SET(LOCAL_FILES
3+
doc-transitiveClosure
4+
)
5+
6+
foreach (f ${LOCAL_FILES})
7+
configure_file("${f}.result" "${PGR_DOCUMENTATION_SOURCE_DIR}/${f}.queries")
8+
list(APPEND LOCAL_DOC_FILES "${PGR_DOCUMENTATION_SOURCE_DIR}/${f}.queries")
9+
endforeach()
10+
11+
set(PGROUTING_DOC_FILES ${PGROUTING_DOC_FILES} ${LOCAL_DOC_FILES} PARENT_SCOPE)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
BEGIN;
2+
BEGIN
3+
SET client_min_messages TO NOTICE;
4+
SET
5+
CREATE TABLE edge_table1 (
6+
id serial,
7+
source integer,
8+
target integer,
9+
cost double precision,
10+
reverse_cost double precision
11+
);
12+
CREATE TABLE
13+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,1,1,-1);
14+
INSERT 0 1
15+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,3,1,-1);
16+
INSERT 0 1
17+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,2,1,-1);
18+
INSERT 0 1
19+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (1,3,1,-1);
20+
INSERT 0 1
21+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (1,2,1,-1);
22+
INSERT 0 1
23+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (3,2,1,-1);
24+
INSERT 0 1
25+
-- q1
26+
SELECT * FROM pgr_transitiveclosure(
27+
'SELECT id,source,target,cost,reverse_cost FROM edge_table1'
28+
);
29+
seq | vid | target_array
30+
-----+-----+--------------
31+
1 | 0 | {1,3,2}
32+
2 | 1 | {3,2}
33+
3 | 3 | {2}
34+
4 | 2 | {}
35+
(4 rows)
36+
37+
-- q2
38+
SELECT * FROM pgr_transitiveclosure(
39+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2'
40+
);
41+
seq | vid | target_array
42+
-----+-----+--------------
43+
1 | 2 | {}
44+
2 | 3 | {2}
45+
(2 rows)
46+
47+
SELECT * FROM pgr_transitiveclosure(
48+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=3'
49+
);
50+
seq | vid | target_array
51+
-----+-----+--------------
52+
1 | 3 | {}
53+
2 | 4 | {3}
54+
(2 rows)
55+
56+
SELECT * FROM pgr_transitiveclosure(
57+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2 or id=3'
58+
);
59+
seq | vid | target_array
60+
-----+-----+--------------
61+
1 | 2 | {}
62+
2 | 3 | {2}
63+
3 | 4 | {3,2}
64+
(3 rows)
65+
66+
SELECT * FROM pgr_transitiveclosure(
67+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=11'
68+
);
69+
seq | vid | target_array
70+
-----+-----+--------------
71+
1 | 6 | {11}
72+
2 | 11 | {}
73+
(2 rows)
74+
75+
-- q3
76+
SELECT * FROM pgr_transitiveclosure(
77+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where cost=-1 or reverse_cost=-1'
78+
);
79+
seq | vid | target_array
80+
-----+-----+---------------
81+
1 | 2 | {}
82+
2 | 3 | {11,12,6,2}
83+
3 | 4 | {11,12,3,6,2}
84+
4 | 6 | {11,12}
85+
5 | 11 | {12}
86+
6 | 10 | {11,12}
87+
7 | 12 | {}
88+
(7 rows)
89+
90+
-- q4
91+
ROLLBACK;
92+
ROLLBACK
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
CREATE TABLE edge_table1 (
2+
id serial,
3+
source integer,
4+
target integer,
5+
cost double precision,
6+
reverse_cost double precision
7+
);
8+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,1,1,-1);
9+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,3,1,-1);
10+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (0,2,1,-1);
11+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (1,3,1,-1);
12+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (1,2,1,-1);
13+
INSERT INTO edge_table1 (source,target,cost,reverse_cost) VALUES (3,2,1,-1);
14+
15+
\echo -- q1
16+
17+
SELECT * FROM pgr_transitiveclosure(
18+
'SELECT id,source,target,cost,reverse_cost FROM edge_table1'
19+
);
20+
21+
\echo -- q2
22+
23+
SELECT * FROM pgr_transitiveclosure(
24+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2'
25+
);
26+
SELECT * FROM pgr_transitiveclosure(
27+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=3'
28+
);
29+
30+
SELECT * FROM pgr_transitiveclosure(
31+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=2 or id=3'
32+
);
33+
34+
SELECT * FROM pgr_transitiveclosure(
35+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where id=11'
36+
);
37+
38+
\echo -- q3
39+
40+
SELECT * FROM pgr_transitiveclosure(
41+
'SELECT id,source,target,cost,reverse_cost FROM edge_table where cost=-1 or reverse_cost=-1'
42+
);
43+
44+
45+
\echo -- q4
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/perl -w
2+
3+
%main::tests = (
4+
'any' => {
5+
'comment' => 'transitiveClosure algorithm tests.',
6+
'data' => [ ],
7+
'tests' => [qw(
8+
doc-transitiveClosure
9+
)],
10+
'documentation' => [qw(
11+
doc-transitiveClosure
12+
)]
13+
},
14+
15+
);
16+
17+
1;

include/c_types/transitiveClosure_rt.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4242
* return type for contraction
4343
* ***********************************************************************/
4444
typedef struct {
45-
int64_t id;
46-
int64_t *transitiveClosure_vertices;
47-
int transitiveClosure_vertices_size;
45+
int seq;
46+
int64_t vid;
47+
int64_t *target_array;
48+
int target_array_size;
4849
} transitiveClosure_rt;
4950

5051
#endif // INCLUDE_C_TYPES_TRANSITIVECLOSURE_RT_H_

include/drivers/transitiveClosure/transitiveClosure_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extern "C" {
4949
5050
********************************************************/
5151
void
52-
do_pgr_contractGraph(
52+
do_pgr_transitiveClosure(
5353
pgr_edge_t *data_edges,
5454
size_t total_tuples,
5555
transitiveClosure_rt **return_tuples,

0 commit comments

Comments
 (0)