Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/dijkstra-example.cpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.4 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 < listS, vecS, directedS,
22    no_property, property < edge_weight_t, int > > graph_t;
23  typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
24  typedef graph_traits < graph_t >::edge_descriptor edge_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#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
36  graph_t g(num_nodes);
37  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
38  for (std::size_t j = 0; j < num_arcs; ++j) {
39    edge_descriptor e; bool inserted;
40    tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
41    weightmap[e] = weights[j];
42  }
43#else
44  graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
45  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
46#endif
47  std::vector<vertex_descriptor> p(num_vertices(g));
48  std::vector<int> d(num_vertices(g));
49  vertex_descriptor s = vertex(A, g);
50
51#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
52  // VC++ has trouble with the named parameters mechanism
53  property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
54  dijkstra_shortest_paths(g, s, &p[0], &d[0], weightmap, indexmap, 
55                          std::less<int>(), closed_plus<int>(), 
56                          (std::numeric_limits<int>::max)(), 0,
57                          default_dijkstra_visitor());
58#else
59  dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
60#endif
61
62  std::cout << "distances and parents:" << std::endl;
63  graph_traits < graph_t >::vertex_iterator vi, vend;
64  for (tie(vi, vend) = vertices(g); vi != vend; ++vi) {
65    std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
66    std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
67      endl;
68  }
69  std::cout << std::endl;
70
71  std::ofstream dot_file("figs/dijkstra-eg.dot");
72
73  dot_file << "digraph D {\n"
74    << "  rankdir=LR\n"
75    << "  size=\"4,3\"\n"
76    << "  ratio=\"fill\"\n"
77    << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
78
79  graph_traits < graph_t >::edge_iterator ei, ei_end;
80  for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
81    graph_traits < graph_t >::edge_descriptor e = *ei;
82    graph_traits < graph_t >::vertex_descriptor
83      u = source(e, g), v = target(e, g);
84    dot_file << name[u] << " -> " << name[v]
85      << "[label=\"" << get(weightmap, e) << "\"";
86    if (p[v] == u)
87      dot_file << ", color=\"black\"";
88    else
89      dot_file << ", color=\"grey\"";
90    dot_file << "]";
91  }
92  dot_file << "}";
93  return EXIT_SUCCESS;
94}
Note: See TracBrowser for help on using the repository browser.