graph_stats.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Alex Breuer
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_GRAPH_STATS_HPP
  8. #define BOOST_GRAPH_GRAPH_STATS_HPP
  9. #include <map>
  10. #include <list>
  11. #include <boost/graph/iteration_macros.hpp>
  12. #include <boost/assert.hpp>
  13. namespace boost
  14. {
  15. namespace graph
  16. {
  17. template < typename Graph > struct sort_edge_by_origin
  18. {
  19. public:
  20. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  21. explicit sort_edge_by_origin(Graph& g) : g(g) {}
  22. inline bool operator()(edge_type a, edge_type b)
  23. {
  24. return source(a, g) == source(b, g) ? target(a, g) < target(b, g)
  25. : source(a, g) < source(b, g);
  26. }
  27. private:
  28. Graph& g;
  29. };
  30. template < typename Graph > struct equal_edge
  31. {
  32. public:
  33. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  34. explicit equal_edge(Graph& g) : g(g) {}
  35. inline bool operator()(edge_type a, edge_type b)
  36. {
  37. return source(a, g) == source(b, g) && target(a, g) == target(b, g);
  38. }
  39. private:
  40. Graph& g;
  41. };
  42. template < typename Graph > unsigned long num_dup_edges(Graph& g)
  43. {
  44. typedef typename graph_traits< Graph >::edge_iterator e_iterator_type;
  45. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  46. std::list< edge_type > all_edges;
  47. BGL_FORALL_EDGES_T(e, g, Graph) { all_edges.push_back(e); }
  48. sort_edge_by_origin< Graph > cmp1(g);
  49. all_edges.sort(cmp1);
  50. equal_edge< Graph > cmp2(g);
  51. all_edges.unique(cmp2);
  52. return num_edges(g) - all_edges.size();
  53. }
  54. template < typename Graph >
  55. std::map< unsigned long, unsigned long > dup_edge_dist(Graph& g)
  56. {
  57. std::map< unsigned long, unsigned long > dist;
  58. typedef
  59. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  60. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  61. BGL_FORALL_VERTICES_T(v, g, Graph)
  62. {
  63. std::list< vertex_type > front_neighbors;
  64. a_iterator_type a_iter, a_end;
  65. for (boost::tie(a_iter, a_end) = adjacent_vertices(v, g);
  66. a_iter != a_end; ++a_iter)
  67. {
  68. front_neighbors.push_back(*a_iter);
  69. }
  70. front_neighbors.sort();
  71. front_neighbors.unique();
  72. dist[out_degree(v, g) - front_neighbors.size()] += 1;
  73. }
  74. return dist;
  75. }
  76. template < typename Graph >
  77. std::map< unsigned long, unsigned long > degree_dist(Graph& g)
  78. {
  79. std::map< unsigned long, unsigned long > dist;
  80. typedef
  81. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  82. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  83. BGL_FORALL_VERTICES_T(v, g, Graph) { dist[out_degree(v, g)] += 1; }
  84. return dist;
  85. }
  86. template < typename Graph >
  87. std::map< unsigned long, double > weight_degree_dist(Graph& g)
  88. {
  89. std::map< unsigned long, double > dist, n;
  90. typedef
  91. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  92. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  93. typedef typename property_map< Graph, edge_weight_t >::const_type
  94. edge_map_type;
  95. typedef typename property_traits< edge_map_type >::value_type
  96. edge_weight_type;
  97. typename property_map< Graph, edge_weight_t >::type em
  98. = get(edge_weight, g);
  99. BGL_FORALL_VERTICES_T(v, g, Graph)
  100. {
  101. edge_weight_type tmp = 0;
  102. BGL_FORALL_OUTEDGES_T(v, e, g, Graph) { tmp += em[e]; }
  103. n[out_degree(v, g)] += 1.;
  104. dist[out_degree(v, g)] += tmp;
  105. }
  106. for (std::map< unsigned long, double >::iterator iter = dist.begin();
  107. iter != dist.end(); ++iter)
  108. {
  109. BOOST_ASSERT(n[iter->first] != 0);
  110. dist[iter->first] /= n[iter->first];
  111. }
  112. return dist;
  113. }
  114. }
  115. }
  116. #endif