labeled_graph.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // (C) Copyright 2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <string>
  8. #include <set>
  9. #include <boost/assert.hpp>
  10. #include <boost/range.hpp>
  11. #include <boost/graph/undirected_graph.hpp>
  12. #include <boost/graph/directed_graph.hpp>
  13. #include <boost/graph/labeled_graph.hpp>
  14. #include "typestr.hpp"
  15. using std::cout;
  16. using std::string;
  17. using namespace boost;
  18. void test_norm();
  19. void test_temp();
  20. void test_bacon();
  21. int main() {
  22. test_norm();
  23. test_temp();
  24. test_bacon();
  25. }
  26. //////////////////////////////////////
  27. // Utility Functions and Types
  28. //////////////////////////////////////
  29. struct Actor {
  30. Actor() { }
  31. Actor(string const& s) : name(s) { }
  32. string name;
  33. };
  34. struct Movie {
  35. Movie() { }
  36. Movie(string const& s) : name(s) { }
  37. string name;
  38. };
  39. template <typename Graph>
  40. void init_graph(Graph& g) {
  41. for(int i = 0; i < 6; ++i) {
  42. add_vertex(i, g);
  43. }
  44. }
  45. template <typename Graph>
  46. void label_graph(Graph& g)
  47. {
  48. typedef typename graph_traits<Graph>::vertex_iterator Iter;
  49. Iter f, l;
  50. int x = 0;
  51. for(boost::tie(f, l) = vertices(g); f != l; ++f, ++x) {
  52. label_vertex(*f, x, g);
  53. }
  54. }
  55. template <typename Graph>
  56. void build_graph(Graph& g) {
  57. // This is the graph shown on the wikipedia page for Graph Theory.
  58. add_edge_by_label(5, 3, g);
  59. add_edge_by_label(3, 4, g);
  60. add_edge_by_label(3, 2, g);
  61. add_edge_by_label(4, 1, g);
  62. add_edge_by_label(4, 0, g);
  63. add_edge_by_label(2, 1, g);
  64. add_edge_by_label(1, 0, g);
  65. BOOST_ASSERT(num_vertices(g) == 6);
  66. BOOST_ASSERT(num_edges(g) == 7);
  67. }
  68. //////////////////////////////////////
  69. // Temporal Labelings
  70. //////////////////////////////////////
  71. void test_norm() {
  72. {
  73. typedef labeled_graph<undirected_graph<>, unsigned> Graph;
  74. Graph g;
  75. init_graph(g);
  76. build_graph(g);
  77. }
  78. {
  79. typedef labeled_graph<directed_graph<>, unsigned> Graph;
  80. Graph g;
  81. init_graph(g);
  82. build_graph(g);
  83. }
  84. }
  85. //////////////////////////////////////
  86. // Temporal Labelings
  87. //////////////////////////////////////
  88. void test_temp() {
  89. typedef undirected_graph<> Graph;
  90. typedef labeled_graph<Graph*, int> LabGraph;
  91. Graph g(6);
  92. LabGraph lg(&g);
  93. label_graph(lg);
  94. build_graph(lg);
  95. }
  96. //////////////////////////////////////
  97. // Labeled w/ Properties
  98. //////////////////////////////////////
  99. void test_bacon() {
  100. string bacon("Kevin Bacon");
  101. string slater("Christian Slater");
  102. Movie murder("Murder in the First");
  103. {
  104. typedef labeled_graph<undirected_graph<Actor, Movie>, string> Graph;
  105. Graph g;
  106. add_vertex(bacon, g);
  107. add_vertex(slater, g);
  108. add_edge_by_label(bacon, slater, murder, g);
  109. }
  110. {
  111. string bacon = "Kevin Bacon";
  112. string slater = "Christian Slater";
  113. typedef labeled_graph<directed_graph<Actor, Movie>, string> Graph;
  114. Graph g;
  115. add_vertex(bacon, g);
  116. add_vertex(slater, g);
  117. add_edge_by_label(bacon, slater, murder, g);
  118. }
  119. }