简体   繁体   中英

Boost Graph Library: Bundled Properties and iterating across edges

Just trying to get my head around the Boost Graph Library and I have a few questions. I'm writing some code which is a wrapper class around a BGL graph. The idea is that I can manipulate the graph however I want, then call a wrapper method to output the graph in GEXF (XML) format.

My code is something like this:

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;

Here are my questions:

  1. When I use bundled properties, I can access vertex_index, but I can't access edge_index. How do I get access to edge indices?

  2. In the code above, I wanted to keep the GEXF class generic, but I ran into a problem when I tried to declare Graph::edge_iterator e, e_end; The above code works, but it's using a concrete type. How should I declare the edge_iterator generically?

There is no edge_index by default in a boost::adjacency_list<...> object because maintaining one impacts the complexity. You must create one yourself, but you must take some care that it provides the desired functionality.

Previously :

Boost Subgraph and Bundled properties

edge_index zero for all edges?

Boost Graph edges with indexes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM