fibonacci_heap.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <vector>
  12. #include <boost/graph/random.hpp>
  13. #ifndef BOOST_NO_CXX11_HDR_RANDOM
  14. #include <random>
  15. namespace random_ns = std;
  16. #else
  17. #include <boost/random/mersenne_twister.hpp>
  18. namespace random_ns = boost;
  19. #endif
  20. #include <algorithm>
  21. #include <boost/pending/fibonacci_heap.hpp>
  22. #include <boost/graph/graph_utility.hpp>
  23. #include <boost/pending/indirect_cmp.hpp>
  24. using namespace boost;
  25. int
  26. main()
  27. {
  28. typedef indirect_cmp<float*,std::less<float> > ICmp;
  29. int i;
  30. random_ns::mt19937 gen;
  31. for (int N = 2; N < 200; ++N) {
  32. uniform_int<> distrib(0, N-1);
  33. boost::variate_generator<random_ns::mt19937&, uniform_int<> > rand_gen(gen, distrib);
  34. for (int t = 0; t < 10; ++t) {
  35. std::vector<float> v, w(N);
  36. ICmp cmp(&w[0], std::less<float>());
  37. fibonacci_heap<int, ICmp> Q(N, cmp);
  38. for (int c = 0; c < w.size(); ++c)
  39. w[c] = c;
  40. #ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
  41. std::random_shuffle(w.begin(), w.end());
  42. #else
  43. std::shuffle(w.begin(), w.end(), gen);
  44. #endif
  45. for (i = 0; i < N; ++i)
  46. Q.push(i);
  47. for (i = 0; i < N; ++i) {
  48. int u = rand_gen();
  49. float r = rand_gen(); r /= 2.0;
  50. w[u] = w[u] - r;
  51. Q.update(u);
  52. }
  53. for (i = 0; i < N; ++i) {
  54. v.push_back(w[Q.top()]);
  55. Q.pop();
  56. }
  57. std::sort(w.begin(), w.end());
  58. if (! std::equal(v.begin(), v.end(), w.begin())) {
  59. std::cout << std::endl << "heap sorted: ";
  60. std::copy(v.begin(), v.end(),
  61. std::ostream_iterator<float>(std::cout," "));
  62. std::cout << std::endl << "correct: ";
  63. std::copy(w.begin(), w.end(),
  64. std::ostream_iterator<float>(std::cout," "));
  65. std::cout << std::endl;
  66. return -1;
  67. }
  68. }
  69. }
  70. std::cout << "fibonacci heap passed test" << std::endl;
  71. return 0;
  72. }