Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
SET(LOCAL_FILES
components-family.rst
pgr_connectedComponentsV.rst
pgr_strongComponentsV.rst
pgr_biconnectedComponents.rst
)

foreach (f ${LOCAL_FILES})
Expand Down
4 changes: 2 additions & 2 deletions doc/components/components-family.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Parameter Type Default Description
.. return_componentsV_start

Description of the return values for connected components (vertex version) and strongly connected components (vertex version)
...............................................................................
.............................................................................................................................

Returns set of ``(seq, component, n_seq, node)``

Expand All @@ -269,7 +269,7 @@ Column Type Description
.. return_componentsE_start

Description of the return values for biconnected components, connected components (edge version) and strongly connected components (edge version)
...............................................................................
.................................................................................................................................................

Returns set of ``(seq, component, n_seq, edge)``

Expand Down
2 changes: 2 additions & 0 deletions doc/queries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ SET(LOCAL_FILES
doc-pgr_maxFlow.queries
doc-pgr_withPointsKSP.queries
doc-pgr_connectedComponentsV.queries
doc-pgr_strongComponentsV.queries
doc-pgr_biconnectedComponents.queries
)

foreach (f ${LOCAL_FILES})
Expand Down
2 changes: 1 addition & 1 deletion doc/src/proposed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Experimental and Proposed functions
contraction-family
flow-family
pgr_labelGraph
pgr_connectedComponentsV
components-family
VRP-category


Expand Down
71 changes: 35 additions & 36 deletions include/components/pgr_components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <boost/graph/biconnected_components.hpp>

#include <vector>
#include <map>
#include <utility>
#include <algorithm>

