Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / graph / example / dijkstra-example.cpp
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 #include <boost/config.hpp>
9 #include <iostream>
10 #include <fstream>
11
12 #include <boost/graph/graph_traits.hpp>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/dijkstra_shortest_paths.hpp>
15 #include <boost/property_map/property_map.hpp>
16
17 using namespace boost;
18
19 int
20 main(int, char *[])
21 {
22   typedef adjacency_list < listS, vecS, directedS,
23     no_property, property < edge_weight_t, int > > graph_t;
24   typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
25   typedef std::pair<int, int> Edge;
26
27   const int num_nodes = 5;
28   enum nodes { A, B, C, D, E };
29   char name[] = "ABCDE";
30   Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
31     Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
32   };
33   int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
34   int num_arcs = sizeof(edge_array) / sizeof(Edge);
35   graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
36   property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
37   std::vector<vertex_descriptor> p(num_vertices(g));
38   std::vector<int> d(num_vertices(g));
39   vertex_descriptor s = vertex(A, g);
40
41   dijkstra_shortest_paths(g, s,
42                           predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
43                           distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
44
45   std::cout << "distances and parents:" << std::endl;
46   graph_traits < graph_t >::vertex_iterator vi, vend;
47   for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
48     std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
49     std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
50       endl;
51   }
52   std::cout << std::endl;
53
54   std::ofstream dot_file("figs/dijkstra-eg.dot");
55
56   dot_file << "digraph D {\n"
57     << "  rankdir=LR\n"
58     << "  size=\"4,3\"\n"
59     << "  ratio=\"fill\"\n"
60     << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
61
62   graph_traits < graph_t >::edge_iterator ei, ei_end;
63   for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
64     graph_traits < graph_t >::edge_descriptor e = *ei;
65     graph_traits < graph_t >::vertex_descriptor
66       u = source(e, g), v = target(e, g);
67     dot_file << name[u] << " -> " << name[v]
68       << "[label=\"" << get(weightmap, e) << "\"";
69     if (p[v] == u)
70       dot_file << ", color=\"black\"";
71     else
72       dot_file << ", color=\"grey\"";
73     dot_file << "]";
74   }
75   dot_file << "}";
76   return EXIT_SUCCESS;
77 }