-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add approximate counting algorithm in C #844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leios
merged 3 commits into
algorithm-archivists:master
from
mahdisarikhani:approximate_counting_c
Oct 11, 2021
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
contents/approximate_counting/code/c/approximate_counting.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <assert.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
// This function returns a pseudo-random number between 0 and 1 | ||
double drand() | ||
{ | ||
return (double)rand() / RAND_MAX; | ||
} | ||
|
||
// This function takes | ||
// - v: value in register | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns the approximate count | ||
double n(double v, double a) | ||
{ | ||
return a * (pow(1 + 1 / a, v) - 1); | ||
} | ||
|
||
// This function takes | ||
// - v: value in register | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns a new value for v | ||
double increment(double v, double a) | ||
{ | ||
// delta is the probability of incrementing our counter | ||
double delta = 1 / (n(v + 1, a) - n(v, a)); | ||
|
||
if (drand() <= delta) { | ||
return v + 1; | ||
} | ||
return v; | ||
} | ||
|
||
// This function simulates counting and takes | ||
// - n_items: number of items to count and loop over | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// It returns n(v, a), the approximate count | ||
double approximate_count(size_t n_items, double a) | ||
{ | ||
int v = 0; | ||
for (size_t i = 0; i < n_items; ++i) { | ||
v = increment(v, a); | ||
} | ||
|
||
return n(v, a); | ||
} | ||
|
||
// This function takes | ||
// - n_trials: the number of counting trials | ||
// - n_items: the number off items to count | ||
// - a: a scaling value for the logarithm based on Morris's paper | ||
// - threshold: the maximum percent error allowed | ||
// It terminates the program on failure | ||
void test_approximation_count(size_t n_trials, size_t n_items, double a, | ||
double threshold) | ||
{ | ||
double sum = 0.0; | ||
for (size_t i = 0; i < n_trials; ++i) { | ||
sum += approximate_count(n_items, a); | ||
} | ||
double avg = sum / n_trials; | ||
|
||
assert(fabs((avg - n_items) / n_items) < threshold); | ||
} | ||
|
||
int main() | ||
{ | ||
srand(time(NULL)); | ||
|
||
printf("Counting Tests, 100 trials\n"); | ||
printf("testing 1000, a = 30, 1%% error\n"); | ||
test_approximation_count(100, 1000, 30, 0.1); | ||
printf("testing 12345, a = 10, 1%% error\n"); | ||
test_approximation_count(100, 12345, 10, 0.1); | ||
printf("testing 222222, a = 0.5, 10%% error\n"); | ||
test_approximation_count(100, 222222, 0.5, 0.1); | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.