| 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 <vector> |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #include <fstream> |
|---|
| 12 | #include <iomanip> |
|---|
| 13 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 14 | #include <boost/graph/bellman_ford_shortest_paths.hpp> |
|---|
| 15 | |
|---|
| 16 | using namespace boost; |
|---|
| 17 | |
|---|
| 18 | template < typename Graph, typename ParentMap > |
|---|
| 19 | struct edge_writer |
|---|
| 20 | { |
|---|
| 21 | edge_writer(const Graph & g, const ParentMap & p) |
|---|
| 22 | : m_g(g), m_parent(p) |
|---|
| 23 | { |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | template < typename Edge > |
|---|
| 27 | void operator() (std::ostream & out, const Edge & e) const |
|---|
| 28 | { |
|---|
| 29 | out << "[label=\"" << get(edge_weight, m_g, e) << "\""; |
|---|
| 30 | typename graph_traits < Graph >::vertex_descriptor |
|---|
| 31 | u = source(e, m_g), v = target(e, m_g); |
|---|
| 32 | if (m_parent[v] == u) |
|---|
| 33 | out << ", color=\"black\""; |
|---|
| 34 | else |
|---|
| 35 | out << ", color=\"grey\""; |
|---|
| 36 | out << "]"; |
|---|
| 37 | } |
|---|
| 38 | const Graph & m_g; |
|---|
| 39 | ParentMap m_parent; |
|---|
| 40 | }; |
|---|
| 41 | template < typename Graph, typename Parent > |
|---|
| 42 | edge_writer < Graph, Parent > |
|---|
| 43 | make_edge_writer(const Graph & g, const Parent & p) |
|---|
| 44 | { |
|---|
| 45 | return edge_writer < Graph, Parent > (g, p); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | struct EdgeProperties { |
|---|
| 49 | int weight; |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | int |
|---|
| 53 | main() |
|---|
| 54 | { |
|---|
| 55 | enum { u, v, x, y, z, N }; |
|---|
| 56 | char name[] = { 'u', 'v', 'x', 'y', 'z' }; |
|---|
| 57 | typedef std::pair < int, int >E; |
|---|
| 58 | const int n_edges = 10; |
|---|
| 59 | E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u), |
|---|
| 60 | E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z,x) }; |
|---|
| 61 | int weight[n_edges] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 }; |
|---|
| 62 | |
|---|
| 63 | typedef adjacency_list < vecS, vecS, directedS, |
|---|
| 64 | no_property, EdgeProperties> Graph; |
|---|
| 65 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
|---|
| 66 | // VC++ can't handle the iterator constructor |
|---|
| 67 | Graph g(N); |
|---|
| 68 | for (std::size_t j = 0; j < n_edges; ++j) |
|---|
| 69 | add_edge(edge_array[j].first, edge_array[j].second, g); |
|---|
| 70 | #else |
|---|
| 71 | Graph g(edge_array, edge_array + n_edges, N); |
|---|
| 72 | #endif |
|---|
| 73 | graph_traits < Graph >::edge_iterator ei, ei_end; |
|---|
| 74 | property_map<Graph, int EdgeProperties::*>::type |
|---|
| 75 | weight_pmap = get(&EdgeProperties::weight, g); |
|---|
| 76 | int i = 0; |
|---|
| 77 | for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i) |
|---|
| 78 | weight_pmap[*ei] = weight[i]; |
|---|
| 79 | |
|---|
| 80 | std::vector<int> distance(N, (std::numeric_limits < short >::max)()); |
|---|
| 81 | std::vector<std::size_t> parent(N); |
|---|
| 82 | for (i = 0; i < N; ++i) |
|---|
| 83 | parent[i] = i; |
|---|
| 84 | distance[z] = 0; |
|---|
| 85 | |
|---|
| 86 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
|---|
| 87 | bool r = bellman_ford_shortest_paths |
|---|
| 88 | (g, int(N), weight_pmap, &parent[0], &distance[0], |
|---|
| 89 | closed_plus<int>(), std::less<int>(), default_bellman_visitor()); |
|---|
| 90 | #else |
|---|
| 91 | bool r = bellman_ford_shortest_paths |
|---|
| 92 | (g, int (N), weight_map(weight_pmap).distance_map(&distance[0]). |
|---|
| 93 | predecessor_map(&parent[0])); |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | if (r) |
|---|
| 97 | for (i = 0; i < N; ++i) |
|---|
| 98 | std::cout << name[i] << ": " << std::setw(3) << distance[i] |
|---|
| 99 | << " " << name[parent[i]] << std::endl; |
|---|
| 100 | else |
|---|
| 101 | std::cout << "negative cycle" << std::endl; |
|---|
| 102 | |
|---|
| 103 | std::ofstream dot_file("figs/bellman-eg.dot"); |
|---|
| 104 | dot_file << "digraph D {\n" |
|---|
| 105 | << " rankdir=LR\n" |
|---|
| 106 | << " size=\"5,3\"\n" |
|---|
| 107 | << " ratio=\"fill\"\n" |
|---|
| 108 | << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; |
|---|
| 109 | |
|---|
| 110 | { |
|---|
| 111 | for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { |
|---|
| 112 | graph_traits < Graph >::edge_descriptor e = *ei; |
|---|
| 113 | graph_traits < Graph >::vertex_descriptor |
|---|
| 114 | u = source(e, g), v = target(e, g); |
|---|
| 115 | // VC++ doesn't like the 3-argument get function, so here |
|---|
| 116 | // we workaround by using 2-nested get()'s. |
|---|
| 117 | dot_file << name[u] << " -> " << name[v] |
|---|
| 118 | << "[label=\"" << get(get(&EdgeProperties::weight, g), e) << "\""; |
|---|
| 119 | if (parent[v] == u) |
|---|
| 120 | dot_file << ", color=\"black\""; |
|---|
| 121 | else |
|---|
| 122 | dot_file << ", color=\"grey\""; |
|---|
| 123 | dot_file << "]"; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | dot_file << "}"; |
|---|
| 127 | return EXIT_SUCCESS; |
|---|
| 128 | } |
|---|