1 //=======================================================================
2 // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
3 // Copyright 2009 Trustees of Indiana University.
4 // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // NOTE: Based off of dijkstra-example.cpp
11 //=======================================================================
12 #include <boost/config.hpp>
16 #include <boost/graph/graph_traits.hpp>
17 #include <boost/graph/adjacency_list.hpp>
18 #include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
20 using namespace boost;
25 typedef adjacency_list < listS, vecS, directedS,
26 no_property, property < edge_weight_t, int > > graph_t;
27 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
28 typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
29 typedef std::pair<int, int> Edge;
31 const int num_nodes = 5;
32 enum nodes { A, B, C, D, E };
33 char name[] = "ABCDE";
34 Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
35 Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
37 int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
38 int num_arcs = sizeof(edge_array) / sizeof(Edge);
39 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
41 property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
42 for (std::size_t j = 0; j < num_arcs; ++j) {
43 edge_descriptor e; bool inserted;
44 boost::tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
45 weightmap[e] = weights[j];
48 graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
49 property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
51 std::vector<vertex_descriptor> p(num_vertices(g));
52 std::vector<int> d(num_vertices(g));
53 vertex_descriptor s = vertex(A, g);
55 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
56 // VC++ has trouble with the named parameters mechanism
57 property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
58 dijkstra_shortest_paths_no_color_map(g, s, &p[0], &d[0], weightmap,
59 indexmap, std::less<int>(),
61 (std::numeric_limits<int>::max)(), 0,
62 default_dijkstra_visitor());
64 dijkstra_shortest_paths_no_color_map(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
67 std::cout << "distances and parents:" << std::endl;
68 graph_traits < graph_t >::vertex_iterator vi, vend;
69 for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
70 std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
71 std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
74 std::cout << std::endl;
76 std::ofstream dot_file("figs/dijkstra-no-color-map-eg.dot");
78 dot_file << "digraph D {\n"
81 << " ratio=\"fill\"\n"
82 << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
84 graph_traits < graph_t >::edge_iterator ei, ei_end;
85 for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
86 graph_traits < graph_t >::edge_descriptor e = *ei;
87 graph_traits < graph_t >::vertex_descriptor
88 u = source(e, g), v = target(e, g);
89 dot_file << name[u] << " -> " << name[v]
90 << "[label=\"" << get(weightmap, e) << "\"";
92 dot_file << ", color=\"black\"";
94 dot_file << ", color=\"grey\"";