Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/test/bellman-test.cpp @ 33

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

updated boost from 1_33_1 to 1_34_1

File size: 2.9 KB
Line 
1//  (C) Copyright Jeremy Siek 2004
2//  Distributed under the Boost Software License, Version 1.0. (See
3//  accompanying file LICENSE_1_0.txt or copy at
4//  http://www.boost.org/LICENSE_1_0.txt)
5
6// From Louis Lavery <Louis@devilsChimney.co.uk>
7/*Expected Output:-
8A:   0 A
9B:  11 A
10
11Actual Output:-
12A:   0 A
13B: 2147483647 B
14*/
15
16#include <iostream>
17#include <iomanip>
18#include <boost/graph/adjacency_list.hpp>
19#include <boost/graph/bellman_ford_shortest_paths.hpp>
20#include <boost/cstdlib.hpp>
21#include <boost/test/minimal.hpp>
22
23int test_main(int, char*[])
24{
25  using namespace boost;
26
27  enum { A, B, Z };
28  char const name[] = "ABZ";
29  int const numVertex = static_cast<int>(Z) + 1;
30  typedef std::pair<int,int> Edge;
31  Edge edge_array[] = {Edge(B,A)};
32  int const numEdges = sizeof(edge_array) / sizeof(Edge);
33  int const weight[numEdges] = {11};
34
35  typedef adjacency_list<vecS,vecS,undirectedS,
36    no_property,property<edge_weight_t,int> > Graph;
37
38  Graph g(edge_array, edge_array + numEdges, numVertex);
39
40  Graph::edge_iterator ei, ei_end;
41  property_map<Graph,edge_weight_t>::type
42    weight_pmap = get(edge_weight, g);
43
44  int i = 0;
45  for(tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
46    weight_pmap[*ei] = weight[i];
47
48  std::vector<int> parent(numVertex);
49  for(i = 0; i < numVertex; ++i)
50    parent[i] = i;
51
52  int inf = (std::numeric_limits<int>::max)();
53  std::vector<int> distance(numVertex, inf);
54  distance[A] = 0; // Set source distance to zero
55
56  bool const r = bellman_ford_shortest_paths
57    (g, int (numVertex),
58     weight_pmap, 
59     &parent[0],
60     &distance[0],
61     closed_plus<int>(),
62     std::less<int>(),
63     default_bellman_visitor());
64
65  if (r) {
66    for(int i = 0; i < numVertex; ++i) {
67      std::cout << name[i] << ": ";
68      if (distance[i] == inf)
69        std::cout  << std::setw(3) << "inf";
70      else
71        std::cout << std::setw(3) << distance[i];
72      std::cout << " " << name[parent[i]] << std::endl;
73    }
74  } else {
75    std::cout << "negative cycle" << std::endl;
76  }
77
78#if !(defined(__INTEL_COMPILER) && __INTEL_COMPILER <= 700) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
79  graph_traits<Graph>::vertex_descriptor s = vertex(A, g);
80  std::vector<int> parent2(numVertex);
81  std::vector<int> distance2(numVertex, 17);
82  bool const r2 = bellman_ford_shortest_paths
83                    (g, 
84                     weight_map(weight_pmap).distance_map(&distance2[0]).
85                     predecessor_map(&parent2[0]).root_vertex(s));
86  if (r2) {
87    for(int i = 0; i < numVertex; ++i) {
88      std::cout << name[i] << ": ";
89      if (distance2[i] == inf)
90        std::cout  << std::setw(3) << "inf";
91      else
92        std::cout << std::setw(3) << distance2[i];
93      std::cout << " " << name[parent2[i]] << std::endl;
94    }
95  } else {
96    std::cout << "negative cycle" << std::endl;
97  }
98
99  BOOST_CHECK(r == r2);
100  if (r && r2) {
101    BOOST_CHECK(parent == parent2);
102    BOOST_CHECK(distance == distance2);
103  }
104#endif
105
106  return boost::exit_success;
107}
Note: See TracBrowser for help on using the repository browser.