summaryrefslogtreecommitdiff
path: root/test/other/fastSubsetSum.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 15:39:23 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 15:39:23 +0100
commit72bd993483453ed8ebc462f1a33385cd355d486f (patch)
treec5592ba1ed2fed79e26ba6158d097c9ceb43f061 /test/other/fastSubsetSum.cpp
parent98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (diff)
parent35d485bcf6a9ed0a9542628ce4aa94a3326d0884 (diff)
merge mzuenni changes
Diffstat (limited to 'test/other/fastSubsetSum.cpp')
-rw-r--r--test/other/fastSubsetSum.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/other/fastSubsetSum.cpp b/test/other/fastSubsetSum.cpp
new file mode 100644
index 0000000..c61b1ea
--- /dev/null
+++ b/test/other/fastSubsetSum.cpp
@@ -0,0 +1,49 @@
+#include "../util.h"
+#include <other/fastSubsetSum.cpp>
+
+int subsetSum(vector<int> w, int t){
+ vector<bool> dp(t+1);
+ dp[0] = true;
+ for(int x : w){
+ for(int i = t; i >= x; i--){
+ dp[i] = dp[i] || dp[i-x];
+ }
+ }
+ int ma = 0;
+ for(int i = 0; i <= t; i++){
+ if(dp[i]) ma = i;
+ }
+ return ma;
+}
+
+void stress_test() {
+ int queries = 0;
+ for (int test = 0; test < 10'000; test++) {
+ int n = Random::integer<int>(1, 20);
+ int t = Random::integer<int>(1, 500);
+ vector<int> w = Random::integers<int>(n, 0, 50);
+ int got = fastSubsetSum(w, t);
+ int expected = subsetSum(w, t);
+ if(got != expected) cerr << "got: " << got << " expected: " << expected << FAIL;
+ queries++;
+ }
+ cerr << "tested queries: " << queries << endl;
+}
+
+void performance_test() {
+ timer t;
+ int n = 10'000, a = 10'000;
+ vector<int> w = Random::integers<int>(n, 0, a);
+ int target = 10'000'000;
+ t.start();
+ hash_t hash = fastSubsetSum(w, target);
+ t.stop();
+ if (t.time > 750) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
+}
+
+int main() {
+ stress_test();
+ performance_test();
+}
+