strong_components.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //=======================================================================
  2. // Copyright 1997-2001 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. /*
  10. IMPORTANT!!!
  11. ~~~~~~~~~~~~
  12. This example uses interfaces that have been deprecated and removed from Boost.Grpah.
  13. Someone needs to update it, as it does NOT compile.
  14. */
  15. #include <boost/config.hpp>
  16. #include <iostream>
  17. #include <vector>
  18. #include <boost/graph/strong_components.hpp>
  19. #include <boost/graph/adjacency_list.hpp>
  20. #include <boost/graph/graphviz.hpp>
  21. #include <boost/graph/graph_utility.hpp>
  22. /*
  23. Sample output:
  24. A directed graph:
  25. a --> b f h
  26. b --> c a
  27. c --> d b
  28. d --> e
  29. e --> d
  30. f --> g
  31. g --> f d
  32. h --> i
  33. i --> h j e c
  34. j -->
  35. Total number of components: 4
  36. Vertex a is in component 3
  37. Vertex b is in component 3
  38. Vertex c is in component 3
  39. Vertex d is in component 0
  40. Vertex e is in component 0
  41. Vertex f is in component 1
  42. Vertex g is in component 1
  43. Vertex h is in component 3
  44. Vertex i is in component 3
  45. Vertex j is in component 2
  46. */
  47. int main(int, char*[])
  48. {
  49. using namespace boost;
  50. const char* name = "abcdefghij";
  51. adjacency_list<vecS, vecS, directedS> G;
  52. dynamic_properties dp;
  53. read_graphviz("scc.dot", G, dp);
  54. std::cout << "A directed graph:" << std::endl;
  55. print_graph(G, name);
  56. std::cout << std::endl;
  57. typedef graph_traits<adjacency_list<vecS, vecS, directedS> >::vertex_descriptor Vertex;
  58. std::vector<int> component(num_vertices(G)), discover_time(num_vertices(G));
  59. std::vector<default_color_type> color(num_vertices(G));
  60. std::vector<Vertex> root(num_vertices(G));
  61. int num = strong_components(G, make_iterator_property_map(component.begin(), get(vertex_index, G)),
  62. root_map(make_iterator_property_map(root.begin(), get(vertex_index, G))).
  63. color_map(make_iterator_property_map(color.begin(), get(vertex_index, G))).
  64. discover_time_map(make_iterator_property_map(discover_time.begin(), get(vertex_index, G))));
  65. std::cout << "Total number of components: " << num << std::endl;
  66. std::vector<int>::size_type i;
  67. for (i = 0; i != component.size(); ++i)
  68. std::cout << "Vertex " << name[i]
  69. <<" is in component " << component[i] << std::endl;
  70. return 0;
  71. }