Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/graph/example/dijkstra-example-listS.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 4.2 KB
Line 
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
16using namespace boost;
17
18int
19main(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 graph_traits<graph_t>::edge_descriptor edge_descriptor;
30  typedef std::pair<int, int> Edge;
31
32  const int num_nodes = 5;
33  enum nodes { A, B, C, D, E };
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  graph_traits<graph_t>::vertex_iterator i, iend;
40
41#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
42  graph_t g(num_nodes);
43  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
44
45  std::vector<vertex_descriptor> msvc_vertices;
46  for (tie(i, iend) = vertices(g); i != iend; ++i)
47    msvc_vertices.push_back(*i);
48
49  for (std::size_t j = 0; j < num_arcs; ++j) {
50    edge_descriptor e; bool inserted;
51    tie(e, inserted) = add_edge(msvc_vertices[edge_array[j].first], 
52                                msvc_vertices[edge_array[j].second], g);
53    weightmap[e] = weights[j];
54  }
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
61  // Manually intialize the vertex index and name maps
62  property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
63  property_map<graph_t, vertex_name_t>::type name = get(vertex_name, g);
64  int c = 0;
65  for (tie(i, iend) = vertices(g); i != iend; ++i, ++c) {
66    indexmap[*i] = c;
67    name[*i] = 'A' + c;
68  }
69
70  vertex_descriptor s = vertex(A, g);
71
72  property_map<graph_t, vertex_distance_t>::type
73    d = get(vertex_distance, g);
74  property_map<graph_t, vertex_predecessor_t>::type
75    p = get(vertex_predecessor, g);
76#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
77  // VC++ has trouble with the named parameters mechanism
78  property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
79  dijkstra_shortest_paths(g, s, p, d, weightmap, indexmap, 
80                          std::less<int>(), closed_plus<int>(), 
81                          (std::numeric_limits<int>::max)(), 0,
82                          default_dijkstra_visitor());
83#else
84  dijkstra_shortest_paths(g, s, predecessor_map(p).distance_map(d));
85#endif
86
87  std::cout << "distances and parents:" << std::endl;
88  graph_traits < graph_t >::vertex_iterator vi, vend;
89  for (tie(vi, vend) = vertices(g); vi != vend; ++vi) {
90    std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
91    std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
92      endl;
93  }
94  std::cout << std::endl;
95
96  std::ofstream dot_file("figs/dijkstra-eg.dot");
97  dot_file << "digraph D {\n"
98    << "  rankdir=LR\n"
99    << "  size=\"4,3\"\n"
100    << "  ratio=\"fill\"\n"
101    << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
102
103  graph_traits < graph_t >::edge_iterator ei, ei_end;
104  for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
105    graph_traits < graph_t >::edge_descriptor e = *ei;
106    graph_traits < graph_t >::vertex_descriptor
107      u = source(e, g), v = target(e, g);
108    dot_file << name[u] << " -> " << name[v]
109      << "[label=\"" << get(weightmap, e) << "\"";
110    if (p[v] == u)
111      dot_file << ", color=\"black\"";
112    else
113      dot_file << ", color=\"grey\"";
114    dot_file << "]";
115  }
116  dot_file << "}";
117  return EXIT_SUCCESS;
118}
Note: See TracBrowser for help on using the repository browser.