johnson-test.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //=======================================================================
  2. // Copyright 2002 Indiana University.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  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. /* Expected Output
  10. 0 1 2 3 4 5 6 7 8 9
  11. 0 0 99 111 123 103 107 125 105 111 123
  12. 1 99 0 12 24 4 8 26 6 12 24
  13. 2 111 12 0 12 16 20 24 18 24 26
  14. 3 123 24 12 0 28 30 12 30 26 14
  15. 4 103 4 16 28 0 4 22 2 8 20
  16. 5 107 8 20 30 4 0 18 6 4 16
  17. 6 125 26 24 12 22 18 0 24 14 2
  18. 7 105 6 18 30 2 6 24 0 10 22
  19. 8 111 12 24 26 8 4 14 10 0 12
  20. 9 123 24 26 14 20 16 2 22 12 0
  21. */
  22. #include <boost/config.hpp>
  23. #include <fstream>
  24. #include <iostream>
  25. #include <vector>
  26. #include <iomanip>
  27. #include <boost/property_map/property_map.hpp>
  28. #include <boost/graph/adjacency_list.hpp>
  29. #include <boost/graph/graphviz.hpp>
  30. #include <boost/graph/johnson_all_pairs_shortest.hpp>
  31. int main()
  32. {
  33. using namespace boost;
  34. typedef adjacency_list<vecS, vecS, undirectedS, no_property,
  35. property< edge_weight_t, int,
  36. property< edge_weight2_t, int > > > Graph;
  37. const int V = 10;
  38. typedef std::pair < int, int >Edge;
  39. Edge edge_array[] =
  40. { Edge(0,1), Edge(1,2), Edge(2,3), Edge(1,4), Edge(2,5), Edge(3,6),
  41. Edge(4,5), Edge(5,6), Edge(4,7), Edge(5,8), Edge(6,9), Edge(7,8),
  42. Edge(8,9)
  43. };
  44. const std::size_t E = sizeof(edge_array) / sizeof(Edge);
  45. Graph g(edge_array, edge_array + E, V);
  46. property_map < Graph, edge_weight_t >::type w = get(edge_weight, g);
  47. int weights[] = { 99, 12, 12, 4, 99, 12, 4, 99, 2, 4, 2, 99, 12 };
  48. int *wp = weights;
  49. graph_traits < Graph >::edge_iterator e, e_end;
  50. for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
  51. w[*e] = *wp++;
  52. std::vector < int >d(V, (std::numeric_limits < int >::max)());
  53. int D[V][V];
  54. johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0]));
  55. std::cout << std::setw(5) <<" ";
  56. for (int k = 0; k < 10; ++k)
  57. std::cout << std::setw(5) << k ;
  58. std::cout << std::endl;
  59. for (int i = 0; i < 10; ++i) {
  60. std::cout <<std::setw(5) << i ;
  61. for (int j = 0; j < 10; ++j) {
  62. std::cout << std::setw(5) << D[i][j] ;
  63. }
  64. std::cout << std::endl;
  65. }
  66. return 0;
  67. }