Skip to content

Commit fdb5650

Browse files
authored
Merge pull request #858 from XJTUmg/gsoc-component
Improve code of connected components and strongly connected components
2 parents 6551559 + 751c043 commit fdb5650

File tree

8 files changed

+52
-83
lines changed

8 files changed

+52
-83
lines changed

include/c_types/pgr_componentV_t.h renamed to include/c_types/pgr_componentsV_rt.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*PGR-GNU*****************************************************************
2-
File: pgr_componentV_t.h
2+
File: pgr_componentSV_rt.h
33
44
Copyright (c) 2015 Celia Virginia Vergara Castillo
55
Mail: vicky_vergara@hotmail.com
@@ -23,8 +23,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2323
********************************************************************PGR-GNU*/
2424
/*! @file */
2525

26-
#ifndef INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
27-
#define INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
26+
#ifndef INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_
27+
#define INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_
2828
#pragma once
2929

3030

@@ -59,6 +59,6 @@ typedef struct {
5959
int64_t component;
6060
int n_seq;
6161
int64_t node;
62-
} pgr_componentV_t;
62+
} pgr_componentsV_rt;
6363

64-
#endif // INCLUDE_C_TYPES_PGR_COMPONENTV_T_H_
64+
#endif // INCLUDE_C_TYPES_PGR_COMPONENTSV_RT_H_

include/components/pgr_components.hpp

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3434
#include <boost/graph/strong_components.hpp>
3535

3636
#include <vector>
37-
#include <map>
3837
#include <utility>
3938
#include <algorithm>
4039

