graph_stats.hpp 3.7 KB

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