mirror of
https://github.com/boostorg/graph.git
synced 2026-02-27 17:12:11 +00:00
made condensation graph separate
[SVN r10879]
This commit is contained in:
74
include/boost/graph/create_condensation_graph.hpp
Normal file
74
include/boost/graph/create_condensation_graph.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef BOOST_CREATE_CONDENSATION_GRAPH_HPP
|
||||
#define BOOST_CREATE_CONDENSATION_GRAPH_HPP
|
||||
|
||||
#include <boost/graph/graph_traits.hpp>
|
||||
#include <boost/property_map.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
template <typename Graph, typename ComponentLists,
|
||||
typename ComponentNumberMap,
|
||||
typename CondensationGraph, typename EdgeMultiplicityMap>
|
||||
void create_condensation_graph(const Graph& g,
|
||||
const ComponentLists& components,
|
||||
ComponentNumberMap component_number,
|
||||
CondensationGraph& cg,
|
||||
EdgeMultiplicityMap edge_mult_map)
|
||||
{
|
||||
typedef typename graph_traits<Graph>::vertex_descriptor vertex;
|
||||
typedef typename graph_traits<Graph>::vertices_size_type size_type;
|
||||
typedef typename graph_traits<CondensationGraph>::vertex_descriptor
|
||||
cg_vertex;
|
||||
std::vector<cg_vertex> to_cg_vertex(components.size());
|
||||
for (size_type s = 0; s < components.size(); ++s)
|
||||
to_cg_vertex[s] = add_vertex(cg);
|
||||
|
||||
for (size_type si = 0; si < components.size(); ++si) {
|
||||
cg_vertex s = to_cg_vertex[si];
|
||||
std::vector<cg_vertex> adj;
|
||||
for (size_type i = 0; i < components[si].size(); ++i) {
|
||||
vertex u = components[s][i];
|
||||
typename graph_traits<Graph>::adjacency_iterator v, v_end;
|
||||
for (tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
|
||||
cg_vertex t = to_cg_vertex[component_number[*v]];
|
||||
if (s != t) // Avoid loops in the condensation graph
|
||||
adj.push_back(t);
|
||||
}
|
||||
}
|
||||
std::sort(adj.begin(), adj.end());
|
||||
if (! adj.empty()) {
|
||||
size_type i = 0;
|
||||
cg_vertex t = adj[i];
|
||||
typename graph_traits<CondensationGraph>::edge_descriptor e;
|
||||
bool inserted;
|
||||
tie(e, inserted) = add_edge(s, t, cg);
|
||||
put(edge_mult_map, e, 1);
|
||||
++i;
|
||||
while (i < adj.size()) {
|
||||
if (adj[i] == t)
|
||||
put(edge_mult_map, e, get(edge_mult_map, e) + 1);
|
||||
else {
|
||||
t = adj[i];
|
||||
tie(e, inserted) = add_edge(s, t, cg);
|
||||
put(edge_mult_map, e, 1);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Graph, typename ComponentLists,
|
||||
typename ComponentNumberMap, typename CondensationGraph>
|
||||
void create_condensation_graph(const Graph& g,
|
||||
const ComponentLists& components,
|
||||
ComponentNumberMap component_number,
|
||||
CondensationGraph& cg)
|
||||
{
|
||||
create_condensation_graph(g, components, component_number, cg,
|
||||
dummy_property_map());
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_CREATE_CONDENSATION_GRAPH_HPP
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <boost/graph/vector_as_graph.hpp>
|
||||
#include <boost/graph/strong_components.hpp>
|
||||
#include <boost/graph/topological_sort.hpp>
|
||||
#include <boost/graph/create_condensation_graph.hpp>
|
||||
|
||||
// For a description of the implementation see ../doc/transitive_closure.pdf.
|
||||
|
||||
@@ -74,6 +75,7 @@ namespace boost {
|
||||
typedef typename property_traits<VertexIndexMap>::value_type size_type;
|
||||
typedef typename graph_traits<Graph>::adjacency_iterator
|
||||
adjacency_iterator;
|
||||
typedef size_type cg_vertex;
|
||||
|
||||
function_requires< VertexListGraphConcept<Graph> >();
|
||||
function_requires< AdjacencyGraphConcept<Graph> >();
|
||||
@@ -81,44 +83,26 @@ namespace boost {
|
||||
function_requires< EdgeMutableGraphConcept<GraphTC> >();
|
||||
function_requires< ReadablePropertyMapConcept<VertexIndexMap, vertex> >();
|
||||
|
||||
// Compute strongly connected components of the graph
|
||||
typedef size_type cg_vertex;
|
||||
std::vector<cg_vertex> component_number_vec(num_vertices(g));
|
||||
iterator_property_map<cg_vertex*, VertexIndexMap>
|
||||
component_number(&component_number_vec[0], index_map);
|
||||
|
||||
int num_scc = strong_components(g, component_number,
|
||||
vertex_index_map(index_map));
|
||||
|
||||
std::vector< std::vector<vertex> > components;
|
||||
build_component_lists(g, num_scc, component_number, components);
|
||||
|
||||
// Construct the condensation graph
|
||||
typedef std::vector< std::vector<cg_vertex> > CG_t;
|
||||
CG_t CG(num_scc);
|
||||
for (cg_vertex s = 0; s < components.size(); ++s) {
|
||||
std::vector<cg_vertex> adj;
|
||||
for (size_type i = 0; i < components[s].size(); ++i) {
|
||||
vertex u = components[s][i];
|
||||
adjacency_iterator v, v_end;
|
||||
for (tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
|
||||
cg_vertex t = component_number[*v];
|
||||
if (s != t) // Avoid loops in the condensation graph
|
||||
adj.push_back(t);
|
||||
}
|
||||
}
|
||||
std::sort(adj.begin(), adj.end());
|
||||
std::vector<cg_vertex>::iterator
|
||||
di = std::unique(adj.begin(), adj.end());
|
||||
if (di != adj.end())
|
||||
adj.erase(di, adj.end());
|
||||
CG[s] = adj;
|
||||
}
|
||||
CG_t CG;
|
||||
create_condensation_graph(g, components, component_number, CG);
|
||||
|
||||
// topological sort the condensation graph
|
||||
std::vector<cg_vertex> topo_order;
|
||||
std::vector<cg_vertex> topo_number(num_vertices(CG));
|
||||
|
||||
topological_sort(CG, std::back_inserter(topo_order),
|
||||
vertex_index_map(identity_property_map()));
|
||||
|
||||
std::reverse(topo_order.begin(), topo_order.end());
|
||||
size_type n = 0;
|
||||
for (std::vector<cg_vertex>::iterator i = topo_order.begin();
|
||||
|
||||
Reference in New Issue
Block a user