【问题标题】:how can I read data from file in boost library如何从 boost 库中的文件中读取数据
【发布时间】:2015-02-01 10:51:18
【问题描述】:

我正在使用 boost 库来查找最大流量 (push relabel) ,并且有一个文件 read_dimacs.hpp 读取数据但 stdin。问题是我的数据文件太大,我想直接从文件中读取数据文件。 谁能帮帮我。代码在下面

#include <boost/config.hpp>
#include <iostream>
#include <string>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/read_dimacs.hpp>

int
main()
{
  using namespace boost;

  typedef adjacency_list_traits<vecS, vecS, directedS> Traits;
  typedef adjacency_list<vecS, 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;
  long flow;

  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);

  Traits::vertex_descriptor s, t;
  read_dimacs_max_flow(g, capacity, rev, s, t);

  flow = push_relabel_max_flow(g, s, t);

  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;
  system("pause");
  return 0;
}

【问题讨论】:

  • 标准输入没有大小限制。你可以做./myprog &lt; thefile,输入的大小没有区别。也许我没有很好地理解你的问题?
  • “我的数据文件太大”是什么意思?您的系统是否内存不足(抛出std::bad_alloc)?对于较小的输入,您的程序是否按预期工作?你能展示一下它是如何做到的吗?
  • 我的数据文件作为 dimacs.dat 文件作为节点和弧超过 1000 个顶点因此我如何在算法中读取它?该算法将数据读取为标准输入,我希望该算法从我的文件中读取数据。
  • 我有数据作为 dimacs.dat 文件,我想找到任意两个顶点之间的最大流量,我希望 push_relabel 算法在我的文件中读取此数据,但该算法将数据作为标准输入读取,而不是从我的文件中读取。我可以对算法进行一些更改以读取我的数据吗?

标签: c++ boost graph push-relabel


【解决方案1】:

你可以

  1. 将底层容器存储在共享内存/内存映射文件中。

    这里有一个例子:Using boost::iostreams::mapped_file_source with std::multimap

    当然,您需要将其与 Boost Graph 结合使用。如果你用 SSCCE 发帖,我可以看看它是否容易适应

  2. 您可以使用 Boost Parallel Graph 库来分配负载

    http://www.boost.org/doc/libs/1_57_0/libs/graph_parallel/doc/html/index.html

    我没有这方面的经验,不足以知道与您的算法需求一起使用是否有意义。 (我知道你仍然可以编译它,但如果算法本质上在一个进程中运行,拉入数据,它可能不是最优的。内存映射文件会更快)

【讨论】:

  • 我有数据作为 dimacs.dat 文件,我想找到任意两个顶点之间的最大流量,我希望 push_relabel 算法在我的文件中读取此数据,但该算法将数据作为标准输入读取,而不是从我的文件中读取。我可以对算法进行一些更改以读取我的数据吗?
  • @HaederALjoburey 看到这个:“如果你用 SSCCE 发帖,我可以看看它是否容易适应”
  • 这很容易用谷歌搜索。 sscce.orgmeta.stackexchange.com/a/22762/159703
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
相关资源
最近更新 更多