Guest User

Untitled

a guest
Nov 19th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <boost/config.hpp>
  2. #include <iostream>                         // for std::cout
  3. #include <utility>                          // for std::pair
  4. #include <algorithm>                        // for std::for_each
  5. #include <boost/utility.hpp>                // for boost::tie
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/graph/graphviz.hpp>
  8.  
  9.  
  10.  
  11. #include <boost/range/begin.hpp>
  12. #include <boost/range/end.hpp>
  13.  
  14. using namespace boost;
  15.  
  16.  
  17.  
  18. template <class Graph> struct exercise_vertex {
  19.   exercise_vertex(Graph& g_, const char name_[]) : g(g_),name(name_) { }
  20.   typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  21.   void operator()(const Vertex& v) const
  22.   {
  23.     using namespace boost;
  24.     typename property_map<Graph, vertex_index_t>::type
  25.       vertex_id = get(vertex_index, g);
  26.     std::cout << "vertex: " << name[get(vertex_id, v)] << std::endl;
  27.  
  28.     std::cout << "\tout-edges: ";
  29.  
  30.     for (auto &e : out_edges(v, g)){
  31.         std::cout<<"!";
  32.     }
  33.  
  34.     typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
  35.     typename boost::graph_traits<Graph>::edge_descriptor e;
  36.     for (std::tie(out_i, out_end) = out_edges(v, g);
  37.          out_i != out_end; ++out_i)
  38.     {
  39.       e = *out_i;
  40.       Vertex src = source(e, g), targ = target(e, g);
  41.       std::cout << "(" << name[get(vertex_id, src)]
  42.                 << "," << name[get(vertex_id, targ)] << ") ";
  43.     }
  44.     std::cout << std::endl;
  45.  
  46.   }
  47.   Graph& g;
  48.   const char *name;
  49. };
  50.  
  51.  
  52. int main(int,char*[])
  53. {
  54.   typedef adjacency_list<vecS, vecS, bidirectionalS,
  55.      no_property, property<edge_weight_t, float> > Graph;
  56.  
  57.   enum { A, B, C, D, E, N };
  58.   const int num_vertices = N;
  59.   const char name[] = "ABCDE";
  60.  
  61.   typedef std::pair<int,int> Edge;
  62.   Edge edge_array[] =
  63.   { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
  64.     Edge(C,E), Edge(B,D), Edge(D,E), };
  65.   const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
  66.  
  67.   float transmission_delay[] = { 1.2, 4.5, 2.6, 0.4, 5.2, 1.8, 3.3, 9.1 };
  68.  
  69.  
  70.   Graph g(edge_array, edge_array + num_edges,
  71.           transmission_delay, num_vertices);
  72.  
  73.   std::for_each(vertices(g).first, vertices(g).second,
  74.                 exercise_vertex<Graph>(g, name));
  75.  
  76.  
  77.   return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment