【发布时间】:2026-01-29 21:10:01
【问题描述】:
我想在边缘被列入黑名单的图上运行 Dijkstra,即,我想计算不使用这些链接的最短路径。 目前,我首先定义过滤器:
typedef std::pair<int, int> Edge;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int> > graph_t;
结构 BlackListEdgeConstraint { 私人的: 标准::设置黑名单; graph_t* g;
public:
BlackListEdgeConstraint():blackList(std::set<Edge>() ),g(NULL){};
BlackListEdgeConstraint(std::set<Edge>& list, graph_t* g_) : blackList(list), g(g_)
{
}
/**
* This is the "predicate function" used by boost::filtered_graph (
* see http://www.boost.org/doc/libs/1_64_0/libs/graph/doc/filtered_graph.html )
* It it returns true, the edge is included in the filtered graph, otherwise it is excluded.
*/
bool operator()(const boost::graph_traits<graph_t>::edge_descriptor& e) const
{
Edge edge(source(e,*g), target(e,*g) );
//Include the edge if it's not in the blacklist.
return blackList.find( edge ) == blackList.end();
}
};
然后我在 5 月执行此操作 main(...) 函数
... I fill the graph g ...
std::set<Edge> blacklist; blacklist.insert( Edge(0,1) );
BlackListEdgeConstraint filter(blacklist, &g);
boost::filtered_graph<graph_t, BlackListEdgeConstraint> filtered(g, filter);
... I run Dikjstra on the filtered graph ...
现在,我所做的工作有效,但很奇怪。实际上,我首先在顶点 0 和 1 之间创建了一条边。然后,在 operator() (...) 内部,我有一个 edge_descriptor 而不是 Edge(如果我将 Edge 作为参数,编译器会按照 here 的解释进行抱怨,所以我猜boost 正在某处进行一些转换,原因我不知道)。然后,我再次检索operator() (...) 中的顶点 0 和 1,并重建 Edge。您知道,如果直接接受 operator()(..) Edge,我会花很长时间做一些简单的事情。
你认为我可以以更优雅、更高效的方式完成相同的操作吗?
【问题讨论】:
标签: c++ performance boost-graph