dijkstra_shortest_paths with undirectedS example

Hello, I am new to BGL, I was trying to test dijkstra_shortest_paths() with undirectedS, the result is not what I wanted, anyone giving help? Thanks, source code like dijkstra-example.cpp in the graph/example. int main(int, char *[]) { typedef adjacency_list < listS, vecS, undirectedS, no_property, property < edge_weight_t, double > > graph_t; typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; typedef std::pair<int, int> Edge; const int num_nodes = 3; Edge edge_array[] = { Edge(0, 1),Edge(1, 2), Edge(2, 0) }; double weights[] = {2.0, 1.0, 1.0}; int num_arcs = sizeof(edge_array) / sizeof(Edge);// #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 graph_t g(num_nodes); property_map<graph_t, edge_weight_t>::type weightmap = get (edge_weight, g); for (std::size_t j = 0; j < num_arcs; ++j) { edge_descriptor e; bool inserted; tie(e, inserted) = add_edge(edge_array[j].first, edge_array [j].second, g); weightmap[e] = weights[j]; } #else graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); property_map<graph_t, edge_weight_t>::type weightmap = get (edge_weight, g); #endif std::vector<vertex_descriptor> pred(num_vertices(g)); std::vector<double> d(num_vertices(g)); vertex_descriptor s = vertex(0, g); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the named parameters mechanism property_map<graph_t, vertex_index_t>::type indexmap = get (vertex_index, g); dijkstra_shortest_paths(g, s, &pred[0], &d[0], weightmap, indexmap, std::less<double>(), closed_plus<double>(), std::numeric_limits<double>::max(), 0, default_dijkstra_visitor()); #else dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d [0])); #endif std::cout << "distances and parents:" << std::endl; graph_traits < graph_t >::vertex_iterator vi, vend; for (tie(vi, vend) = vertices(g); vi != vend; ++vi) { std::cout << "distance(" << *vi << ") = " << d[*vi] << ", "; std::cout << "parent(" << *vi << ") = " << pred[*vi] << std::endl; } std::cout << "min paths tree" << std::endl; adjacency_list<> tree(num_nodes); for(tie(vi,vend) = vertices(g); vi != vend; ++vi) if (*vi != pred[*vi]) add_edge(pred[*vi], *vi, tree); print_graph(tree); std::cout << std::endl; return EXIT_SUCCESS; } The result is : distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 0 distance(0) = 1, parent(2) = 0 min paths tree 0 -> 1 2 1 -> 2 -> I think the correct one should be: distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 2 distance(2) = 1, parent(2) = 0 min paths tree 0 -> 1 1 -> 2 2 -> Any idea what is wrong? thanks!

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi,
The result is : distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 0 distance(0) = 1, parent(2) = 0
According to your code, the last line should be distance(2) = 1, parent(2) = 0 Thr results are all correct. Why did you expect them to be different? You have an undirected Graph, and you can directly reach node 1 and 2 from node 0:
min paths tree 0 -> 1 2 1 -> 2 ->
I think the correct one should be: distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 2
Why that? Node 1 is directly connected with node 0 and the path via node 2 is not shorter.
distance(2) = 1, parent(2) = 0 min paths tree 0 -> 1 1 -> 2
Here again: see above.
2 ->
Michael - -- Dipl.-Ing. Michael Kettner, Wissenschaftlicher Mitarbeiter Institut für Verkehrswesen, Eisenbahnbau und -betrieb, Universitaet Hannover Appelstr. 9A # Tel: ++49/(0)511/762-4273 D-30167 Hannover # Fax: ++49/(0)511/762-3001 http://www.ive.uni-hannover.de # kettner@ive.uni-hannover.de -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE+pU5pkCdGnb0kVFMRAsOiAKCB5OfzNHHux/kj9M92h4TnFLSL3gCfdegJ QpU7QJwOU88UONIDHLgaumI= =qxUV -----END PGP SIGNATURE-----

Thanks, Michael, I knew that I misunderstand the problem, the results from the code is correct. O.K., let me explain my problem, and I would appreciate for any hints and help. Suppose that I have a number of points(node), I know their distance (weight), and I know the starting point, how could I get the shortest path passing all the these points or some of these points? Any algorithm I can use in BGL? Thanks! --- In Boost-Users@yahoogroups.com, Michael Kettner <kettner@i...> wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hi,
The result is : distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 0 distance(0) = 1, parent(2) = 0
According to your code, the last line should be distance(2) = 1, parent(2) = 0 Thr results are all correct. Why did you expect them to be different? You have an undirected Graph, and you can directly reach node 1 and 2 from node 0:
min paths tree 0 -> 1 2 1 -> 2 ->
I think the correct one should be: distances and parents: distance(0) = 0, parent(0) = 0 distance(1) = 2, parent(1) = 2
Why that? Node 1 is directly connected with node 0 and the path via node 2 is not shorter.
distance(2) = 1, parent(2) = 0 min paths tree 0 -> 1 1 -> 2
Here again: see above.
2 ->
Michael
- -- Dipl.-Ing. Michael Kettner, Wissenschaftlicher Mitarbeiter Institut für Verkehrswesen, Eisenbahnbau und -betrieb, Universitaet Hannover Appelstr. 9A # Tel: ++49/(0)511/762-4273 D-30167 Hannover # Fax: ++49/(0)511/762-3001 http://www.ive.uni-hannover.de # kettner@i... -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org
iD8DBQE+pU5pkCdGnb0kVFMRAsOiAKCB5OfzNHHux/kj9M92h4TnFLSL3gCfdegJ QpU7QJwOU88UONIDHLgaumI= =qxUV -----END PGP SIGNATURE-----

"jfw1000" <jfw1000@yahoo.com> writes:
Thanks, Michael, I knew that I misunderstand the problem, the results from the code is correct.
O.K., let me explain my problem, and I would appreciate for any hints and help.
Suppose that I have a number of points(node), I know their distance (weight), and I know the starting point, how could I get the shortest path passing all the these points or some of these points? Any algorithm I can use in BGL?
Hah! Classic travelling salesman problem! NP complete! Well, if you have a big graph any algorithm which actually finds the shortest path is going to be SLOW. However, there are some interesting heuristic algorithms out there which do pretty well. Just google for "travelling salesman" and have fun exploring. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams
-
jfw1000
-
Michael Kettner