Advertisement
Guest User

Sidon Sets

a guest
Feb 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3.  
  4. //For timing
  5. #include <chrono>
  6. using namespace std::chrono;
  7.  
  8. using namespace std;
  9.  
  10. static int totalSets = 0;
  11. static int maxSize = 0;
  12. static set<int> largestset;
  13.  
  14. void printSet(set<int> myset) {
  15. cout << "Sidon size " << myset.size() << ": ";
  16. for (int elem : myset) {
  17. cout << elem << ", ";
  18. }
  19.  
  20. cout << endl;
  21. }
  22.  
  23. static int sizeMinToPrint = 1;
  24. void countSet(set<int> myset) {
  25. totalSets++;
  26.  
  27. if (myset.size() > maxSize) {
  28. maxSize = myset.size();
  29. largestset = myset;
  30.  
  31. }
  32.  
  33. return;
  34. }
  35.  
  36.  
  37. void sidon(int n, int max, set<int>& progress, set<int>& sums) {
  38.  
  39. //base case
  40. if (n > max) return;
  41.  
  42. bool validSet = true;
  43.  
  44. //Now we must verify that n will not make a non-distinct sum
  45. //We check that every sum of n and an element in the set is new
  46. progress.insert(n);
  47.  
  48. for (int elem : progress) {
  49. int newSum = elem + n;
  50. //A sum is already possible; not a sidon set
  51. if (sums.count(newSum)) {
  52. validSet = false;
  53. break;
  54. }
  55. }
  56.  
  57. //If the set including n is a sidon set, we try to add it and recurse
  58. if (validSet) {
  59. for (int elem : progress) {
  60. int newSum = elem + n;
  61. sums.insert(elem+n);
  62. }
  63. //Print out progress as possible sidon set
  64. countSet(progress);
  65.  
  66. sidon(n+1, max, progress, sums);
  67.  
  68. //Remove all sums including n
  69. //This is done so we do not have to make copies of the set, increasing performance
  70. for (int elem : progress) {
  71. int newSum = elem + n;
  72. sums.erase(newSum);
  73. }
  74. }
  75.  
  76.  
  77. //Unchoose
  78. progress.erase(n);
  79.  
  80. //Try to make sidon sets not including n
  81. sidon(n+1, max, progress, sums);
  82.  
  83. }
  84.  
  85. void sidon(int max) {
  86. totalSets = 0;
  87. maxSize = 0;
  88.  
  89. set<int> progress;
  90. set<int> sums;
  91. sidon(1, max, progress, sums);
  92. cout << "Found " << totalSets << " Sidon sets from 1 to " << max << "." << endl;
  93. cout << "The max set size is " << maxSize << "." << endl;
  94. cout << "One such largest set is ";
  95. printSet(largestset);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. int main()
  102. {
  103. for(int i = 0; i < 10; i++) {
  104. cout << "." << endl;
  105. }
  106.  
  107. // Get starting timepoint
  108. auto start = steady_clock::now();
  109.  
  110. sidon(26);
  111.  
  112. // Get ending timepoint
  113. auto stop = steady_clock::now();
  114. auto duration = duration_cast<milliseconds>(stop - start);
  115.  
  116. cout << "Time taken by function: "
  117. << duration.count() << " millseconds" << endl;
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement