Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/graph/example/min_max_paths.cpp @ 12

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

added boost

File size: 3.0 KB
Line 
1//=======================================================================
2// Copyright 1997-2001 University of Notre Dame.
3// Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
4//
5// Distributed under the Boost Software License, Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//=======================================================================
9#include <boost/config.hpp>
10#include <iostream>
11
12#include <boost/graph/graph_traits.hpp>
13#include <boost/graph/graph_utility.hpp>
14#include <boost/graph/adjacency_list.hpp>
15#include <boost/graph/dijkstra_shortest_paths.hpp>
16#include <boost/graph/visitors.hpp>
17#include <boost/graph/transpose_graph.hpp>
18
19/* Output:
20
21  distances from start vertex:
22  distance(a) = 0
23  distance(b) = 3
24  distance(c) = 1
25  distance(d) = 3
26  distance(e) = 3
27
28  min-max paths tree
29  a --> c
30  b -->
31  c --> d
32  d --> e
33  e --> b
34
35*/
36
37int 
38main(int , char* [])
39{
40  using namespace boost;
41
42  typedef adjacency_list<listS, vecS, directedS, 
43    no_property, property<edge_weight_t, int> > Graph;
44  typedef graph_traits<Graph>::vertex_descriptor Vertex;
45
46  typedef std::pair<int,int> E;
47
48  const char name[] = "abcdef";
49
50  const int num_nodes = 6;
51  E edges[] = { E(0,2), E(1,1), E(1,3), E(1,4), E(2,1), E(2,3), 
52                E(3,4), E(4,0), E(4,1) };
53  int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1};
54  const int n_edges = sizeof(edges)/sizeof(E);
55#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
56  // VC++ can't handle iterator constructors
57  Graph G(num_nodes);
58  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
59  for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j) {
60    graph_traits<Graph>::edge_descriptor e; bool inserted;
61    tie(e, inserted) = add_edge(edges[j].first, edges[j].second, G);
62    weightmap[e] = weights[j];
63  }
64#else
65  Graph G(edges, edges + n_edges, weights, num_nodes);
66  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, G);
67#endif
68
69  std::vector<Vertex> p(num_vertices(G));
70  std::vector<int> d(num_vertices(G));
71
72  Vertex s = *(vertices(G).first);
73
74#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
75  dijkstra_shortest_paths
76    (G, s, &p[0], &d[0], weightmap, get(vertex_index, G),
77     std::greater<int>(), closed_plus<int>(), (std::numeric_limits<int>::max)(), 0,
78     default_dijkstra_visitor());
79#else
80  dijkstra_shortest_paths
81    (G, s, distance_map(&d[0]).
82     predecessor_map(&p[0]).
83     distance_compare(std::greater<int>()));
84#endif
85
86  std::cout << "distances from start vertex:" << std::endl;
87  graph_traits<Graph>::vertex_iterator vi, vend;
88  for(tie(vi,vend) = vertices(G); vi != vend; ++vi)
89    std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << std::endl;
90  std::cout << std::endl;
91
92  std::cout << "min-max paths tree" << std::endl;
93  adjacency_list<> tree(num_nodes);
94 
95  for(tie(vi,vend) = vertices(G); vi != vend; ++vi)
96    if (*vi != p[*vi])
97      add_edge(p[*vi], *vi, tree);
98
99  print_graph(tree, name);
100
101  return 0;
102}
Note: See TracBrowser for help on using the repository browser.