• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=======================================================================
2 // Copyright 1997-2001 University of Notre Dame.
3 // Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 
10 #error \
11     "This example appears to be incorrect; it uses edge weights that are smaller than 0 using the comparison operator passed to Dijkstra's algorithm, which is not allowed."
12 
13 #include <boost/config.hpp>
14 #include <iostream>
15 
16 #include <boost/graph/graph_traits.hpp>
17 #include <boost/graph/graph_utility.hpp>
18 #include <boost/graph/adjacency_list.hpp>
19 #include <boost/graph/dijkstra_shortest_paths.hpp>
20 #include <boost/graph/visitors.hpp>
21 #include <boost/graph/transpose_graph.hpp>
22 
23 /* Output:
24 
25   distances from start vertex:
26   distance(a) = 0
27   distance(b) = 3
28   distance(c) = 1
29   distance(d) = 3
30   distance(e) = 3
31 
32   min-max paths tree
33   a --> c
34   b -->
35   c --> d
36   d --> e
37   e --> b
38 
39 */
40 
main(int,char * [])41 int main(int, char*[])
42 {
43     using namespace boost;
44 
45     typedef adjacency_list< listS, vecS, directedS, no_property,
46         property< edge_weight_t, int > >
47         Graph;
48     typedef graph_traits< Graph >::vertex_descriptor Vertex;
49 
50     typedef std::pair< int, int > E;
51 
52     const char name[] = "abcdef";
53 
54     const int num_nodes = 6;
55     E edges[] = { E(0, 2), E(1, 1), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4),
56         E(4, 0), E(4, 1) };
57     int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
58     const int n_edges = sizeof(edges) / sizeof(E);
59 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
60     // VC++ can't handle iterator constructors
61     Graph G(num_nodes);
62     property_map< Graph, edge_weight_t >::type weightmap = get(edge_weight, G);
63     for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j)
64     {
65         graph_traits< Graph >::edge_descriptor e;
66         bool inserted;
67         boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, G);
68         weightmap[e] = weights[j];
69     }
70 #else
71     Graph G(edges, edges + n_edges, weights, num_nodes);
72     property_map< Graph, edge_weight_t >::type weightmap = get(edge_weight, G);
73 #endif
74 
75     std::vector< Vertex > p(num_vertices(G));
76     std::vector< int > d(num_vertices(G));
77 
78     Vertex s = *(vertices(G).first);
79 
80 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
81     dijkstra_shortest_paths(G, s, &p[0], &d[0], weightmap, get(vertex_index, G),
82         std::greater< int >(), closed_plus< int >(),
83         (std::numeric_limits< int >::max)(), 0, default_dijkstra_visitor());
84 #else
85     dijkstra_shortest_paths(G, s,
86         distance_map(&d[0]).predecessor_map(&p[0]).distance_compare(
87             std::greater< int >()));
88 #endif
89 
90     std::cout << "distances from start vertex:" << std::endl;
91     graph_traits< Graph >::vertex_iterator vi, vend;
92     for (boost::tie(vi, vend) = vertices(G); vi != vend; ++vi)
93         std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << std::endl;
94     std::cout << std::endl;
95 
96     std::cout << "min-max paths tree" << std::endl;
97     adjacency_list<> tree(num_nodes);
98 
99     for (boost::tie(vi, vend) = vertices(G); vi != vend; ++vi)
100         if (*vi != p[*vi])
101             add_edge(p[*vi], *vi, tree);
102 
103     print_graph(tree, name);
104 
105     return 0;
106 }
107