如何在boost图库中使用带有捆绑属性图的`randomize_property`?

时间:2011-04-19 18:04:47

标签: c++ boost graph

在文档中:http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/random.html#randomize_property

只有一个函数原型,我找不到一个有效的例子。 我尝试了几件事,但它无法编译。 这是一个简单的源代码:

#include <ctime>
#include <iostream>
#include <boost/graph/random.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/graph/graphviz.hpp>
using namespace std;
using namespace boost;

struct EdgeProperty {
  int cost;
}; 

typedef adjacency_list<
        setS, // disallow parallel edge
        vecS, 
        undirectedS,
        no_property,
        EdgeProperty
> Graph;

typedef erdos_renyi_iterator<minstd_rand, Graph> ERGen;

int main(int argc, const char *argv[])
{
  minstd_rand gen(time(0));
  assert(argc >= 3);
  int n = atoi(argv[1]);
  double p = atof(argv[2]);
  Graph g(ERGen(gen, n, p), ERGen(), n);

  // randomize_property< [unknown class] >(g, gen);

  return 0;
}

更新:@phooji提供的代码有效。我为EdgeProperty添加了一个默认构造函数,我的代码也编译了:

struct EdgeProperty {
  EdgeProperty(int x = 0) : cost(x) { }
  int cost;
}; 

原始编译错误是作为gist here发布的,我无法理解。希望有人告诉我这是如何运作的。

3 个答案:

答案 0 :(得分:2)

这为我编译:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <boost/random/linear_congruential.hpp>

struct myedge {
  myedge(int x) : testme(x) {
  }
  int testme;
};

typedef boost::adjacency_list<boost::setS, // disallow parallel edge
  boost::vecS,
  boost::undirectedS,
  boost::no_property,
  myedge
  > mygraph;

int main(int argc, char**argv) {
  mygraph g;

  // auto pmap = boost::get(&myedge::testme, g);
  boost::minstd_rand gen(0);
  boost::randomize_property<boost::edge_bundle_t>(g, gen);
  return EXIT_SUCCESS; // :)
}

希望有所帮助 - 我没有时间对其进行实际测试,如果这不是你想要的那样,请道歉。

答案 1 :(得分:0)

我可以看到至少一个问题,即无效的边缘属性定义。要定义图属性,应使用property<>类型。有几种预定义的属性类型,如索引,权重,颜色等。要定义边缘成本,请使用edge_weight_t属性类型。因此,图表类型定义应如下所示:

typedef adjacency_list<
      setS, // disallow parallel edge
      vecS,
      undirectedS,
      no_property,
      property<edge_weight_t, int>
> Graph; 

要访问属性类型,请使用property_map<>property_map<Graph, edge_weight_t>::type

编辑我对捆绑属性的错误,仍然很难为randomize_property<Property>的模板参数提供正确的类型,该参数应该是属性 kind 。如果您在我的示例中定义图表,则使用情况为randomize_property<edge_weight_t>(g, gen);

答案 2 :(得分:0)

您可以使用以下解决方法,直到通过boost正确整理:

boost::detail::randomize_property<int EdgeProperty::*> 
      (g, gen, &EdgeProperty::cost, boost::edge_bundle_t);
相关问题