Update contrib.
1 //=======================================================================
2 // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
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 //=======================================================================
10 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
14 #include <boost/config.hpp>
18 #include <boost/graph/graph_traits.hpp>
19 #include <boost/graph/adjacency_list.hpp>
20 #include <boost/graph/dijkstra_shortest_paths.hpp>
23 #include "std_log_result.h"
24 #define LOG_FILENAME_LINE __FILE__, __LINE__
29 using namespace boost;
34 typedef adjacency_list < listS, vecS, directedS,
35 no_property, property < edge_weight_t, int > > graph_t;
36 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
37 typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
38 typedef std::pair<int, int> Edge;
40 const int num_nodes = 5;
41 enum nodes { A, B, C, D, E };
42 char name[] = "ABCDE";
43 Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
44 Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
46 int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
47 int num_arcs = sizeof(edge_array) / sizeof(Edge);
48 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
50 property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
51 for (std::size_t j = 0; j < num_arcs; ++j) {
52 edge_descriptor e; bool inserted;
53 tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
54 weightmap[e] = weights[j];
57 graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
58 property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
60 std::vector<vertex_descriptor> p(num_vertices(g));
61 std::vector<int> d(num_vertices(g));
62 vertex_descriptor s = vertex(A, g);
64 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
65 // VC++ has trouble with the named parameters mechanism
66 property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
67 dijkstra_shortest_paths(g, s, &p[0], &d[0], weightmap, indexmap,
68 std::less<int>(), closed_plus<int>(),
69 (std::numeric_limits<int>::max)(), 0,
70 default_dijkstra_visitor());
72 dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
75 std::cout << "distances and parents:" << std::endl;
76 graph_traits < graph_t >::vertex_iterator vi, vend;
77 for (tie(vi, vend) = vertices(g); vi != vend; ++vi) {
78 std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
79 std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
82 std::cout << std::endl;
84 std::ofstream dot_file("figs/dijkstra-eg.dot");
86 dot_file << "digraph D {\n"
89 << " ratio=\"fill\"\n"
90 << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
92 graph_traits < graph_t >::edge_iterator ei, ei_end;
93 for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
94 graph_traits < graph_t >::edge_descriptor e = *ei;
95 graph_traits < graph_t >::vertex_descriptor
96 u = source(e, g), v = target(e, g);
97 dot_file << name[u] << " -> " << name[v]
98 << "[label=\"" << get(weightmap, e) << "\"";
100 dot_file << ", color=\"black\"";
102 dot_file << ", color=\"grey\"";
108 std_log(LOG_FILENAME_LINE,"[End Test Case ]");
109 testResultXml("dijkstra-example");