graph_stats.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/graph_traits.hpp>
  12. #include <boost/graph/iteration_macros.hpp>
  13. #include <boost/graph/properties.hpp>
  14. #include <boost/assert.hpp>
  15. namespace boost
  16. {
  17. namespace graph
  18. {
  19. template < typename Graph > struct sort_edge_by_origin
  20. {
  21. public:
  22. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  23. explicit sort_edge_by_origin(Graph& g) : g(g) {}
  24. inline bool operator()(edge_type a, edge_type b)
  25. {
  26. return source(a, g) == source(b, g) ? target(a, g) < target(b, g)
  27. : source(a, g) < source(b, g);
  28. }
  29. private:
  30. Graph& g;
  31. };
  32. template < typename Graph > struct equal_edge
  33. {
  34. public:
  35. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  36. explicit equal_edge(Graph& g) : g(g) {}
  37. inline bool operator()(edge_type a, edge_type b)
  38. {
  39. return source(a, g) == source(b, g) && target(a, g) == target(b, g);
  40. }
  41. private:
  42. Graph& g;
  43. };
  44. template < typename Graph > unsigned long num_dup_edges(Graph& g)
  45. {
  46. typedef typename graph_traits< Graph >::edge_iterator e_iterator_type;
  47. typedef typename graph_traits< Graph >::edge_descriptor edge_type;
  48. std::list< edge_type > all_edges;
  49. BGL_FORALL_EDGES_T(e, g, Graph) { all_edges.push_back(e); }
  50. sort_edge_by_origin< Graph > cmp1(g);
  51. all_edges.sort(cmp1);
  52. equal_edge< Graph > cmp2(g);
  53. all_edges.unique(cmp2);
  54. return num_edges(g) - all_edges.size();
  55. }
  56. template < typename Graph >
  57. std::map< unsigned long, unsigned long > dup_edge_dist(Graph& g)
  58. {
  59. std::map< unsigned long, unsigned long > dist;
  60. typedef
  61. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  62. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  63. BGL_FORALL_VERTICES_T(v, g, Graph)
  64. {
  65. std::list< vertex_type > front_neighbors;
  66. a_iterator_type a_iter, a_end;
  67. for (boost::tie(a_iter, a_end) = adjacent_vertices(v, g);
  68. a_iter != a_end; ++a_iter)
  69. {
  70. front_neighbors.push_back(*a_iter);
  71. }
  72. front_neighbors.sort();
  73. front_neighbors.unique();
  74. dist[out_degree(v, g) - front_neighbors.size()] += 1;
  75. }
  76. return dist;
  77. }
  78. template < typename Graph >
  79. std::map< unsigned long, unsigned long > degree_dist(Graph& g)
  80. {
  81. std::map< unsigned long, unsigned long > dist;
  82. typedef
  83. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  84. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  85. BGL_FORALL_VERTICES_T(v, g, Graph) { dist[out_degree(v, g)] += 1; }
  86. return dist;
  87. }
  88. template < typename Graph >
  89. std::map< unsigned long, double > weight_degree_dist(Graph& g)
  90. {
  91. std::map< unsigned long, double > dist, n;
  92. typedef
  93. typename graph_traits< Graph >::adjacency_iterator a_iterator_type;
  94. typedef typename graph_traits< Graph >::vertex_descriptor vertex_type;
  95. typedef typename property_map< Graph, edge_weight_t >::const_type
  96. edge_map_type;
  97. typedef typename property_traits< edge_map_type >::value_type
  98. edge_weight_type;
  99. typename property_map< Graph, edge_weight_t >::type em
  100. = get(edge_weight, g);
  101. BGL_FORALL_VERTICES_T(v, g, Graph)
  102. {
  103. edge_weight_type tmp = 0;
  104. BGL_FORALL_OUTEDGES_T(v, e, g, Graph) { tmp += em[e]; }
  105. n[out_degree(v, g)] += 1.;
  106. dist[out_degree(v, g)] += tmp;
  107. }
  108. for (std::map< unsigned long, double >::iterator iter = dist.begin();
  109. iter != dist.end(); ++iter)
  110. {
  111. BOOST_ASSERT(n[iter->first] != 0);
  112. dist[iter->first] /= n[iter->first];
  113. }
  114. return dist;
  115. }
  116. }
  117. }
  118. #endif