增强图:应用考虑特定边缘子集的算法

log*_*og0 4 c++ algorithm boost boost-graph

我有一个带有类型边的巨大图形(即带有类型属性的边).说

typedef adjacency_list<vecS, vecS, vertex_prop, edge_prop> Graph;  
Run Code Online (Sandbox Code Playgroud)

边缘的"类型"是edge_prop的成员,并且具有{A,B,C,D}中的值,
我想运行宽度优先的搜索算法,只考虑类型A或B的边缘.
你会怎样?去做 ?

toc*_*och 9

因为很难找到混合BGL的不同主题的简单示例,所以我使用filtered_graph和捆绑属性在下面发布一个完整且有效的示例:

#include <iostream>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>

using namespace boost;

enum edge_type_e {
  A, B, C, D
};

class edge_property_c {
public:
  edge_property_c(void) : type_m(A) {}
  edge_property_c(edge_type_e type) : type_m(type) {}
  edge_type_e type_m;
};

typedef adjacency_list<vecS, vecS, undirectedS, no_property, edge_property_c> graph_t;
typedef graph_t::edge_descriptor edge_id_t;

class edge_predicate_c {
public:
  edge_predicate_c() : graph_m(0) {}
  edge_predicate_c(graph_t& graph) : graph_m(&graph) {}
  bool operator()(const edge_id_t& edge_id) const {
    edge_type_e type = (*graph_m)[edge_id].type_m;
    return (type == A || type == B);
  }
private:
  graph_t* graph_m;
};

int main() {
  enum { a, b, c, d, e, n };
  const char* name = "abcde";
  graph_t g(n);
  add_edge(a, b, edge_property_c(A), g);
  add_edge(a, c, edge_property_c(C), g);
  add_edge(c, d, edge_property_c(A), g);
  add_edge(c, e, edge_property_c(B), g);
  add_edge(d, b, edge_property_c(D), g);
  add_edge(e, c, edge_property_c(B), g);

  filtered_graph<graph_t, edge_predicate_c> fg(g, edge_predicate_c(g));

  std::cout << "edge set: ";
  print_edges(g, name);
  std::cout << "filtered edge set: ";
  print_edges(fg, name);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)


log*_*og0 3

最后我认为 boost::graph 的方法是使用boost:filtered_graphdemo 进行使用

“filtered_graph 类模板是一个适配器,用于创建图的过滤视图。谓词函数对象确定原始图的哪些边和顶点将显示在过滤图中。”

因此,您可以基于 property_map 提供边缘(或顶点)过滤函子。就我而言,我使用内部捆绑属性。请参阅捆绑属性的属性映射。