min_max_paths.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //=======================================================================
  2. // Copyright 1997-2001 University of Notre Dame.
  3. // Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #error "This example appears to be incorrect; it uses edge weights that are smaller than 0 using the comparison operator passed to Dijkstra's algorithm, which is not allowed."
  10. #include <boost/config.hpp>
  11. #include <iostream>
  12. #include <boost/graph/graph_traits.hpp>
  13. #include <boost/graph/graph_utility.hpp>
  14. #include <boost/graph/adjacency_list.hpp>
  15. #include <boost/graph/dijkstra_shortest_paths.hpp>
  16. #include <boost/graph/visitors.hpp>
  17. #include <boost/graph/transpose_graph.hpp>
  18. /* Output:
  19. distances from start vertex:
  20. distance(a) = 0
  21. distance(b) = 3
  22. distance(c) = 1
  23. distance(d) = 3
  24. distance(e) = 3
  25. min-max paths tree
  26. a --> c
  27. b -->
  28. c --> d
  29. d --> e
  30. e --> b
  31. */
  32. int
  33. main(int , char* [])
  34. {
  35. using namespace boost;
  36. typedef adjacency_list<listS, vecS, directedS,
  37. no_property, property<edge_weight_t, int> > Graph;
  38. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  39. typedef std::pair<int,int> E;
  40. const char name[] = "abcdef";
  41. const int num_nodes = 6;
  42. E edges[] = { E(0,2), E(1,1), E(1,3), E(1,4), E(2,1), E(2,3),
  43. E(3,4), E(4,0), E(4,1) };
  44. int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1};
  45. const int n_edges = sizeof(edges)/sizeof(E);
  46. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  47. // VC++ can't handle iterator constructors
  48. Graph G(num_nodes);
  49. property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
  50. for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j) {
  51. graph_traits<Graph>::edge_descriptor e; bool inserted;
  52. boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, G);
  53. weightmap[e] = weights[j];
  54. }
  55. #else
  56. Graph G(edges, edges + n_edges, weights, num_nodes);
  57. property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
  58. #endif
  59. std::vector<Vertex> p(num_vertices(G));
  60. std::vector<int> d(num_vertices(G));
  61. Vertex s = *(vertices(G).first);
  62. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  63. dijkstra_shortest_paths
  64. (G, s, &p[0], &d[0], weightmap, get(vertex_index, G),
  65. std::greater<int>(), closed_plus<int>(), (std::numeric_limits<int>::max)(), 0,
  66. default_dijkstra_visitor());
  67. #else
  68. dijkstra_shortest_paths
  69. (G, s, distance_map(&d[0]).
  70. predecessor_map(&p[0]).
  71. distance_compare(std::greater<int>()));
  72. #endif
  73. std::cout << "distances from start vertex:" << std::endl;
  74. graph_traits<Graph>::vertex_iterator vi, vend;
  75. for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi)
  76. std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << std::endl;
  77. std::cout << std::endl;
  78. std::cout << "min-max paths tree" << std::endl;
  79. adjacency_list<> tree(num_nodes);
  80. for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi)
  81. if (*vi != p[*vi])
  82. add_edge(p[*vi], *vi, tree);
  83. print_graph(tree, name);
  84. return 0;
  85. }