cc89ab4fd175d6a542b5a0fa596d01379a4b0ba7
[platform/upstream/boost.git] / libs / graph / example / dijkstra-no-color-map-example.cpp
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, 
5 //
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)
9 //
10 // NOTE: Based off of dijkstra-example.cpp
11 //=======================================================================
12 #include <boost/config.hpp>
13 #include <iostream>
14 #include <fstream>
15
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>
19
20 using namespace boost;
21
22 int
23 main(int, char *[])
24 {
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;
30
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)
36   };
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
40   graph_t g(num_nodes);
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];
46   }
47 #else
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);
50 #endif
51   std::vector<vertex_descriptor> p(num_vertices(g));
52   std::vector<int> d(num_vertices(g));
53   vertex_descriptor s = vertex(A, g);
54
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>(),
60                                        closed_plus<int>(),
61                                        (std::numeric_limits<int>::max)(), 0,
62                                        default_dijkstra_visitor());
63 #else
64   dijkstra_shortest_paths_no_color_map(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
65 #endif
66
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::
72       endl;
73   }
74   std::cout << std::endl;
75
76   std::ofstream dot_file("figs/dijkstra-no-color-map-eg.dot");
77
78   dot_file << "digraph D {\n"
79     << "  rankdir=LR\n"
80     << "  size=\"4,3\"\n"
81     << "  ratio=\"fill\"\n"
82     << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
83
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) << "\"";
91     if (p[v] == u)
92       dot_file << ", color=\"black\"";
93     else
94       dot_file << ", color=\"grey\"";
95     dot_file << "]";
96   }
97   dot_file << "}";
98   return EXIT_SUCCESS;
99 }