BFS in Boost graph library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rboorgapally
    New Member
    • Oct 2008
    • 2

    BFS in Boost graph library

    Hi everyone,
    I am trying to run BFS on a graph that is created with input from a file. I am facing problems with the output though the code is compiling:

    Code:
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/depth_first_search.hpp>
    #include <fstream>
    #include <utility>
    #include <boost/graph/breadth_first_search.hpp>
    #include <algorithm>
    
    namespace std {
    
      using namespace boost;
    
      template <typename T>
      std::istream& operator>> (std::istream& in, std::pair<T,T>& p) {
         in >> p.first >> p.second;
         return in;
      }
    
      class graph_out : public default_bfs_visitor {
        public:
        graph_out() : ecounter(0) {}
    
        template <typename Edge, typename Graph>
        void tree_edge(Edge u, const Graph&) {
           std::cout << "discovered an edge" << endl;
           std::cout << ecounter++ << u;
        }
    
        private:
        int ecounter;
    
      };
    
    }
    
    
    
    int main(){
      using namespace std;
      using namespace boost;
      typedef adjacency_list <listS, vecS, directedS> file_dep_graph;
    
      std::ifstream file_in("directed_graph.dat");
      typedef graph_traits<file_dep_graph>::vertices_size_type size_type;
      size_type n_vertices;
    
      if(file_in >> n_vertices){
        std::cout << n_vertices << endl; // read in number of vertices
      }
    
      istream_iterator<std::pair<size_type, size_type> >
      input_begin(file_in), input_end;
    
      file_dep_graph g(input_begin, input_end, n_vertices);
    
      graph_out vis;
      graph_traits<file_dep_graph>::vertex_descriptor a = *vertices(g).first;
      breadth_first_search(g,a,visitor(vis));
    
    }
    The directed_graph. dat file is:

    Code:
    14
    (1,2)
    (2,3)
    (3,4)
    (3,5)
    (4,6)
    (6,7)
    (7,8)
    (7,9)
    (7,10)
    (8,11)
    (9,12)
    (12,13)
    (13,14)
    Please dont suggest me any alternatives, such as without using a file etc as I need to do it this way only.
  • rboorgapally
    New Member
    • Oct 2008
    • 2

    #2
    Hi everyone,
    I found the answer :)
    I entered the values in the directed_graph. dat file wrongly.
    We shouldn't enter the values in pairs.
    We should do so in inegers.

    Comment

    Working...