我在使用原始图表中的相同组件过滤子图时遇到问题。我想在子图的向量中输出它们。按照`connected_components中的例子,我试图让它适应我的需要:
// Create a typedef for the Graph type
typedef adjacency_list<
vecS,
vecS,
undirectedS,
property<vertex_index_t,int >,
property<edge_index_t,int> > Graph;
//typedef subgraph < Graph > SubGraph;
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
typedef graph_traits<Graph> GraphTraits;
// Iterators
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
typedef property_map<Graph, vertex_index_t>::type VertexIndexMap;
typedef property_map<Graph, edge_index_t>::type EdgeIndexMap;
std::vector<Graph> connected_components_subgraphs(const Graph &g)
{
std::vector<int> component(num_vertices(g));
int num = boost::connected_components(g, &component[0]);
for (int i=0; i<component.size(); i++)
cout << component[i] << endl;
cout << "NUM=" << num << endl;
// Something to output the induced subgraphs where every subgraph is in the same component
}
我完全停留在图形的过滤中,因为我不了解为向量组件中的顶点存储的外部属性如何被利用或传递给某些函数所需的过滤图表。
特别是,似乎这个问题与我的需求非常相似,但是没有任何代码,我发现很难找出问题。
splitting a boost graph into connected components
如何从同一连通组件中的节点输出诱导子图?
答案 0 :(得分:7)
您可以使用主图的filtered_graph
次视图:
typedef filtered_graph<Graph, EdgeInComponent, VertexInComponent> ComponentGraph;
std::vector<ComponentGraph> connected_components_subgraphs(Graph const&g)
{
vertex_component_map mapping = boost::make_shared<std::vector<unsigned long>>(num_vertices(g));
size_t num = boost::connected_components(g, mapping->data());
std::vector<ComponentGraph> component_graphs;
for (size_t i = 0; i < num; i++)
component_graphs.push_back(ComponentGraph(g, EdgeInComponent(mapping, i, g), VertexInComponent(mapping, i)));
return component_graphs;
}
当然,这只是提出了如何实现过滤谓词的问题。我选择分享mapping
向量:
typedef boost::shared_ptr<std::vector<unsigned long>> vertex_component_map;
我不想假设您可以共享全局或只是复制它。例如,VertexInComponent
谓词如下所示:
struct VertexInComponent
{
vertex_component_map mapping_;
unsigned long which_;
VertexInComponent(vertex_component_map m, unsigned long which)
: mapping_(m), which_(which) {}
template <typename Vertex> bool operator()(Vertex const&v) const {
return mapping_->at(v)==which_;
}
};
同样,EdgeInComponent
可以实现。事实上,您可以将其快捷方式并使用类似
struct AnyElement {
template <typename EdgeOrVertex> bool operator()(EdgeOrVertex const&) const { return true; }
};
两个中的一个。这是一个主要的样本:
Graph g;
add_edge(0, 1, g);
add_edge(1, 4, g);
add_edge(4, 0, g);
add_edge(2, 5, g);
for (auto const& component : connected_components_subgraphs(g))
{
std::cout << "component [ ";
for (auto e : make_iterator_range(edges(component)))
std::cout << source(e, component) << " -> " << target(e, component) << "; ";
std::cout << "]\n";
}
它打印出来:
component [ 0 -> 1; 1 -> 4; 4 -> 0; ]
component [ 2 -> 5; ]
component [ ]
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/make_shared.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
using namespace boost;
// Create a typedef for the Graph type
typedef adjacency_list<vecS, vecS, undirectedS, property<vertex_index_t, int>, property<edge_index_t, int>> Graph;
// typedef subgraph < Graph > SubGraph;
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
typedef graph_traits<Graph> GraphTraits;
// Iterators
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
typedef property_map<Graph, vertex_index_t>::type VertexIndexMap;
typedef property_map<Graph, edge_index_t>::type EdgeIndexMap;
typedef boost::shared_ptr<std::vector<unsigned long>> vertex_component_map;
struct EdgeInComponent
{
vertex_component_map mapping_;
unsigned long which_;
Graph const& master_;
EdgeInComponent(vertex_component_map m, unsigned long which, Graph const& master)
: mapping_(m), which_(which), master_(master) {}
template <typename Edge> bool operator()(Edge const&e) const {
return mapping_->at(source(e,master_))==which_
|| mapping_->at(target(e,master_))==which_;
}
};
struct VertexInComponent
{
vertex_component_map mapping_;
unsigned long which_;
VertexInComponent(vertex_component_map m, unsigned long which)
: mapping_(m), which_(which) {}
template <typename Vertex> bool operator()(Vertex const&v) const {
return mapping_->at(v)==which_;
}
};
struct AnyVertex {
template <typename Vertex> bool operator()(Vertex const&) const { return true; }
};
typedef filtered_graph<Graph, EdgeInComponent, VertexInComponent> ComponentGraph;
std::vector<ComponentGraph> connected_components_subgraphs(Graph const&g)
{
vertex_component_map mapping = boost::make_shared<std::vector<unsigned long>>(num_vertices(g));
size_t num = boost::connected_components(g, mapping->data());
std::vector<ComponentGraph> component_graphs;
for (size_t i = 0; i < num; i++)
component_graphs.push_back(ComponentGraph(g, EdgeInComponent(mapping, i, g), VertexInComponent(mapping, i)));
return component_graphs;
}
int main()
{
Graph g;
add_edge(0, 1, g);
add_edge(1, 4, g);
add_edge(4, 0, g);
add_edge(2, 5, g);
for (auto const& component : connected_components_subgraphs(g))
{
std::cout << "component [ ";
for (auto e : make_iterator_range(edges(component)))
std::cout << source(e, component) << " -> " << target(e, component) << "; ";
std::cout << "]\n";
}
}
c++11
如果你可以使用C ++ 11 lambdas可以使代码大大缩短,因为你可以就地定义过滤谓词:
typedef filtered_graph<Graph, function<bool(Graph::edge_descriptor)>, function<bool(Graph::vertex_descriptor)> > ComponentGraph;
std::vector<ComponentGraph> connected_components_subgraphs(Graph const&g)
{
vertex_component_map mapping = boost::make_shared<std::vector<unsigned long>>(num_vertices(g));
size_t num = boost::connected_components(g, mapping->data());
std::vector<ComponentGraph> component_graphs;
for (size_t i = 0; i < num; i++)
component_graphs.emplace_back(g,
[mapping,i,&g](Graph::edge_descriptor e) {
return mapping->at(source(e,g))==i
|| mapping->at(target(e,g))==i;
},
[mapping,i](Graph::vertex_descriptor v) {
return mapping->at(v)==i;
});
return component_graphs;
}
答案 1 :(得分:2)
而不是filtered_graph
,您可以使用subgraph
,如下所示:
vector<int> comp(num_vertices(g));
size_t num = boost::connected_components(g, comp.data());
vector<Graph*> comps(num);
for(size_t i=0;i<num;++i) {
comps[i] = & g.create_subgraph();
}
for(size_t i=0;i<num_vertices(g);++i) {
add_vertex(i, *comps[comp[i]]);
}
其中Graph
定义为:
using Graph = subgraph< adjacency_list<vecS, vecS, undirectedS, property<vertex_index_t, int>, property<edge_index_t, int>> >;
请注意,您需要使用local_to_global
将子图中的顶点描述符映射到根图。
正在运行示例:http://coliru.stacked-crooked.com/a/cebece41c0daed87
在这种情况下,了解filtered_graph
vs subgraph
的优点会很有趣。