undirected_adjacency_list.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <iostream>
  10. #include <boost/graph/adjacency_list.hpp>
  11. using namespace boost;
  12. template < typename UndirectedGraph > void
  13. undirected_graph_demo1()
  14. {
  15. const int V = 3;
  16. UndirectedGraph undigraph(V);
  17. typename graph_traits < UndirectedGraph >::vertex_descriptor zero, one, two;
  18. typename graph_traits < UndirectedGraph >::out_edge_iterator out, out_end;
  19. typename graph_traits < UndirectedGraph >::in_edge_iterator in, in_end;
  20. zero = vertex(0, undigraph);
  21. one = vertex(1, undigraph);
  22. two = vertex(2, undigraph);
  23. add_edge(zero, one, undigraph);
  24. add_edge(zero, two, undigraph);
  25. add_edge(one, two, undigraph);
  26. std::cout << "out_edges(0):";
  27. for (boost::tie(out, out_end) = out_edges(zero, undigraph); out != out_end; ++out)
  28. std::cout << ' ' << *out;
  29. std::cout << std::endl << "in_edges(0):";
  30. for (boost::tie(in, in_end) = in_edges(zero, undigraph); in != in_end; ++in)
  31. std::cout << ' ' << *in;
  32. std::cout << std::endl;
  33. }
  34. template < typename DirectedGraph > void
  35. directed_graph_demo()
  36. {
  37. const int V = 2;
  38. DirectedGraph digraph(V);
  39. typename graph_traits < DirectedGraph >::vertex_descriptor u, v;
  40. typedef typename DirectedGraph::edge_property_type Weight;
  41. typename property_map < DirectedGraph, edge_weight_t >::type
  42. weight = get(edge_weight, digraph);
  43. typename graph_traits < DirectedGraph >::edge_descriptor e1, e2;
  44. bool found;
  45. u = vertex(0, digraph);
  46. v = vertex(1, digraph);
  47. add_edge(u, v, Weight(1.2), digraph);
  48. add_edge(v, u, Weight(2.4), digraph);
  49. boost::tie(e1, found) = edge(u, v, digraph);
  50. boost::tie(e2, found) = edge(v, u, digraph);
  51. std::cout << "in a directed graph is ";
  52. #ifdef __GNUC__
  53. // no boolalpha
  54. std::cout << "(u,v) == (v,u) ? " << (e1 == e2) << std::endl;
  55. #else
  56. std::cout << "(u,v) == (v,u) ? "
  57. << std::boolalpha << (e1 == e2) << std::endl;
  58. #endif
  59. std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl;
  60. std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl;
  61. }
  62. template < typename UndirectedGraph > void
  63. undirected_graph_demo2()
  64. {
  65. const int V = 2;
  66. UndirectedGraph undigraph(V);
  67. typename graph_traits < UndirectedGraph >::vertex_descriptor u, v;
  68. typedef typename UndirectedGraph::edge_property_type Weight;
  69. typename property_map < UndirectedGraph, edge_weight_t >::type
  70. weight = get(edge_weight, undigraph);
  71. typename graph_traits < UndirectedGraph >::edge_descriptor e1, e2;
  72. bool found;
  73. u = vertex(0, undigraph);
  74. v = vertex(1, undigraph);
  75. add_edge(u, v, Weight(3.1), undigraph);
  76. boost::tie(e1, found) = edge(u, v, undigraph);
  77. boost::tie(e2, found) = edge(v, u, undigraph);
  78. std::cout << "in an undirected graph is ";
  79. #ifdef __GNUC__
  80. std::cout << "(u,v) == (v,u) ? " << (e1 == e2) << std::endl;
  81. #else
  82. std::cout << "(u,v) == (v,u) ? "
  83. << std::boolalpha << (e1 == e2) << std::endl;
  84. #endif
  85. std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl;
  86. std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl;
  87. std::cout << "the edges incident to v: ";
  88. typename boost::graph_traits<UndirectedGraph>::out_edge_iterator e, e_end;
  89. typename boost::graph_traits<UndirectedGraph>::vertex_descriptor
  90. s = vertex(0, undigraph);
  91. for (boost::tie(e, e_end) = out_edges(s, undigraph); e != e_end; ++e)
  92. std::cout << "(" << source(*e, undigraph)
  93. << "," << target(*e, undigraph) << ")" << std::endl;
  94. }
  95. int
  96. main()
  97. {
  98. typedef property < edge_weight_t, double >Weight;
  99. typedef adjacency_list < vecS, vecS, undirectedS,
  100. no_property, Weight > UndirectedGraph;
  101. typedef adjacency_list < vecS, vecS, directedS,
  102. no_property, Weight > DirectedGraph;
  103. undirected_graph_demo1 < UndirectedGraph > ();
  104. directed_graph_demo < DirectedGraph > ();
  105. undirected_graph_demo2 < UndirectedGraph > ();
  106. return 0;
  107. }