os/ossrv/stdcpp/tsrc/Boost_test/graph/src/dijkstra-example.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//=======================================================================
sl@0
     2
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, 
sl@0
     3
//
sl@0
     4
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
//=======================================================================
sl@0
     8
sl@0
     9
/*
sl@0
    10
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
sl@0
    11
*/
sl@0
    12
sl@0
    13
sl@0
    14
#include <boost/config.hpp>
sl@0
    15
#include <iostream>
sl@0
    16
#include <fstream>
sl@0
    17
sl@0
    18
#include <boost/graph/graph_traits.hpp>
sl@0
    19
#include <boost/graph/adjacency_list.hpp>
sl@0
    20
#include <boost/graph/dijkstra_shortest_paths.hpp>
sl@0
    21
sl@0
    22
#ifdef __SYMBIAN32__
sl@0
    23
#include "std_log_result.h"
sl@0
    24
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    25
#endif
sl@0
    26
sl@0
    27
sl@0
    28
sl@0
    29
using namespace boost;
sl@0
    30
sl@0
    31
int
sl@0
    32
main(int, char *[])
sl@0
    33
{
sl@0
    34
  typedef adjacency_list < listS, vecS, directedS,
sl@0
    35
    no_property, property < edge_weight_t, int > > graph_t;
sl@0
    36
  typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
sl@0
    37
  typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
sl@0
    38
  typedef std::pair<int, int> Edge;
sl@0
    39
sl@0
    40
  const int num_nodes = 5;
sl@0
    41
  enum nodes { A, B, C, D, E };
sl@0
    42
  char name[] = "ABCDE";
sl@0
    43
  Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
sl@0
    44
    Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
sl@0
    45
  };
sl@0
    46
  int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
sl@0
    47
  int num_arcs = sizeof(edge_array) / sizeof(Edge);
sl@0
    48
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
sl@0
    49
  graph_t g(num_nodes);
sl@0
    50
  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
sl@0
    51
  for (std::size_t j = 0; j < num_arcs; ++j) {
sl@0
    52
    edge_descriptor e; bool inserted;
sl@0
    53
    tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
sl@0
    54
    weightmap[e] = weights[j];
sl@0
    55
  }
sl@0
    56
#else
sl@0
    57
  graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
sl@0
    58
  property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
sl@0
    59
#endif
sl@0
    60
  std::vector<vertex_descriptor> p(num_vertices(g));
sl@0
    61
  std::vector<int> d(num_vertices(g));
sl@0
    62
  vertex_descriptor s = vertex(A, g);
sl@0
    63
sl@0
    64
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
sl@0
    65
  // VC++ has trouble with the named parameters mechanism
sl@0
    66
  property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
sl@0
    67
  dijkstra_shortest_paths(g, s, &p[0], &d[0], weightmap, indexmap, 
sl@0
    68
                          std::less<int>(), closed_plus<int>(), 
sl@0
    69
                          (std::numeric_limits<int>::max)(), 0,
sl@0
    70
                          default_dijkstra_visitor());
sl@0
    71
#else
sl@0
    72
  dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0]));
sl@0
    73
#endif
sl@0
    74
sl@0
    75
  std::cout << "distances and parents:" << std::endl;
sl@0
    76
  graph_traits < graph_t >::vertex_iterator vi, vend;
sl@0
    77
  for (tie(vi, vend) = vertices(g); vi != vend; ++vi) {
sl@0
    78
    std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
sl@0
    79
    std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
sl@0
    80
      endl;
sl@0
    81
  }
sl@0
    82
  std::cout << std::endl;
sl@0
    83
sl@0
    84
  std::ofstream dot_file("figs/dijkstra-eg.dot");
sl@0
    85
sl@0
    86
  dot_file << "digraph D {\n"
sl@0
    87
    << "  rankdir=LR\n"
sl@0
    88
    << "  size=\"4,3\"\n"
sl@0
    89
    << "  ratio=\"fill\"\n"
sl@0
    90
    << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";
sl@0
    91
sl@0
    92
  graph_traits < graph_t >::edge_iterator ei, ei_end;
sl@0
    93
  for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
sl@0
    94
    graph_traits < graph_t >::edge_descriptor e = *ei;
sl@0
    95
    graph_traits < graph_t >::vertex_descriptor
sl@0
    96
      u = source(e, g), v = target(e, g);
sl@0
    97
    dot_file << name[u] << " -> " << name[v]
sl@0
    98
      << "[label=\"" << get(weightmap, e) << "\"";
sl@0
    99
    if (p[v] == u)
sl@0
   100
      dot_file << ", color=\"black\"";
sl@0
   101
    else
sl@0
   102
      dot_file << ", color=\"grey\"";
sl@0
   103
    dot_file << "]";
sl@0
   104
  }
sl@0
   105
  dot_file << "}";
sl@0
   106
  
sl@0
   107
   #ifdef __SYMBIAN32__
sl@0
   108
	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
sl@0
   109
	testResultXml("dijkstra-example");
sl@0
   110
	close_log_file();
sl@0
   111
    #endif
sl@0
   112
  return EXIT_SUCCESS;
sl@0
   113
}
sl@0
   114
sl@0
   115
sl@0
   116