how can I read data from file in boost library

1.4k views Asked by At

I am using boost library to find maximum flow (push relabel) , and there is a file read_dimacs.hpp read the data but stdin. the problem is my data file is too big and i want to read data file in direct way from file . Can any one help me.the code is below

#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;
}
1

There are 1 answers

3
sehe On

You can

  1. store the underlying container in shared memory/memory mapped file.

    An example of this is here: Using boost::iostreams::mapped_file_source with std::multimap

    Of course you'll need to tie it in with Boost Graph. If you post with a SSCCE, I could see whether it's easy to adapt

  2. you could distribute the load by using Boost Parallel Graph library

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

    I don't have experience with this, enough, to know whether it makes sense to use with your algoritm needs. (I know you can still compile it, but it might be suboptimal if the algorithm essentially runs in one process, pulling in the data. Memory mapped files would be way faster there)