使用boost::make_filtered_graph进行图过滤的示例程序
在使用boost库进行图处理的时候,经常会遇到需要对图进行过滤的情况。这时我们可以使用boost::make_filtered_graph函数来创建一个过滤器过滤后的图。
下面是一个使用boost::make_filtered_graph进行图过滤的示例程序,该程序创建了一个有向图,并使用一个过滤器过滤出入度大于2的节点和边:
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/make_filtered_graph.hpp>
using namespace boost;
//定义一个有向图类型
typedef adjacency_list<vecS, vecS, directedS> graph_t;
//定义过滤器
struct in_out_degree_filter {
template <typename VertexOrEdge>
bool operator()(const VertexOrEdge& v) const {
if(is_vertex(v)) {
return (in_degree(v, graph) + out_degree(v, graph)) > 2;
}else{
typename graph_traits<graph_t>::edge_d