473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BFS in Boost graph library

2 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <boost/graph/depth_first_search.hpp>
  3. #include <fstream>
  4. #include <utility>
  5. #include <boost/graph/breadth_first_search.hpp>
  6. #include <algorithm>
  7.  
  8. namespace std {
  9.  
  10.   using namespace boost;
  11.  
  12.   template <typename T>
  13.   std::istream& operator>> (std::istream& in, std::pair<T,T>& p) {
  14.      in >> p.first >> p.second;
  15.      return in;
  16.   }
  17.  
  18.   class graph_out : public default_bfs_visitor {
  19.     public:
  20.     graph_out() : ecounter(0) {}
  21.  
  22.     template <typename Edge, typename Graph>
  23.     void tree_edge(Edge u, const Graph&) {
  24.        std::cout << "discovered an edge" << endl;
  25.        std::cout << ecounter++ << u;
  26.     }
  27.  
  28.     private:
  29.     int ecounter;
  30.  
  31.   };
  32.  
  33. }
  34.  
  35.  
  36.  
  37. int main(){
  38.   using namespace std;
  39.   using namespace boost;
  40.   typedef adjacency_list <listS, vecS, directedS> file_dep_graph;
  41.  
  42.   std::ifstream file_in("directed_graph.dat");
  43.   typedef graph_traits<file_dep_graph>::vertices_size_type size_type;
  44.   size_type n_vertices;
  45.  
  46.   if(file_in >> n_vertices){
  47.     std::cout << n_vertices << endl; // read in number of vertices
  48.   }
  49.  
  50.   istream_iterator<std::pair<size_type, size_type> >
  51.   input_begin(file_in), input_end;
  52.  
  53.   file_dep_graph g(input_begin, input_end, n_vertices);
  54.  
  55.   graph_out vis;
  56.   graph_traits<file_dep_graph>::vertex_descriptor a = *vertices(g).first;
  57.   breadth_first_search(g,a,visitor(vis));
  58.  
  59. }
The directed_graph. dat file is:

Expand|Select|Wrap|Line Numbers
  1. 14
  2. (1,2)
  3. (2,3)
  4. (3,4)
  5. (3,5)
  6. (4,6)
  7. (6,7)
  8. (7,8)
  9. (7,9)
  10. (7,10)
  11. (8,11)
  12. (9,12)
  13. (12,13)
  14. (13,14)
Please dont suggest me any alternatives, such as without using a file etc as I need to do it this way only.
Oct 15 '08 #1
1 3157
rboorgapally
2 New Member
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.
Oct 15 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

205
10540
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
2
1520
by: nin234 | last post by:
My apologies in advance if any one is offended by this post. I have an unstable job in a start up company and thought of sharpening my skill sets. I noticed that many companies are asking for boost and austria C++ library knowledge. I am thinking of using that in my project/learning them. For the past 20 months , I developed a fairly complex real time multi threaded (Posix threads) daemon in C++ using Berkeley DB, sockets, C++ concepts...
1
7923
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right version of the MS Graph type library. Up until now I have been walking them through the process of setting the references to include their version of MS Graph library. My problem is that I can not seem to get the syntax correct .. or perhaps...
3
7823
by: Jef Driesen | last post by:
I'm trying to create a graph from an image, where pixel values are regions labels. I have defined my graph to use lists instead of the vectors (because I need to add/remove vertices and edges) and one extra property for the vertex (the region label): typedef boost::adjacency_list< boost::listS, // Adjacency list boost::listS, // Vertex list boost::undirectedS, // Undirected graph unsigned int, // Vertex property...
1
1474
by: George Sakkis | last post by:
I've just started toying with the python bindings of BGL and I'm puzzled from the following: True False It seems that the vertices iterator creates new vertex objects every time instead of iterating over the existing ones. This essentially prevents, among other things, storing vertices as keys in a dictionary since the hashes of the stored and the new vertex differ although they
1
3197
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray, but have an unused value in e.g. a distance map, e.g. -1, to which the map can be initialised. So I tried to write a map adapter which can be passed such a map together with the value indicating "white". This adapter will return
0
2118
by: Tim Frink | last post by:
Hi, I want to use the depth_first_search algorithm from the Boost Graph Library. However, I've problems with the definition of my own ColorMap. To illustrate this, here is an example (taken from http://www.boost.org/libs/graph/example/dfs-example.cpp): int main() {
4
2544
by: Man4ish | last post by:
namespace ve/////////////////ve.h { struct VertexProperties { std::size_t index; boost::default_color_type color; }; } /////////////////////////////////////////////////////////////////////////////////////////////////// namespace ed///////////////////////ed.h
2
2303
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include <boost/graph/adjacency_list.hpp> #include <boost/tuple/tuple.hpp> #include <set> using namespace std;
0
8424
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8415
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.