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 "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."
11
12 #include <boost/config.hpp>
13 #include <iostream>
14
15 #include <boost/graph/graph_traits.hpp>
16 #include <boost/graph/graph_utility.hpp>
17 #include <boost/graph/adjacency_list.hpp>
18 #include <boost/graph/dijkstra_shortest_paths.hpp>
19 #include <boost/graph/visitors.hpp>
20 #include <boost/graph/transpose_graph.hpp>
21
22 /* Output:
23
24 distances from start vertex:
25 distance(a) = 0
26 distance(b) = 3
27 distance(c) = 1
28 distance(d) = 3
29 distance(e) = 3
30
31 min-max paths tree
32 a --> c
33 b -->
34 c --> d
35 d --> e
36 e --> b
37
38 */
39
40 int
main(int,char * [])41 main(int , char* [])
42 {
43 using namespace boost;
44
45 typedef adjacency_list<listS, vecS, directedS,
46 no_property, property<edge_weight_t, int> > Graph;
47 typedef graph_traits<Graph>::vertex_descriptor Vertex;
48
49 typedef std::pair<int,int> E;
50
51 const char name[] = "abcdef";
52
53 const int num_nodes = 6;
54 E edges[] = { E(0,2), E(1,1), E(1,3), E(1,4), E(2,1), E(2,3),
55 E(3,4), E(4,0), E(4,1) };
56 int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1};
57 const int n_edges = sizeof(edges)/sizeof(E);
58 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
59 // VC++ can't handle iterator constructors
60 Graph G(num_nodes);
61 property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
62 for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j) {
63 graph_traits<Graph>::edge_descriptor e; bool inserted;
64 boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, G);
65 weightmap[e] = weights[j];
66 }
67 #else
68 Graph G(edges, edges + n_edges, weights, num_nodes);
69 property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
70 #endif
71
72 std::vector<Vertex> p(num_vertices(G));
73 std::vector<int> d(num_vertices(G));
74
75 Vertex s = *(vertices(G).first);
76
77 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
78 dijkstra_shortest_paths
79 (G, s, &p[0], &d[0], weightmap, get(vertex_index, G),
80 std::greater<int>(), closed_plus<int>(), (std::numeric_limits<int>::max)(), 0,
81 default_dijkstra_visitor());
82 #else
83 dijkstra_shortest_paths
84 (G, s, distance_map(&d[0]).
85 predecessor_map(&p[0]).
86 distance_compare(std::greater<int>()));
87 #endif
88
89 std::cout << "distances from start vertex:" << std::endl;
90 graph_traits<Graph>::vertex_iterator vi, vend;
91 for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi)
92 std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << std::endl;
93 std::cout << std::endl;
94
95 std::cout << "min-max paths tree" << std::endl;
96 adjacency_list<> tree(num_nodes);
97
98 for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi)
99 if (*vi != p[*vi])
100 add_edge(p[*vi], *vi, tree);
101
102 print_graph(tree, name);
103
104 return 0;
105 }
106