incremental-components-eg.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  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/incremental_components.hpp>
  15. #include <boost/pending/disjoint_sets.hpp>
  16. using namespace boost;
  17. int main(int argc, char* argv[])
  18. {
  19. typedef adjacency_list <vecS, vecS, undirectedS> Graph;
  20. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  21. //typedef graph_traits<Graph>::edge_descriptor Edge;
  22. typedef graph_traits<Graph>::vertices_size_type VertexIndex;
  23. // Create a graph
  24. const int VERTEX_COUNT = 6;
  25. Graph graph(VERTEX_COUNT);
  26. add_edge(0, 1, graph);
  27. add_edge(1, 4, graph);
  28. // reate the disjoint-sets object, which requires rank and parent
  29. // vertex properties.
  30. std::vector<Vertex> rank(num_vertices(graph));
  31. std::vector<Vertex> parent(num_vertices(graph));
  32. typedef VertexIndex* Rank;
  33. typedef Vertex* Parent;
  34. disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]);
  35. // Determine the connected components, storing the results in the
  36. // disjoint-sets object.
  37. initialize_incremental_components(graph, ds);
  38. incremental_components(graph, ds);
  39. // Add a couple more edges and update the disjoint-sets
  40. add_edge(4, 0, graph);
  41. add_edge(2, 5, graph);
  42. ds.union_set(4, 0);
  43. ds.union_set(2, 5);
  44. BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
  45. std::cout << "representative[" << current_vertex << "] = " <<
  46. ds.find_set(current_vertex) << std::endl;
  47. }
  48. std::cout << std::endl;
  49. // Generate component index. NOTE: We would need to pass in a vertex
  50. // index map into the component_index constructor if our graph type
  51. // used listS instead of vecS (identity_property_map is used by
  52. // default).
  53. typedef component_index<VertexIndex> Components;
  54. Components components(parent.begin(), parent.end());
  55. // Iterate through the component indices
  56. BOOST_FOREACH(VertexIndex component_index, components) {
  57. std::cout << "component " << component_index << " contains: ";
  58. // Iterate through the child vertex indices for [component_index]
  59. BOOST_FOREACH(VertexIndex child_index,
  60. components[component_index]) {
  61. std::cout << child_index << " ";
  62. }
  63. std::cout << std::endl;
  64. }
  65. return (0);
  66. }