os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bellman-test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//  (C) Copyright Jeremy Siek 2004 
sl@0
     2
//  Distributed under the Boost Software License, Version 1.0. (See
sl@0
     3
//  accompanying file LICENSE_1_0.txt or copy at
sl@0
     4
//  http://www.boost.org/LICENSE_1_0.txt)
sl@0
     5
sl@0
     6
// From Louis Lavery <Louis@devilsChimney.co.uk>
sl@0
     7
sl@0
     8
/*
sl@0
     9
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
sl@0
    10
*/
sl@0
    11
sl@0
    12
/*Expected Output:-
sl@0
    13
A:   0 A
sl@0
    14
B:  11 A
sl@0
    15
sl@0
    16
Actual Output:-
sl@0
    17
A:   0 A
sl@0
    18
B: 2147483647 B
sl@0
    19
*/
sl@0
    20
sl@0
    21
#include <iostream>
sl@0
    22
#include <iomanip>
sl@0
    23
#include <boost/graph/adjacency_list.hpp>
sl@0
    24
#include <boost/graph/bellman_ford_shortest_paths.hpp>
sl@0
    25
#include <boost/cstdlib.hpp>
sl@0
    26
#include <boost/test/minimal.hpp>
sl@0
    27
#ifdef __SYMBIAN32__
sl@0
    28
#include "std_log_result.h"
sl@0
    29
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    30
#endif
sl@0
    31
int test_main(int, char*[])
sl@0
    32
{
sl@0
    33
  using namespace boost;
sl@0
    34
sl@0
    35
  enum { A, B, Z };
sl@0
    36
  char const name[] = "ABZ";
sl@0
    37
  int const numVertex = static_cast<int>(Z) + 1;
sl@0
    38
  typedef std::pair<int,int> Edge;
sl@0
    39
  Edge edge_array[] = {Edge(B,A)};
sl@0
    40
  int const numEdges = sizeof(edge_array) / sizeof(Edge);
sl@0
    41
  int const weight[numEdges] = {11};
sl@0
    42
sl@0
    43
  typedef adjacency_list<vecS,vecS,undirectedS,
sl@0
    44
    no_property,property<edge_weight_t,int> > Graph;
sl@0
    45
sl@0
    46
  Graph g(edge_array, edge_array + numEdges, numVertex);
sl@0
    47
sl@0
    48
  Graph::edge_iterator ei, ei_end;
sl@0
    49
  property_map<Graph,edge_weight_t>::type
sl@0
    50
    weight_pmap = get(edge_weight, g);
sl@0
    51
sl@0
    52
  int i = 0;
sl@0
    53
  for(tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
sl@0
    54
    weight_pmap[*ei] = weight[i];
sl@0
    55
sl@0
    56
  std::vector<int> parent(numVertex);
sl@0
    57
  for(i = 0; i < numVertex; ++i)
sl@0
    58
    parent[i] = i;
sl@0
    59
sl@0
    60
  int inf = (std::numeric_limits<int>::max)();
sl@0
    61
  std::vector<int> distance(numVertex, inf);
sl@0
    62
  distance[A] = 0; // Set source distance to zero
sl@0
    63
sl@0
    64
  bool const r = bellman_ford_shortest_paths
sl@0
    65
    (g, int (numVertex),
sl@0
    66
     weight_pmap, 
sl@0
    67
     &parent[0],
sl@0
    68
     &distance[0],
sl@0
    69
     closed_plus<int>(),
sl@0
    70
     std::less<int>(),
sl@0
    71
     default_bellman_visitor());
sl@0
    72
sl@0
    73
  if (r) {
sl@0
    74
    for(int i = 0; i < numVertex; ++i) {
sl@0
    75
      std::cout << name[i] << ": ";
sl@0
    76
      if (distance[i] == inf)
sl@0
    77
        std::cout  << std::setw(3) << "inf";
sl@0
    78
      else
sl@0
    79
        std::cout << std::setw(3) << distance[i];
sl@0
    80
      std::cout << " " << name[parent[i]] << std::endl;
sl@0
    81
    }
sl@0
    82
  } else {
sl@0
    83
    std::cout << "negative cycle" << std::endl;
sl@0
    84
  }
sl@0
    85
sl@0
    86
#if !(defined(__INTEL_COMPILER) && __INTEL_COMPILER <= 700) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
sl@0
    87
  graph_traits<Graph>::vertex_descriptor s = vertex(A, g);
sl@0
    88
  std::vector<int> parent2(numVertex);
sl@0
    89
  std::vector<int> distance2(numVertex, 17);
sl@0
    90
  bool const r2 = bellman_ford_shortest_paths
sl@0
    91
                    (g, 
sl@0
    92
                     weight_map(weight_pmap).distance_map(&distance2[0]).
sl@0
    93
                     predecessor_map(&parent2[0]).root_vertex(s));
sl@0
    94
  if (r2) {
sl@0
    95
    for(int i = 0; i < numVertex; ++i) {
sl@0
    96
      std::cout << name[i] << ": ";
sl@0
    97
      if (distance2[i] == inf)
sl@0
    98
        std::cout  << std::setw(3) << "inf";
sl@0
    99
      else
sl@0
   100
        std::cout << std::setw(3) << distance2[i];
sl@0
   101
      std::cout << " " << name[parent2[i]] << std::endl;
sl@0
   102
    }
sl@0
   103
  } else {
sl@0
   104
    std::cout << "negative cycle" << std::endl;
sl@0
   105
  }
sl@0
   106
sl@0
   107
  BOOST_CHECK(r == r2);
sl@0
   108
  if (r && r2) {
sl@0
   109
    BOOST_CHECK(parent == parent2);
sl@0
   110
    BOOST_CHECK(distance == distance2);
sl@0
   111
  }
sl@0
   112
#endif
sl@0
   113
sl@0
   114
  #ifdef __SYMBIAN32__
sl@0
   115
	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
sl@0
   116
sl@0
   117
	testResultXml("bellman-test");
sl@0
   118
	close_log_file();
sl@0
   119
#endif
sl@0
   120
  return boost::exit_success;
sl@0
   121
}