johnson-eg.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <vector>
  12. #include <iomanip>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <boost/graph/adjacency_list.hpp>
  15. #include <boost/graph/graphviz.hpp>
  16. #include <boost/graph/johnson_all_pairs_shortest.hpp>
  17. int
  18. main()
  19. {
  20. using namespace boost;
  21. typedef adjacency_list<vecS, vecS, directedS, no_property,
  22. property< edge_weight_t, int, property< edge_weight2_t, int > > > Graph;
  23. const int V = 6;
  24. typedef std::pair < int, int >Edge;
  25. Edge edge_array[] =
  26. { Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(0, 5),
  27. Edge(1, 2), Edge(1, 5), Edge(1, 3), Edge(2, 4), Edge(2, 5),
  28. Edge(3, 2), Edge(4, 3), Edge(4, 1), Edge(5, 4)
  29. };
  30. const std::size_t E = sizeof(edge_array) / sizeof(Edge);
  31. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  32. // VC++ can't handle the iterator constructor
  33. Graph g(V);
  34. for (std::size_t j = 0; j < E; ++j)
  35. add_edge(edge_array[j].first, edge_array[j].second, g);
  36. #else
  37. Graph g(edge_array, edge_array + E, V);
  38. #endif
  39. property_map < Graph, edge_weight_t >::type w = get(edge_weight, g);
  40. int weights[] = { 0, 0, 0, 0, 0, 3, -4, 8, 1, 7, 4, -5, 2, 6 };
  41. int *wp = weights;
  42. graph_traits < Graph >::edge_iterator e, e_end;
  43. for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
  44. w[*e] = *wp++;
  45. std::vector < int >d(V, (std::numeric_limits < int >::max)());
  46. int D[V][V];
  47. johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0]));
  48. std::cout << " ";
  49. for (int k = 0; k < V; ++k)
  50. std::cout << std::setw(5) << k;
  51. std::cout << std::endl;
  52. for (int i = 0; i < V; ++i) {
  53. std::cout << std::setw(3) << i << " -> ";
  54. for (int j = 0; j < V; ++j) {
  55. if (D[i][j] == (std::numeric_limits<int>::max)())
  56. std::cout << std::setw(5) << "inf";
  57. else
  58. std::cout << std::setw(5) << D[i][j];
  59. }
  60. std::cout << std::endl;
  61. }
  62. std::ofstream fout("figs/johnson-eg.dot");
  63. fout << "digraph A {\n"
  64. << " rankdir=LR\n"
  65. << "size=\"5,3\"\n"
  66. << "ratio=\"fill\"\n"
  67. << "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n";
  68. graph_traits < Graph >::edge_iterator ei, ei_end;
  69. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  70. fout << source(*ei, g) << " -> " << target(*ei, g)
  71. << "[label=" << get(edge_weight, g)[*ei] << "]\n";
  72. fout << "}\n";
  73. return 0;
  74. }