incremental_components.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2009 Trustees of Indiana University.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Michael Hansen
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. #include <iostream>
  11. #include <vector>
  12. #include <boost/foreach.hpp>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/graph_utility.hpp>
  15. #include <boost/graph/incremental_components.hpp>
  16. #include <boost/pending/disjoint_sets.hpp>
  17. /*
  18. This example shows how to use the disjoint set data structure
  19. to compute the connected components of an undirected, changing
  20. graph.
  21. Sample output:
  22. An undirected graph:
  23. 0 <--> 1 4
  24. 1 <--> 0 4
  25. 2 <--> 5
  26. 3 <-->
  27. 4 <--> 1 0
  28. 5 <--> 2
  29. representative[0] = 1
  30. representative[1] = 1
  31. representative[2] = 5
  32. representative[3] = 3
  33. representative[4] = 1
  34. representative[5] = 5
  35. component 0 contains: 4 1 0
  36. component 1 contains: 3
  37. component 2 contains: 5 2
  38. */
  39. using namespace boost;
  40. int main(int argc, char* argv[])
  41. {
  42. typedef adjacency_list <vecS, vecS, undirectedS> Graph;
  43. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  44. typedef graph_traits<Graph>::vertices_size_type VertexIndex;
  45. const int VERTEX_COUNT = 6;
  46. Graph graph(VERTEX_COUNT);
  47. std::vector<VertexIndex> rank(num_vertices(graph));
  48. std::vector<Vertex> parent(num_vertices(graph));
  49. typedef VertexIndex* Rank;
  50. typedef Vertex* Parent;
  51. disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]);
  52. initialize_incremental_components(graph, ds);
  53. incremental_components(graph, ds);
  54. graph_traits<Graph>::edge_descriptor edge;
  55. bool flag;
  56. boost::tie(edge, flag) = add_edge(0, 1, graph);
  57. ds.union_set(0,1);
  58. boost::tie(edge, flag) = add_edge(1, 4, graph);
  59. ds.union_set(1,4);
  60. boost::tie(edge, flag) = add_edge(4, 0, graph);
  61. ds.union_set(4,0);
  62. boost::tie(edge, flag) = add_edge(2, 5, graph);
  63. ds.union_set(2,5);
  64. std::cout << "An undirected graph:" << std::endl;
  65. print_graph(graph, get(boost::vertex_index, graph));
  66. std::cout << std::endl;
  67. BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
  68. std::cout << "representative[" << current_vertex << "] = " <<
  69. ds.find_set(current_vertex) << std::endl;
  70. }
  71. std::cout << std::endl;
  72. typedef component_index<VertexIndex> Components;
  73. // NOTE: Because we're using vecS for the graph type, we're
  74. // effectively using identity_property_map for a vertex index map.
  75. // If we were to use listS instead, the index map would need to be
  76. // explicitly passed to the component_index constructor.
  77. Components components(parent.begin(), parent.end());
  78. // Iterate through the component indices
  79. BOOST_FOREACH(VertexIndex current_index, components) {
  80. std::cout << "component " << current_index << " contains: ";
  81. // Iterate through the child vertex indices for [current_index]
  82. BOOST_FOREACH(VertexIndex child_index,
  83. components[current_index]) {
  84. std::cout << child_index << " ";
  85. }
  86. std::cout << std::endl;
  87. }
  88. return (0);
  89. }