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