|
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 |
|
9 /* |
|
10 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. |
|
11 */ |
|
12 |
|
13 |
|
14 #include <boost/config.hpp> |
|
15 #include <iostream> |
|
16 #include <fstream> |
|
17 |
|
18 #include <boost/graph/graph_traits.hpp> |
|
19 #include <boost/graph/adjacency_list.hpp> |
|
20 #include <boost/graph/dijkstra_shortest_paths.hpp> |
|
21 |
|
22 #ifdef __SYMBIAN32__ |
|
23 #include "std_log_result.h" |
|
24 #define LOG_FILENAME_LINE __FILE__, __LINE__ |
|
25 #endif |
|
26 |
|
27 |
|
28 |
|
29 using namespace boost; |
|
30 |
|
31 int |
|
32 main(int, char *[]) |
|
33 { |
|
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; |
|
39 |
|
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) |
|
45 }; |
|
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 |
|
49 graph_t g(num_nodes); |
|
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]; |
|
55 } |
|
56 #else |
|
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); |
|
59 #endif |
|
60 std::vector<vertex_descriptor> p(num_vertices(g)); |
|
61 std::vector<int> d(num_vertices(g)); |
|
62 vertex_descriptor s = vertex(A, g); |
|
63 |
|
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()); |
|
71 #else |
|
72 dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); |
|
73 #endif |
|
74 |
|
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:: |
|
80 endl; |
|
81 } |
|
82 std::cout << std::endl; |
|
83 |
|
84 std::ofstream dot_file("figs/dijkstra-eg.dot"); |
|
85 |
|
86 dot_file << "digraph D {\n" |
|
87 << " rankdir=LR\n" |
|
88 << " size=\"4,3\"\n" |
|
89 << " ratio=\"fill\"\n" |
|
90 << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; |
|
91 |
|
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) << "\""; |
|
99 if (p[v] == u) |
|
100 dot_file << ", color=\"black\""; |
|
101 else |
|
102 dot_file << ", color=\"grey\""; |
|
103 dot_file << "]"; |
|
104 } |
|
105 dot_file << "}"; |
|
106 |
|
107 #ifdef __SYMBIAN32__ |
|
108 std_log(LOG_FILENAME_LINE,"[End Test Case ]"); |
|
109 testResultXml("dijkstra-example"); |
|
110 close_log_file(); |
|
111 #endif |
|
112 return EXIT_SUCCESS; |
|
113 } |
|
114 |
|
115 |
|
116 |