| 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 <iomanip> |
|---|
| 12 | #include <vector> |
|---|
| 13 | #include <boost/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 | |
|---|
| 18 | int |
|---|
| 19 | main() |
|---|
| 20 | { |
|---|
| 21 | using namespace boost; |
|---|
| 22 | typedef adjacency_list<vecS, vecS, directedS, no_property, |
|---|
| 23 | property< edge_weight_t, int, property< edge_weight2_t, int > > > Graph; |
|---|
| 24 | const int V = 5; |
|---|
| 25 | typedef std::pair < int, int >Edge; |
|---|
| 26 | Edge edge_array[] = |
|---|
| 27 | { Edge(0, 1), Edge(0, 4), Edge(0, 2), Edge(1, 3), Edge(1, 4), |
|---|
| 28 | Edge(2, 1), Edge(3, 2), Edge(3, 0), Edge(4, 3) |
|---|
| 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 | |
|---|
| 40 | property_map < Graph, edge_weight_t >::type w = get(edge_weight, g); |
|---|
| 41 | int weights[] = { 3, -4, 8, 1, 7, 4, -5, 2, 6 }; |
|---|
| 42 | int *wp = weights; |
|---|
| 43 | |
|---|
| 44 | graph_traits < Graph >::edge_iterator e, e_end; |
|---|
| 45 | for (boost::tie(e, e_end) = edges(g); e != e_end; ++e) |
|---|
| 46 | w[*e] = *wp++; |
|---|
| 47 | |
|---|
| 48 | std::vector < int >d(V, (std::numeric_limits < int >::max)()); |
|---|
| 49 | int D[V][V]; |
|---|
| 50 | johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0])); |
|---|
| 51 | |
|---|
| 52 | std::cout << " "; |
|---|
| 53 | for (int k = 0; k < V; ++k) |
|---|
| 54 | std::cout << std::setw(5) << k; |
|---|
| 55 | std::cout << std::endl; |
|---|
| 56 | for (int i = 0; i < V; ++i) { |
|---|
| 57 | std::cout << i << " -> "; |
|---|
| 58 | for (int j = 0; j < V; ++j) { |
|---|
| 59 | if (D[i][j] > 20 || D[i][j] < -20) |
|---|
| 60 | std::cout << std::setw(5) << "inf"; |
|---|
| 61 | else |
|---|
| 62 | std::cout << std::setw(5) << D[i][j]; |
|---|
| 63 | } |
|---|
| 64 | std::cout << std::endl; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | std::ofstream fout("figs/johnson-eg.dot"); |
|---|
| 68 | fout << "digraph A {\n" |
|---|
| 69 | << " rankdir=LR\n" |
|---|
| 70 | << "size=\"5,3\"\n" |
|---|
| 71 | << "ratio=\"fill\"\n" |
|---|
| 72 | << "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n"; |
|---|
| 73 | |
|---|
| 74 | graph_traits < Graph >::edge_iterator ei, ei_end; |
|---|
| 75 | for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) |
|---|
| 76 | fout << source(*ei, g) << " -> " << target(*ei, g) |
|---|
| 77 | << "[label=" << get(edge_weight, g)[*ei] << "]\n"; |
|---|
| 78 | |
|---|
| 79 | fout << "}\n"; |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|