components_on_edgelist.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 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. #include <boost/config.hpp>
  10. #include <iostream>
  11. #include <iterator>
  12. #include <vector>
  13. #include <algorithm>
  14. #include <utility>
  15. #include <boost/graph/edge_list.hpp>
  16. #include <boost/graph/incremental_components.hpp>
  17. #include <boost/pending/disjoint_sets.hpp>
  18. #include <boost/utility.hpp>
  19. #include <boost/graph/graph_utility.hpp>
  20. /*
  21. This example demonstrates the usage of the
  22. connected_components_on_edgelist algorithm. This differs from the
  23. connect_components algorithm in that the graph object
  24. only needs to provide access to the "list" of edges (via the
  25. edges() function).
  26. The example graphs come from "Introduction to
  27. Algorithms", Cormen, Leiserson, and Rivest p. 87 (though we number
  28. the vertices from zero instead of one).
  29. Sample output:
  30. An undirected graph (edge list):
  31. (0,1) (1,4) (4,0) (2,5)
  32. Total number of components: 3
  33. Vertex 0 is in the component who's representative is 1
  34. Vertex 1 is in the component who's representative is 1
  35. Vertex 2 is in the component who's representative is 5
  36. Vertex 3 is in the component who's representative is 3
  37. Vertex 4 is in the component who's representative is 1
  38. Vertex 5 is in the component who's representative is 5
  39. component 0 contains: 4 1 0
  40. component 1 contains: 3
  41. component 2 contains: 5 2
  42. */
  43. using namespace std;
  44. using boost::tie;
  45. int main(int , char* [])
  46. {
  47. using namespace boost;
  48. typedef int Index; // ID of a Vertex
  49. typedef pair<Index,Index> Edge;
  50. const int N = 6;
  51. const int E = 4;
  52. Edge edgelist[] = { Edge(0, 1), Edge(1, 4), Edge(4, 0), Edge(2, 5) };
  53. edge_list<Edge*,Edge,ptrdiff_t,std::random_access_iterator_tag> g(edgelist, edgelist + E);
  54. cout << "An undirected graph (edge list):" << endl;
  55. print_edges(g, identity_property_map());
  56. cout << endl;
  57. disjoint_sets_with_storage<> ds(N);
  58. incremental_components(g, ds);
  59. component_index<int> components(&ds.parents()[0],
  60. &ds.parents()[0] + ds.parents().size());
  61. cout << "Total number of components: " << components.size() << endl;
  62. for (int k = 0; k != N; ++k)
  63. cout << "Vertex " << k << " is in the component who's representative is "
  64. << ds.find_set(k) << endl;
  65. cout << endl;
  66. for (std::size_t i = 0; i < components.size(); ++i) {
  67. cout << "component " << i << " contains: ";
  68. component_index<int>::component_iterator
  69. j = components[i].first,
  70. jend = components[i].second;
  71. for ( ; j != jend; ++j)
  72. cout << *j << " ";
  73. cout << endl;
  74. }
  75. return 0;
  76. }