【问题标题】:find connected components of filtered graph查找过滤图的连通分量
【发布时间】:2012-11-13 15:00:53
【问题描述】:

如何使用 BGL 计算过滤图的连通分量?我创建了一个工作过滤器,它检查一个名为“alive”的自定义顶点属性并仅返回“活动”顶点,但 connected_components 阻塞 filtered_graph 。我认为这与过滤后的图具有非连续顶点 id 的事实有关,因此现在不再定义已定义的 operator[],但我不确定为什么或如何围绕它进行编码。

typedef boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, VertexProperties> Graph;
typedef boost::property_map<Graph, ::vertex_alive_t>::type AliveMap;

template <typename AliveMap>
struct vertex_is_alive {
  vertex_is_alive() { }
  vertex_is_alive(AliveMap alive) : m_alive(alive) { }
  template <typename Vertex>
  bool operator()(const Vertex& v) const {
    return boost::get(m_alive,v) == STILL_ALIVE_CODE;
  }
  AliveMap m_alive;
};

Graph G;
//generate G...
int N = boost::num_vertices(G);
vector<int> component(N);
int num = boost::connected_components(G, &component[0]);
//do something with component and play around with vertex_alive statuses...this part works fine.
vertex_is_alive<AliveMap> filter(boost::get(::vertex_alive_t(), G));
boost::filtered_graph<Graph, vertex_is_alive<AliveMap> > fG (G, filter); 
int num = boost::connected_components(fG, &component[0]);
//this makes it choke

我得到的错误信息是(删除家谱):

/usr/local/include/boost/property_map/property_map.hpp:354:56: error: no match for ‘operator[]’ in ‘((const boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >, boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >*, unsigned int, unsigned int&, vertex_alive_t>&)pa)[k]’
/usr/local/include/boost/property_map/property_map.hpp:354:56: note: candidate is:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:245:0,
                 from main.cpp:1:
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2465:24: note: Reference boost::vec_adj_list_vertex_property_map<Graph, GraphPtr, ValueType, Reference, Tag>::operator[](boost::vec_adj_list_vertex_property_map<Graph, GraphPtr, ValueType, Reference, Tag>::key_type) const [with Graph = boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >; GraphPtr = boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >*; ValueType = unsigned int; Reference = unsigned int&; Tag = vertex_alive_t; boost::vec_adj_list_vertex_property_map<Graph, GraphPtr, ValueType, Reference, Tag>::key_type = unsigned int]
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2465:24: note:   no known conversion for argument 1 from ‘const boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>’ to ‘boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >, boost::adjacency_list<boost::hash_mapS, boost::vecS, boost::undirectedS, boost::property<vertex_alive_t, unsigned int> >*, unsigned int, unsigned int&, vertex_alive_t>::key_type {aka unsigned int}’

【问题讨论】:

  • 你应该使用boost::filtered_graph&lt;Graph,boost::keep_all, vertex_is_alive&lt;AliveMap&gt; &gt; fG (G, boost::keep_all(), filter);。过滤图的签名是 `filtered_graph`,所以您使用顶点过滤器作为边过滤器。错误中的最后一行暗示 boost::get(m_alive,x) 要求 x 是一个顶点描述符(无符号整数),并且它被传递一个边缘描述符(const boost::detail::edge_desc_impl<:undirected_tag unsigned int>)。

标签: c++ boost boost-graph


【解决方案1】:

你应该使用 boost::filtered_graph > fG (G, boost::keep_all(), filter);。过滤图的签名是filtered_graph&lt;Graph, EdgePredicate, VertexPredicate&gt;,因此您使用顶点过滤器作为边过滤器。错误中的最后一行暗示 boost::get(m_alive,x) 要求 x 是一个顶点描述符 (unsigned int) 并且它被传递一个边描述符 (const boost::detail::edge_desc_impl&lt;boost::undirected_tag, unsigned int&gt;)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多