【发布时间】:2014-11-02 08:35:00
【问题描述】:
我一直在努力弄清楚如何做到这一点。我有兴趣快速找到图形的割集。我知道 BGL 支持通过对例如 edmonds_karp_max_flow 支持的 colorMap 参数进行迭代来查找割集。 Gomory Hu 算法需要多次调用最小割算法。
我希望的结果是拥有一个包含以下内容的多图: (颜色,顶点)
以下代码尝试重写 Boost 图形库中的示例,以使用多映射作为 associative_property_map。可以通过以下方式编译代码: clang -lboost_graph -o edmonds_karp edmonds_karp.cpp 或 g++ 而不是 clang。我不明白由此产生的错误。
#include <boost/config.hpp>
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/edmonds_karp_max_flow.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/unordered_map.hpp>
int main()
{
using namespace boost;
typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
typedef adjacency_list < listS, vecS, directedS,
property < vertex_name_t, std::string >,
property < edge_capacity_t, long,
property < edge_residual_capacity_t, long,
property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
Graph g;
property_map < Graph, edge_capacity_t >::type
capacity = get(edge_capacity, g);
property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
property_map < Graph, edge_residual_capacity_t >::type
residual_capacity = get(edge_residual_capacity, g);
std::multimap<default_color_type, Traits::vertex_descriptor> colorMap;
boost::associative_property_map< std::map<default_color_type,
Traits::vertex_descriptor> >
color_map(colorMap);
Traits::vertex_descriptor s, t;
read_dimacs_max_flow(g, capacity, rev, s, t);
std::vector<Traits::edge_descriptor> pred(num_vertices(g));
long flow = edmonds_karp_max_flow
(g, s, t, capacity, residual_capacity, rev,
make_iterator_property_map(color_map.begin()),
&pred[0]);
std::cout << "c The total flow:" << std::endl;
std::cout << "s " << flow << std::endl << std::endl;
std::cout << "c flow values:" << std::endl;
graph_traits < Graph >::vertex_iterator u_iter, u_end;
graph_traits < Graph >::out_edge_iterator ei, e_end;
for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
if (capacity[*ei] > 0)
std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
<< (capacity[*ei] - residual_capacity[*ei]) << std::endl;
// if using the original example, unedited, this piece of code works
// BOOST_FOREACH(default_color_type x, color){
// std::cout << x << std::endl;
// }
return EXIT_SUCCESS;
}
提示将不胜感激。谢谢。
【问题讨论】:
-
According to the documentation,
color_map属性映射需要有一个顶点描述符作为键和一个颜色作为值。在您的示例中,您将这些颠倒了,因此它不起作用(当您只需要使用color_map时,您使用make_iterator_property_map也是错误的)。 -
@cv_and_he 原则上,BGL/Boost PropertyMap 的想法是,将选择的数据结构调整为“LvaluePropertyMap”应该是“容易的”。然而,对于这个提议的多映射,存在关键常量(需要代理对象来获得可变性)和(严重)低效率的问题。
-
@mje 考虑使用Boost BiMap 或只是转换数据。
标签: c++ algorithm boost graph minimum-cut