@@ -57,75 +56,42 @@ class Pgr_components {
5756
typedef typename G::V V;
5857

5958
//! Connected Components Vertex Version
60-
std::vector<pgr_componentV_t> connectedComponentsV(
59+
std::vector<pgr_componentsV_rt> connectedComponentsV(
6160
G &graph);
6261

6362
//! Strongly Connected Components Vertex Version
64-
std::vector<pgr_componentV_t> strongComponentsV(
63+
std::vector<pgr_componentsV_rt> strongComponentsV(
6564
G &graph);
6665

6766
private:
68-
//! Compare two pgr_componentV_t structs
69-
static bool sort_cmp(
70-
const pgr_componentV_t &a,
71-
const pgr_componentV_t &b);
72-
7367
//! Generate Results, Vertex Version
74-
std::vector<pgr_componentV_t> generate_resultsV(
68+
std::vector<pgr_componentsV_rt> generate_resultsV(
7569
G &graph,
7670
std::vector< V >);
7771

78-
//! Generate Map V_to_id
79-
std::map< V, int64_t > V_to_id;
80-
void generate_map(
81-
std::map< int64_t, V > id_to_V);
8272
};
8373

8474

8575
/******************** IMPLEMENTTION ******************/
8676

87-
//! Compare two pgr_componentV_t structs
88-
template < class G >
89-
bool
90-
Pgr_components< G >::sort_cmp(
91-
const pgr_componentV_t &a,
92-
const pgr_componentV_t &b) {
93-
if (a.component == b.component)
94-
return a.node < b.node;
95-
return a.component < b.component;
96-
}
97-
98-
//! Generate map V_to_id
99-
template < class G >
100-
void
101-
Pgr_components< G >::generate_map(
102-
std::map< int64_t, V > id_to_V) {
103-
V_to_id.clear();
104-
for (auto iter : id_to_V) {
105-
V_to_id.insert(std::make_pair(iter.second, iter.first));
106-
}
107-
}
108-
10977
//! Generate Results, Vertex Version
11078
template < class G >
111-
std::vector<pgr_componentV_t>
79+
std::vector<pgr_componentsV_rt>
11280
Pgr_components< G >::generate_resultsV(
11381
G &graph,
11482
std::vector< V > components) {
115-
// generate V_to_id
116-
generate_map(graph.vertices_map);
11783

11884
// generate results
11985
auto totalNodes = num_vertices(graph.graph);
12086

121-
std::vector< pgr_componentV_t > results;
87+
std::vector< pgr_componentsV_rt > results;
12288
results.resize(totalNodes);
12389

12490
std::vector< int64_t > result_comp;
12591
result_comp.resize(0);
12692
size_t temp_size = 0;
127-
for (auto i = 0; i < totalNodes; i++) {
128-
results[i].node = V_to_id.find(i)->second;
93+
for (V i = 0; i < totalNodes; i++) {
94+
results[i].node = graph[i].id;
12995
if (components[i] >= temp_size) {
13096
result_comp.push_back(results[i].node);
13197
temp_size++;
@@ -136,51 +102,54 @@ Pgr_components< G >::generate_resultsV(
136102
}
137103

138104
// generate component number
139-
for (auto i = 0; i < totalNodes; i++) {
105+
for (V i = 0; i < totalNodes; i++) {
140106
results[i].component = result_comp[components[i]];
141107
}
142108

143109
// sort results and generate n_seq
144-
std::sort(results.begin(), results.end(), sort_cmp);
145-
for (auto i = 0; i < totalNodes; i++) {
146-
if (i == 0 || results[i].component != results[i - 1].component) {
147-
results[i].n_seq = 1;
148-
} else {
149-
results[i].n_seq = results[i - 1].n_seq + 1;
150-
}
151-
}
110+
std::sort(results.begin(), results.end(),
111+
[](const pgr_componentsV_rt &left, const pgr_componentsV_rt &right) {
112+
return left.node < right.node; });
113+
114+
std::stable_sort(results.begin(), results.end(),
115+
[](const pgr_componentsV_rt &left, const pgr_componentsV_rt &right) {
116+
return left.component < right.component; });
117+
118+
auto current = results[0].component;
119+
int seq(0);
120+
for (auto &result: results) {
121+
result.n_seq = result.component == current ? ++seq : seq = 1;
122+
current = result.component;
123+
}
124+
152125
return results;
153126
}
154127

155128
//! Connected Components Vertex Version
156129
template < class G >
157-
std::vector<pgr_componentV_t>
130+
std::vector<pgr_componentsV_rt>
158131
Pgr_components< G >::connectedComponentsV(
159132
G &graph) {
160133
// perform the algorithm
161134
std::vector< V > components(num_vertices(graph.graph));
162135
boost::connected_components(graph.graph, &components[0]);
163136

164137
//get the results
165-
std::vector<pgr_componentV_t> results = generate_resultsV(graph, components);
166-
167-
// get the results
168-
return results;
138+
return generate_resultsV(graph, components);
169139
}
170140

171141
//! Strongly Connected Components Vertex Version
172142
template < class G >
173-
std::vector<pgr_componentV_t>
143+
std::vector<pgr_componentsV_rt>
174144
Pgr_components< G >::strongComponentsV(
175145
G &graph) {
176146
// perform the algorithm
177147
std::vector< V > components(num_vertices(graph.graph));
178-
boost::strong_components(graph.graph, &components[0]);
148+
boost::strong_components(graph.graph,
149+
boost::make_iterator_property_map(components.begin(), get(boost::vertex_index, graph.graph)));
179150

180151
// get the results
181-
std::vector<pgr_componentV_t> results = generate_resultsV(graph, components);
182-
183-
return results;
152+
return generate_resultsV(graph, components);
184153
}
185154

186155
#endif // INCLUDE_COMPONENTS_PGR_COMPONENTS_HPP_

include/drivers/components/connectedComponentsV_driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3232
#pragma once
3333

3434
#include "c_types/pgr_edge_t.h"
35-
#include "c_types/pgr_componentV_t.h"
35+
#include "c_types/pgr_componentsV_rt.h"
3636

3737
#ifdef __cplusplus
3838
extern "C" {
@@ -49,7 +49,7 @@ extern "C" {
4949
do_pgr_connectedComponentsV(
5050
pgr_edge_t *data_edges,
5151
size_t total_edges,
52-
pgr_componentV_t **return_tuples,
52+
pgr_componentsV_rt **return_tuples,
5353
size_t *return_count,
5454
char ** log_msg,
5555
char ** notice_msg,

include/drivers/components/strongComponentsV_driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3232
#pragma once
3333

3434
#include "c_types/pgr_edge_t.h"
35-
#include "c_types/pgr_componentV_t.h"
35+
#include "c_types/pgr_componentsV_rt.h"
3636

3737
#ifdef __cplusplus
3838
extern "C" {
@@ -49,7 +49,7 @@ extern "C" {
4949
do_pgr_strongComponentsV(
5050
pgr_edge_t *data_edges,
5151
size_t total_edges,
52-
pgr_componentV_t **return_tuples,
52+
pgr_componentsV_rt **return_tuples,
5353
size_t *return_count,
5454
char ** log_msg,
5555
char ** notice_msg,

src/components/src/connectedComponentsV.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static
6767
void
6868
process(
6969
char* edges_sql,
70-
pgr_componentV_t **result_tuples,
70+
pgr_componentsV_rt **result_tuples,
7171
size_t *result_count) {
7272
/*
7373
* https://www.postgresql.org/docs/current/static/spi-spi-connect.html
@@ -146,7 +146,7 @@ PGDLLEXPORT Datum connectedComponentsV(PG_FUNCTION_ARGS) {
146146
/**************************************************************************/
147147
/* MODIFY AS NEEDED */
148148
/* */
149-
pgr_componentV_t *result_tuples = NULL;
149+
pgr_componentsV_rt *result_tuples = NULL;
150150
size_t result_count = 0;
151151
/* */
152152
/**************************************************************************/
@@ -203,7 +203,7 @@ PGDLLEXPORT Datum connectedComponentsV(PG_FUNCTION_ARGS) {
203203

204204
funcctx = SRF_PERCALL_SETUP();
205205
tuple_desc = funcctx->tuple_desc;
206-
result_tuples = (pgr_componentV_t*) funcctx->user_fctx;
206+
result_tuples = (pgr_componentsV_rt*) funcctx->user_fctx;
207207

208208
if (funcctx->call_cntr < funcctx->max_calls) {
209209
HeapTuple tuple;

src/components/src/connectedComponentsV_driver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5050

5151
template < class G >
5252
static
53-
std::vector<pgr_componentV_t>
53+
std::vector<pgr_componentsV_rt>
5454
pgr_connectedComponentsV(
5555
G &graph) {
56-
std::vector<pgr_componentV_t> results;
56+
std::vector<pgr_componentsV_rt> results;
5757
Pgr_components< G > fn_components;
5858
return fn_components.connectedComponentsV(graph);
5959
}
@@ -63,7 +63,7 @@ void
6363
do_pgr_connectedComponentsV(
6464
pgr_edge_t *data_edges,
6565
size_t total_edges,
66-
pgr_componentV_t **return_tuples,
66+
pgr_componentsV_rt **return_tuples,
6767
size_t *return_count,
6868
char ** log_msg,
6969
char ** notice_msg,
@@ -81,7 +81,7 @@ do_pgr_connectedComponentsV(
8181

8282
graphType gType = UNDIRECTED;
8383

84-
std::vector<pgr_componentV_t> results;
84+
std::vector<pgr_componentsV_rt> results;
8585

8686
log << "Working with Undirected Graph\n";
8787
pgrouting::UndirectedGraph undigraph(gType);

src/components/src/strongComponentsV.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static
6767
void
6868
process(
6969
char* edges_sql,
70-
pgr_componentV_t **result_tuples,
70+
pgr_componentsV_rt **result_tuples,
7171
size_t *result_count) {
7272
/*
7373
* https://www.postgresql.org/docs/current/static/spi-spi-connect.html
@@ -146,7 +146,7 @@ PGDLLEXPORT Datum strongComponentsV(PG_FUNCTION_ARGS) {
146146
/**************************************************************************/
147147
/* MODIFY AS NEEDED */
148148
/* */
149-
pgr_componentV_t *result_tuples = NULL;
149+
pgr_componentsV_rt *result_tuples = NULL;
150150
size_t result_count = 0;
151151
/* */
152152
/**************************************************************************/
@@ -203,7 +203,7 @@ PGDLLEXPORT Datum strongComponentsV(PG_FUNCTION_ARGS) {
203203

204204
funcctx = SRF_PERCALL_SETUP();
205205
tuple_desc = funcctx->tuple_desc;
206-
result_tuples = (pgr_componentV_t*) funcctx->user_fctx;
206+
result_tuples = (pgr_componentsV_rt*) funcctx->user_fctx;
207207

208208
if (funcctx->call_cntr < funcctx->max_calls) {
209209
HeapTuple tuple;

src/components/src/strongComponentsV_driver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5050

5151
template < class G >
5252
static
53-
std::vector<pgr_componentV_t>
53+
std::vector<pgr_componentsV_rt>
5454
pgr_strongComponentsV(
5555
G &graph) {
56-
std::vector<pgr_componentV_t> results;
56+
std::vector<pgr_componentsV_rt> results;
5757
Pgr_components< G > fn_components;
5858
return fn_components.strongComponentsV(graph);
5959
}
@@ -63,7 +63,7 @@ void
6363
do_pgr_strongComponentsV(
6464
pgr_edge_t *data_edges,
6565
size_t total_edges,
66-
pgr_componentV_t **return_tuples,
66+
pgr_componentsV_rt **return_tuples,
6767
size_t *return_count,
6868
char ** log_msg,
6969
char ** notice_msg,
@@ -81,7 +81,7 @@ do_pgr_strongComponentsV(
8181

8282
graphType gType = DIRECTED;
8383

84-
std::vector<pgr_componentV_t> results;
84+
std::vector<pgr_componentsV_rt> results;
8585

8686
log << "Working with Directed Graph\n";
8787
pgrouting::DirectedGraph digraph(gType);

0 commit comments

Comments
 (0)