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