Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / graph / example / dijkstra-example-listS.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
16 using namespace boost;
17
18 int
19 main(int, char *[])
20 {
21   typedef adjacency_list_traits<listS, listS, 
22     directedS>::vertex_descriptor vertex_descriptor;
23   typedef adjacency_list < listS, listS, directedS,
24     property<vertex_index_t, int, 
25     property<vertex_name_t, char,
26     property<vertex_distance_t, int,
27     property<vertex_predecessor_t, vertex_descriptor> > > >, 
28     property<edge_weight_t, int> > graph_t;
29   typedef std::pair<int, int> Edge;
30
31   const int num_nodes = 5;
32   enum nodes { A, B, C, D, E };
33   Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
34     Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
35   };
36   int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
37   int num_arcs = sizeof(edge_array) / sizeof(Edge);
38   graph_traits<graph_t>::vertex_iterator i, iend;
39
40   graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
41   property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
42
43   // Manually intialize the vertex index and name maps
44   property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
45   property_map<graph_t, vertex_name_t>::type name = get(vertex_name, g);
46   int c = 0;
47   for (boost::tie(i, iend) = vertices(g); i != iend; ++i, ++c) {
48     indexmap[*i] = c;
49     name[*i] = 'A' + c;
50   }
51
52   vertex_descriptor s = vertex(A, g);
53
54   property_map<graph_t, vertex_distance_t>::type
55     d = get(vertex_distance, g);
56   property_map<graph_t, vertex_predecessor_t>::type
57     p = get(vertex_predecessor, g);
58   dijkstra_shortest_paths(g, s, predecessor_map(p).distance_map(d));
59
60   std::cout << "distances and parents:" << std::endl;
61   graph_traits < graph_t >::vertex_iterator vi, vend;
62   for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
63     std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
64     std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
65       endl;
66   }
67   std::cout << std::endl;
68
69   std::ofstream dot_file("figs/dijkstra-eg.dot");
70   dot_file << "digraph D {\n"
71     << "  rankdir=LR\n"
72     << "  size=\"4,3\"\n"
73     << "  ratio=\"fill\"\n"
74     << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
75
76   graph_traits < graph_t >::edge_iterator ei, ei_end;
77   for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
78     graph_traits < graph_t >::edge_descriptor e = *ei;
79     graph_traits < graph_t >::vertex_descriptor
80       u = source(e, g), v = target(e, g);
81     dot_file << name[u] << " -> " << name[v]
82       << "[label=\"" << get(weightmap, e) << "\"";
83     if (p[v] == u)
84       dot_file << ", color=\"black\"";
85     else
86       dot_file << ", color=\"grey\"";
87     dot_file << "]";
88   }
89   dot_file << "}";
90   return EXIT_SUCCESS;
91 }