【问题标题】:Boost Graph Library: Bundled Properties and iterating across edgesBoost Graph Library:捆绑属性和跨边迭代
【发布时间】:2011-12-08 00:20:48
【问题描述】:

只是想了解一下 Boost Graph Library,我有几个问题。我正在编写一些代码,它是围绕 BGL 图的包装类。这个想法是我可以随心所欲地操纵图形,然后调用包装器方法以 GEXF (XML) 格式输出图形。

我的代码是这样的:

struct Vertex {
   std::string label;
   ...
};

struct Edge {
   std::string label;
   double weight;
   ...
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, Vertex, Edge> GraphType;

template <typename Graph>
class GEXF
{
   private:
      Graph graph;
   ...
};

template <typename Graph>
void GEXF<Graph>::buildXML()
{
   ...

   // output the edges
   property_map<adjacency_list<>, edge_index_t>::type edge_id = get(edge_index, graph);
   GraphType::edge_iterator e, e_end;
   for(tie(e, e_end) = edges(graph); e != e_end; ++e)
   {
      xmlpp::Element *edge = ePtr->add_child("edge");

      // next line gives an error, property not found
      edge->set_attribute("id", tostring<size_t>(get(edge_id, *e)));
      edge->set_attribute("source", tostring<size_t>(source(*e, graph)));
      edge->set_attribute("target", tostring<size_t>(target(*e, graph)));
   }
}

...
// instantiate in main():
GEXF<GraphType> gexf;

这是我的问题:

  1. 使用捆绑属性时,可以访问vertex_index,但无法访问edge_index。如何访问边缘索引?

  2. 在上面的代码中,我想保持 GEXF 类是通用的,但是当我尝试声明 Graph::edge_iterator e, e_end; 时遇到了问题。上面的代码有效,但它使用的是具体类型。我应该如何一般地声明 edge_iterator?

【问题讨论】:

    标签: c++ templates boost boost-graph


    【解决方案1】:

    boost::adjacency_list&lt;...&gt; 对象中默认没有 edge_index,因为维护一个会影响复杂性。您必须自己创建一个,但您必须注意它提供所需的功能。

    以前:

    Boost Subgraph and Bundled properties

    edge_index zero for all edges?

    Boost Graph edges with indexes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多