Expand All @@ -50,8 +51,8 @@ template < class G >
class Pgr_components {
public:
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::E_i E_i;
typedef typename G::E E;
typedef typename G::E_i E_i;

//! Connected Components Vertex Version
std::vector<pgr_components_rt> connectedComponentsV(
Expand All @@ -68,9 +69,7 @@ class Pgr_components {
private:
//! Generate Results, Vertex Version
std::vector<pgr_components_rt> generate_results(
G &graph,
std::vector< std::vector< int64_t > >);

};


Expand All @@ -80,29 +79,27 @@ class Pgr_components {
template < class G >
std::vector<pgr_components_rt>
Pgr_components< G >::generate_results(
G &graph,
std::vector< std::vector< int64_t > > components) {
// sort identifier
auto num_comps = components.size();
for (int i = 0; i < num_comps; i++) {
size_t num_comps = components.size();
for (size_t i = 0; i < num_comps; i++) {
std::sort(components[i].begin(), components[i].end());
}
sort(components.begin(), components.end());

// generate results
std::vector< pgr_components_rt > results;
for (auto i = 0; i < num_comps; i++) {
for (size_t i = 0; i < num_comps; i++) {
int64_t tempComp = components[i][0];
auto sizeCompi = components[i].size();
for (auto j = 0; j < sizeCompi; j++) {
size_t sizeCompi = components[i].size();
for (size_t j = 0; j < sizeCompi; j++) {
pgr_components_rt tmp;
tmp.identifier = components[i][j];
tmp.n_seq = j + 1;
tmp.n_seq = static_cast< int > (j + 1);
tmp.component = tempComp;
results.push_back(tmp);
}
}

return results;
}

Expand All @@ -111,40 +108,42 @@ template < class G >
std::vector<pgr_components_rt>
Pgr_components< G >::connectedComponentsV(
G &graph) {
int totalNodes = num_vertices(graph.graph);
size_t totalNodes = num_vertices(graph.graph);

// perform the algorithm
std::vector< int > components(totalNodes);
int num_comps = boost::connected_components(graph.graph, &components[0]);

//get the results
// get the results
std::vector< std::vector< int64_t > > results;
results.resize(num_comps);
for (int i = 0; i < totalNodes; i++)
for (size_t i = 0; i < totalNodes; i++)
results[components[i]].push_back(graph[i].id);

return generate_results(graph, results);
return generate_results(results);
}

//! Strongly Connected Components Vertex Version
template < class G >
std::vector<pgr_components_rt>
Pgr_components< G >::strongComponentsV(
G &graph) {
int totalNodes = num_vertices(graph.graph);
size_t totalNodes = num_vertices(graph.graph);

// perform the algorithm
std::vector< int > components(totalNodes);
int num_comps = boost::strong_components(graph.graph,
boost::make_iterator_property_map(components.begin(), get(boost::vertex_index, graph.graph)));
int num_comps = boost::strong_components(graph.graph,
boost::make_iterator_property_map(components.begin(),
get(boost::vertex_index,
graph.graph)));

// get the results
std::vector< std::vector< int64_t > > results;
results.resize(num_comps);
for (int i = 0; i < totalNodes; i++)
for (size_t i = 0; i < totalNodes; i++)
results[components[i]].push_back(graph[i].id);

return generate_results(graph, results);
return generate_results(results);
}

//! Biconnected Components
Expand All @@ -153,24 +152,24 @@ std::vector<pgr_components_rt>
Pgr_components< G >::biconnectedComponents(
G &graph) {
// perform the algorithm
struct order_edges {
bool operator() (const E &left, const E &right) const {
return left.get_property() < right.get_property();
}
};
typedef std::map< E, int > edge_map;
edge_map bicmp_map;
boost::associative_property_map< edge_map > bimap(bicmp_map);
int num_comps = biconnected_components(graph.graph, bimap);
struct order_edges {
bool operator() (const E &left, const E &right) const {
return left.get_property() < right.get_property();
}
};
typedef std::map< E, int > edge_map;
edge_map bicmp_map;

boost::associative_property_map< edge_map > bimap(bicmp_map);
size_t num_comps = biconnected_components(graph.graph, bimap);

// get the results
E_i ei, ei_end;
std::vector< std::vector< int64_t > > components(num_comps);
for (boost::tie(ei, ei_end) = edges(graph.graph); ei != ei_end; ei++)
components[bimap[*ei]].push_back(graph[*ei].id);
E_i ei, ei_end;
std::vector< std::vector< int64_t > > components(num_comps);
for (boost::tie(ei, ei_end) = edges(graph.graph); ei != ei_end; ei++)
components[bimap[*ei]].push_back(graph[*ei].id);

return generate_results(graph, components);
return generate_results(components);
}

#endif // INCLUDE_COMPONENTS_PGR_COMPONENTS_HPP_
10 changes: 6 additions & 4 deletions include/components/pgr_componentsGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace graph {
template <class G, typename T_V, typename T_E>
class Pgr_componentsGraph;

} // namespace graph
} // namespace graph

typedef graph::Pgr_componentsGraph <
boost::adjacency_list < boost::vecS, boost::vecS,
Expand Down Expand Up @@ -91,9 +91,11 @@ class Pgr_componentsGraph : public Pgr_base_graph<G, T_V, T_E> {
auto vm_s = Pgr_base_graph< G, T_V, T_E >::get_V(T_V(edge, true));
auto vm_t = Pgr_base_graph< G, T_V, T_E >::get_V(T_V(edge, false));

pgassert((Pgr_base_graph< G, T_V, T_E >::vertices_map).find(edge.source) !=
pgassert((Pgr_base_graph< G, T_V, T_E >::vertices_map).find(edge.source)
!=
(Pgr_base_graph< G, T_V, T_E >::vertices_map).end());
pgassert((Pgr_base_graph< G, T_V, T_E >::vertices_map).find(edge.target) !=
pgassert((Pgr_base_graph< G, T_V, T_E >::vertices_map).find(edge.target)
!=
(Pgr_base_graph< G, T_V, T_E >::vertices_map).end());
if (edge.cost >= 0) {
boost::tie(e, inserted) =
Expand All @@ -112,4 +114,4 @@ class Pgr_componentsGraph : public Pgr_base_graph<G, T_V, T_E> {
} // namespace graph
} // namespace pgrouting

#endif // INCLUDE_COMPONENTS_PGR_COMPONENTSGRAPH_HPP_
#endif // INCLUDE_COMPONENTS_PGR_COMPONENTSGRAPH_HPP_