Re: [BGL] BFS and copy problem (was Re: [boost] Compilation)
Again, Luca, we can't help you unless you post the code (the complete C++ file). I can't just guess what is going wrong. You're probably forgetting to do something, and there's no way I can tell what that is based on what you've written below. On Thu, 24 Jan 2002, lucatoldo wrote: luca.t> I think there is something I do not know, rather important, about the luca.t> process of adding edges to the graph object. Infact, even in the luca.t> simple example/bfs.cpp if I swap the start with the ends, I get luca.t> different results. Namely: luca.t> luca.t> If I change the code from this luca.t> luca.t> boost::add_edge(0,2,G); luca.t> boost::add_edge(1,1,G); luca.t> boost::add_edge(3,1,G); luca.t> ... luca.t> boost::add_edge(4,1,G); luca.t> luca.t> to luca.t> luca.t> boost::add_edge(2,0,G); luca.t> boost::add_edge(1,1,G); luca.t> boost::add_edge(1,3,G); luca.t> ... luca.t> boost::add_edge(1,4,G); luca.t> luca.t> then I get luca.t> luca.t> the graph OK but the distances and the parent vectors are screwed: luca.t> luca.t> I get luca.t> luca.t> distances: 0 2 2 2 1 luca.t> instead of luca.t> distances: 0 2 1 2 2 luca.t> luca.t> and luca.t> luca.t> parent[0] = 0 luca.t> parent[1] = 4 luca.t> parent[2] = 4 luca.t> parent[3] = 4 luca.t> parent[4] = 0 luca.t> luca.t> instead of luca.t> luca.t> parent[0] = 0 luca.t> parent[1] = 2 luca.t> parent[2] = 0 luca.t> parent[3] = 2 luca.t> parent[4] = 2 luca.t> luca.t> I feel I am missing some basic knowledge on how to appropriately use luca.t> this powerful and amazingly fast system. I would love to learn the luca.t> missing bit. luca.t> luca.t> Looking forward your advice and sorry again for the dumminess of this luca.t> question. luca.t> luca.t> luca.t> Info: http://www.boost.org Send unsubscribe requests to: mailto:boost-unsubscribe@yahoogroups.com luca.t> luca.t> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ luca.t> luca.t> ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
// Sorry, I simply wanted to keep the bandwith small.
// here is the code, with the modification I made.
// I will appreciate your instructions on how to proceed.
// Regards and thanks
// Luca
//====================================================================
===
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// This file is part of the Boost Graph Library
//
// You should have received a copy of the License Agreement for the
// Boost Graph Library along with the software; see the file LICENSE.
// If not, contact Office of Research, University of Notre Dame, Notre
// Dame, IN 46556.
//
// Permission to modify the code and to distribute modified code is
// granted, provided the text of this NOTICE is retained, a notice
that
// the code was modified is included with the above COPYRIGHT NOTICE
and
// with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
// file is distributed with the modified code.
//
// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED.
// By way of example, but not limitation, Licensor MAKES NO
// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE
COMPONENTS
// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS,
TRADEMARKS
// OR OTHER RIGHTS.
//====================================================================
===
#include
Graph;
Graph G(5);
boost::add_edge(2, 0, G);
boost::add_edge(1, 1, G);
boost::add_edge(3, 1, G);
boost::add_edge(4, 1, G);
boost::add_edge(2, 1, G);
boost::add_edge(3, 2, G);
boost::add_edge(4, 2, G);
boost::add_edge(1, 3, G);
boost::add_edge(4, 3, G);
boost::add_edge(0, 4, G);
boost::add_edge(1, 4, G);
/*
boost::add_edge(0, 2, G);
boost::add_edge(1, 1, G);
boost::add_edge(1, 3, G);
boost::add_edge(1, 4, G);
boost::add_edge(2, 1, G);
boost::add_edge(2, 3, G);
boost::add_edge(2, 4, G);
boost::add_edge(3, 1, G);
boost::add_edge(3, 4, G);
boost::add_edge(4, 0, G);
boost::add_edge(4, 1, G);
*/
typedef Graph::vertex_descriptor Vertex;
Graph G_copy(5);
// Array to store predecessor (parent) of each vertex. This will be
// used as a Decorator (actually, its iterator will be).
std::vector<Vertex> p(boost::num_vertices(G));
// VC++ version of std::vector has no ::pointer, so
// I use ::value_type* instead.
typedef std::vector<Vertex>::value_type* Piter;
// Array to store distances from the source to each vertex . We use
// a built-in array here just for variety. This will also be used as
// a Decorator.
boost::graph_traits<Graph>::vertices_size_type d[5];
std::fill_n(d, 5, 0);
// The source vertex
Vertex s = *(boost::vertices(G).first);
p[s] = s;
boost::breadth_first_search
(G, s,
boost::visitor(boost::make_bfs_visitor
(std::make_pair(boost::record_distances(d, boost::on_tree_edge
()),
std::make_pair
(boost::record_predecessors(&p[0],
boost::on_tree_edge
()),
copy_graph(G_copy, boost::on_examine_edge
())))) ));
boost::print_graph(G);
boost::print_graph(G_copy);
if (boost::num_vertices(G) < 11) {
std::cout << "distances: ";
#ifdef BOOST_OLD_STREAM_ITERATORS
std::copy(d, d + 5, std::ostream_iterator
Hi Luca, I ran the code that you posted, and got the following output: 0 --> 4 1 --> 1 3 4 2 --> 0 1 3 --> 1 2 4 --> 1 2 3 0 --> 4 1 --> 1 3 4 2 --> 0 1 3 --> 1 2 4 --> 1 2 3 distances: 0 2 2 2 1 parent[0] = 0 parent[1] = 4 parent[2] = 4 parent[3] = 4 parent[4] = 0 The above output looks correct to me. It prints the graph G, then a copy of G, then the distances, which look right given the parent structure, which says that the BFS tree for this graph was 0 -> 4 4 -> 1 2 3 Did you expect different output? Or did the program you post not exhibit the problem you are having? Cheers, Jeremy ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
I would like to inform all users of the boost system that as expected was my mistake. Infact, the library and the bfs.cpp work exactely as to be expected given the semantic of the methods used. The very patient and creative Jeremy have explained me in the meantime that I was using the wrong class type for my need. If I use the undirectedS instead of the bidirectionalS in the typedef of the adjacency_list, then I get exactely the behaviour and the result that I needed/expected. The mistake I was doing was to assume that "bidirectionalS" and "undirectedS" be the same thing. Indeed they are not. Again, a mistake of a beginner with very limited knowledge in graphs and very limited knowledge in C++. Thanks to your patience I got my wheel running now, and it is a powerful stimulus for adding depth to my knowledge. For this reason, I am now looking forward your excellent Graph book that I ordered yesterday. I will post on this mailing list as soon as the paper (describing the study which also makes use of the boost library) is accepted for publication. Regards Luca
Thanks for sharing what you learned Luca. I'm sure others will run into the same problem. Also, to further explain the difference between a bidirectionalS, undirectedS, and directedS adjacency_list: The bidirectionalS type is for representing directed graphs and provides efficient access to both out-edges and in-edges. The undirectedS is for undirected graphs but also provides both in-edges and out-edges. The directedS is for directed graphs, and only provides access to out_edges. Cheers, Jeremy On Thu, 24 Jan 2002, lucatoldo wrote: luca.t> I would like to inform all users of the boost system that as expected luca.t> was my mistake. Infact, the library and the bfs.cpp work exactely as luca.t> to be expected given the semantic of the methods used. luca.t> luca.t> The very patient and creative Jeremy have explained me in the luca.t> meantime that I was using the wrong class type for my need. luca.t> luca.t> If I use the undirectedS instead of the bidirectionalS in the typedef luca.t> of the adjacency_list, then I get exactely the behaviour and the luca.t> result that I needed/expected. luca.t> luca.t> The mistake I was doing was to assume that "bidirectionalS" luca.t> and "undirectedS" be the same thing. Indeed they are not. luca.t> luca.t> Again, a mistake of a beginner with very limited knowledge in graphs luca.t> and very limited knowledge in C++. luca.t> luca.t> Thanks to your patience I got my wheel running now, and it is a luca.t> powerful stimulus for adding depth to my knowledge. For this reason, luca.t> I am now looking forward your excellent Graph book that I ordered luca.t> yesterday. luca.t> luca.t> I will post on this mailing list as soon as the paper (describing the luca.t> study which also makes use of the boost library) is accepted for luca.t> publication. luca.t> luca.t> Regards luca.t> luca.t> Luca ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
participants (2)
-
Jeremy Siek
-
lucatoldo