C++ 又一个BGL';s中介中心性问题
根据回复,我尝试实现中间性中心性,如下所示:C++ 又一个BGL';s中介中心性问题,c++,c++11,boost,boost-graph,C++,C++11,Boost,Boost Graph,根据回复,我尝试实现中间性中心性,如下所示: typedef struct vpr_ { int id; } VProp; typedef boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, VProp, EProp> graph_t; boost::shared_array_property_map<double, boost::property_map<
typedef struct vpr_
{
int id;
} VProp;
typedef boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, VProp, EProp> graph_t;
boost::shared_array_property_map<double, boost::property_map<graph_t, int>::const_type> centrality_map(num_vertices(g), get(VProp::id, g));
我怎样才能解决这个问题?感谢Jackb,这个BGL算法和BGL中的许多其他算法一样,需要一个“顶点索引属性映射”——一个在区间
[0,num_顶点(g))内的图形顶点集和整数之间的一对一映射
现在,您的结构EProp和VProp不能用作顶点索引属性映射的替代品,这就是导致编译错误的原因
如果您可以使用boost::adjacency_list
作为图形类型,那么顶点索引映射将免费提供(作为图形的内部属性)。然后您将能够按照在引用线程中调用的方式调用中心性算法:centrality_map(num_顶点(g),get(boost::vertex_索引,g));
如果由于某些原因无法使用boost::vecS,则必须自己组织映射。对于集合和列表,此处将讨论它及其参考
请参阅此处有关属性映射的相关讨论,特别是此处的顶点索引映射:lol Nice:“还有一个”…?另一篇文章来自2011:)我认为(在“捆绑属性的属性映射”一节中),它应该是boost::shared\u array\u Property\u Map centrality\u Map(num\u顶点(g),get(&VProp::id,g))
。但是既然你没有提供一个可编译的示例,我就不能确定了。@sehe,是的,但是有些人降级你的帖子只是因为它已经存在了……我想确定我知道有人已经发布了一些关于这个的信息。@cv\u和\u-he,为什么你要使用int-VProp::*
作为类型参数?@cv\u和\u-he,我已经添加了一些按照要求编译了上面的代码。@jackb我没有认真使用Boost.Graph库,而且我对图论知之甚少,所以我的答案只是猜测。我使用int-VProp::*
作为类型参数的唯一原因是,我在前面的评论中链接的文档说,这是获取属性的方法从绑定属性映射。特别是它说您应该使用boost::property\u map::const\u type
或boost::property\u map::type
,这取决于您的图形是否为const。您添加的代码使用const\u type
失败,但使用type
编译。我必须关联到每个顶点和一些临边需要与每个节点和超边缘关联的一些C++类(在我的示例中没有显示,以便使代码可编译)。我必须使用列表,因为我想让我的图形在运行时可编辑,即删除或添加边。我的目标与上面显示的目标类似,即我想在之后调用boost::brandes\u betweenness\u centrality(g,centrality\u map)
。你的意思是我必须将所有VProp
和EProp
映射到std::map
?如果可能的话,我想避免这种情况……即使你计划删除顶点或边,你也可以使用vecS。如果你使用顶点列表,你必须填充一些映射(一些映射可以是std::map、boost::unordered\u map,甚至是boost::bimap)。然后必须填充它们的映射,然后将其传递给算法,而不是顶点索引映射。请参见此处的代码
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:246:0,
from /usr/local/include/boost/graph/random.hpp:23,
from ../../src/graph/Graph.h:25,
from Graph.cpp:23:
/usr/local/include/boost/graph/detail/adjacency_list.hpp: In instantiation of ‘struct boost::adj_list_any_vertex_pa::bind_<int, boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, vpr_>’:
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2568:12: required from ‘struct boost::detail::adj_list_choose_vertex_pa<int, boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, vpr_>’
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2705:12: required from ‘struct boost::adj_list_vertex_property_selector::bind_<boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, vpr_, int>’
/usr/local/include/boost/graph/properties.hpp:217:12: required from ‘struct boost::detail::vertex_property_map<boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, int>’
/usr/local/include/boost/graph/properties.hpp:228:10: required from ‘struct boost::property_map<boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, int>’
Graph.cpp:374:76: required from here
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2498:29: error: forming reference to void
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2499:35: error: forming reference to void
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2502:47: error: forming reference to void
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2504:53: error: forming reference to void
Graph.cpp: In member function ‘void Graph::getBetweennes()’:
Graph.cpp:374:88: error: template argument 2 is invalid
Graph.cpp:375:17: error: invalid type in declaration before ‘(’ token
In file included from ../../src/graph/graph_t.h:33:0,
from ../../src/graph/Graph.h:26,
from Graph.cpp:23:
../../src/graph/VProp.h:38:6: error: invalid use of non-static data member ‘vpr_::id’
Graph.cpp:375:46: error: from this location
Graph.cpp:375:52: error: expression list treated as compound expression in initializer [-fpermissive]
#include <boost/graph/iteration_macros.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/graph/random.hpp>
typedef struct vpr_
{
int id;
} VProp;
typedef struct epr_
{
int id;
} EProp;
typedef boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, VProp, EProp> graph_t;
int main(void) {
graph_t g;
boost::shared_array_property_map<double, boost::property_map<graph_t, VProp::*>::const_type> centrality_map(num_vertices(g), get(&VProp::id, g));
}
gtest.cpp: In function ‘int main()’:
gtest.cpp:18:81: error: template argument 2 is invalid
gtest.cpp:18:94: error: template argument 2 is invalid
gtest.cpp:18:110: error: invalid type in declaration before ‘(’ token
gtest.cpp:18:146: error: expression list treated as compound expression in initializer [-fpermissive]
gtest.cpp:18:146: error: cannot convert ‘boost::adj_list_any_vertex_pa::bind_<int vpr_::*, boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, vpr_>::type {aka boost::adj_list_vertex_property_map<boost::adjacency_list<boost::listS, boost::listS, boost::undirectedS, vpr_, epr_>, int, int&, int vpr_::*>}’ to ‘int’ in initialization