dijkstra_no_color_map_compare.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //=======================================================================
  2. // Copyright 2009 Trustees of Indiana University.
  3. // Authors: Michael Hansen
  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. #include <iostream>
  10. #include <map>
  11. #include <vector>
  12. #include <boost/config.hpp>
  13. #ifdef BOOST_MSVC
  14. // Without disabling this we get hard errors about initialialized pointers:
  15. #pragma warning(disable:4703)
  16. #endif
  17. #include <boost/lexical_cast.hpp>
  18. #include <boost/random.hpp>
  19. #include <boost/graph/adjacency_list.hpp>
  20. #include <boost/graph/dijkstra_shortest_paths.hpp>
  21. #include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
  22. #include <boost/graph/properties.hpp>
  23. #include <boost/graph/random.hpp>
  24. #include <boost/test/minimal.hpp>
  25. #include <boost/graph/iteration_macros.hpp>
  26. #define INITIALIZE_VERTEX 0
  27. #define DISCOVER_VERTEX 1
  28. #define EXAMINE_VERTEX 2
  29. #define EXAMINE_EDGE 3
  30. #define EDGE_RELAXED 4
  31. #define EDGE_NOT_RELAXED 5
  32. #define FINISH_VERTEX 6
  33. template <typename Graph>
  34. void run_dijkstra_test(const Graph& graph)
  35. {
  36. using namespace boost;
  37. // Set up property maps
  38. typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
  39. typedef typename std::map<vertex_t, vertex_t> vertex_map_t;
  40. typedef associative_property_map<vertex_map_t> predecessor_map_t;
  41. vertex_map_t default_vertex_map, no_color_map_vertex_map;
  42. predecessor_map_t default_predecessor_map(default_vertex_map),
  43. no_color_map_predecessor_map(no_color_map_vertex_map);
  44. typedef typename std::map<vertex_t, double> vertex_double_map_t;
  45. typedef associative_property_map<vertex_double_map_t> distance_map_t;
  46. vertex_double_map_t default_vertex_double_map, no_color_map_vertex_double_map;
  47. distance_map_t default_distance_map(default_vertex_double_map),
  48. no_color_map_distance_map(no_color_map_vertex_double_map);
  49. // Run dijkstra algoirthms
  50. dijkstra_shortest_paths(graph, vertex(0, graph),
  51. predecessor_map(default_predecessor_map)
  52. .distance_map(default_distance_map));
  53. dijkstra_shortest_paths_no_color_map(graph, vertex(0, graph),
  54. predecessor_map(no_color_map_predecessor_map)
  55. .distance_map(no_color_map_distance_map));
  56. // Verify that predecessor maps are equal
  57. BOOST_CHECK(std::equal(default_vertex_map.begin(), default_vertex_map.end(),
  58. no_color_map_vertex_map.begin()));
  59. // Verify that distance maps are equal
  60. BOOST_CHECK(std::equal(default_vertex_double_map.begin(), default_vertex_double_map.end(),
  61. no_color_map_vertex_double_map.begin()));
  62. }
  63. int test_main(int argc, char* argv[])
  64. {
  65. using namespace boost;
  66. int vertices_to_create = 10;
  67. int edges_to_create = 500;
  68. std::size_t random_seed = time(0);
  69. if (argc > 1) {
  70. vertices_to_create = lexical_cast<int>(argv[1]);
  71. }
  72. if (argc > 2) {
  73. edges_to_create = lexical_cast<int>(argv[2]);
  74. }
  75. if (argc > 3) {
  76. random_seed = lexical_cast<std::size_t>(argv[3]);
  77. }
  78. minstd_rand generator(random_seed);
  79. // Set up graph
  80. typedef adjacency_list<listS, listS, directedS,
  81. property<vertex_index_t, int >,
  82. property<edge_weight_t, double> > graph_t;
  83. graph_t graph;
  84. generate_random_graph(graph, vertices_to_create, edges_to_create, generator);
  85. // Set up property maps
  86. typedef property_map<graph_t, vertex_index_t>::type index_map_t;
  87. index_map_t index_map = get(vertex_index, graph);
  88. int vertex_index = 0;
  89. BGL_FORALL_VERTICES(current_vertex, graph, graph_t) {
  90. put(index_map, current_vertex, vertex_index++);
  91. }
  92. randomize_property<edge_weight_t>(graph, generator);
  93. // Run comparison test with original dijkstra_shortest_paths
  94. std::cout << "Running dijkstra shortest paths test with " << num_vertices(graph) <<
  95. " vertices and " << num_edges(graph) << " edges " << std::endl;
  96. run_dijkstra_test(graph);
  97. return 0;
  98. }