-
Notifications
You must be signed in to change notification settings - Fork 1
Add advanced tree utilities #1
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
Open
ericpts
wants to merge
3
commits into
adrian-budau:master
Choose a base branch
from
ericpts:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| **/*.o | ||
| bin/* |
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 |
|---|---|---|
|
|
@@ -5,22 +5,18 @@ FOLDER=/usr/local/include/src | |
| SOURCES = $(wildcard examples/*.cpp) | ||
| OBJECTS = $(SOURCES:.cpp=.o) | ||
|
|
||
| CFLAGS = -Wall -Wextra -O2 -std=c++0x -I$(CURDIR) -pedantic -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wformat=2 -Winit-self -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror | ||
| CFLAGS = -Wall -Wextra -Og -g -std=c++11 -I$(CURDIR) -pedantic -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wformat=2 -Winit-self -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wstrict-overflow=2 -Wswitch-default -Wundef -Werror -Iexamples/ | ||
| LDFLAGS = -lm | ||
|
|
||
| install: | ||
| @if [ -a $(FOLDER) ]; then echo "Folder src already exists in \"/usr/local/include\", sorry but I don't know what to do, I hope it's me :-)" && exit -1; else exit 0; fi; | ||
| @if [ -d $(FOLDER) ]; then echo "Folder src already exists in \"/usr/local/include\", sorry but I don't know what to do, I hope it's me :-)" && exit 1; else exit 0; fi; | ||
| @sudo ln -s $(CURDIR)/src/ /usr/local/include/src | ||
| @sudo ln -s $(CURDIR)/inputGenerator.hpp /usr/local/include/inputGenerator.hpp | ||
| @echo "Install OK" | ||
|
|
||
| clean: | ||
| @mkdir tmp | ||
| @rm -rf examples/*.dSYM | ||
| @mv examples/*.cpp tmp/ | ||
| @rm -f examples/* | ||
| @mv tmp/* examples/ | ||
| @rm -rf tmp | ||
| @rm -f examples/*.o | ||
| @rm -f src/*.gch | ||
| @echo "Clean OK" | ||
|
|
||
|
|
@@ -37,10 +33,17 @@ lint: | |
| find . -name "*.hpp" | xargs python2 cpplint.py --filter=-legal --counting=detailed | ||
|
|
||
| benchmark: | ||
| $(CXX) $(CFLAGS) examples/benchmark.cpp -o examples/benchmark $(LDFLAGS) | ||
| ./examples/benchmark | ||
| $(CXX) $(CFLAGS) -DINPUT_GENERATOR_DEBUG examples/benchmark.cpp -o bin/benchmark $(LDFLAGS) | ||
| ./bin/benchmark | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Countfefete shouldnt be here i guess? |
||
|
|
||
| countfefete: | ||
| $(CXX) $(CFLAGS) -DINPUT_GENERATOR_DEBUG examples/countfefete.cpp -o bin/countfefete $(LDFLAGS) | ||
| @./bin/countfefete 17 10000 1000000000 0 | ||
| @./bin/countfefete 18 40000 1000000000 0 | ||
| @./bin/countfefete 19 70000 1000000000 0 | ||
| @./bin/countfefete 20 100000 1000000000 0 | ||
|
|
||
| %.o: %.cpp | ||
| $(CXX) $(CFLAGS) $< -o $@ $(LDFLAGS) | ||
|
|
||
| test: $(OBJECTS) benchmark | ||
| test: $(OBJECTS) benchmark countfefete | ||
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
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,18 @@ | ||
| #include <chrono> | ||
|
|
||
| using namespace std::chrono; | ||
|
|
||
| template<typename duration_type> | ||
| double time_elapsed(const duration_type & d) { | ||
| return duration_cast<duration<double>>(d).count(); | ||
| } | ||
|
|
||
| template<typename function> | ||
| double time_taken(function f) { | ||
| auto start = system_clock::now(); | ||
|
|
||
| f(); | ||
| auto end = system_clock::now(); | ||
|
|
||
| return time_elapsed(end - start); | ||
| } |
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,239 @@ | ||
| #include <iostream> | ||
| #include <cassert> | ||
| #include <functional> | ||
| #include <random> | ||
| #include <sstream> | ||
|
|
||
| #include "../inputGenerator.hpp" | ||
|
|
||
| #include "benchmark.hpp" | ||
|
|
||
| using namespace std; | ||
| using namespace inputGenerator; | ||
|
|
||
| using node_value_t = int; | ||
|
|
||
| using gen = AdvancedTreeGenerator<node_value_t>; | ||
| using graph_t = gen::graph_t; | ||
| using Tree = gen::Tree; | ||
| using HTree = gen::HTree; | ||
| using Chain = gen::Chain; | ||
| using Star = gen::Star; | ||
| using RandomTree = gen::RandomTree; | ||
| using WideRandomTree = gen::WideRandomTree; | ||
|
|
||
| void init(int seed = 1337) | ||
| { | ||
| Seed::logging = false; | ||
| Seed::create(seed); | ||
| } | ||
|
|
||
| bool random_flip(double p) | ||
| { | ||
| uniform_real_distribution<double>d (0, 1); | ||
| return d(Generator::getGenerator()) < p; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| string format(graph_t V) | ||
| { | ||
| ostringstream cout; | ||
| V.Index(1); | ||
| cout << V.size() << "\n"; | ||
| int req = V.size() - 1; | ||
|
|
||
|
|
||
| for (const auto& n : V) { | ||
| cout << n.data() << " "; | ||
| } | ||
| cout.seekp(-1, ios_base::end); | ||
| cout << "\n"; | ||
|
|
||
| for (const auto& e : V.edges()) | ||
| { | ||
| cout << e.from().index() << " " << e.to().index() << "\n"; | ||
| req--; | ||
| } | ||
|
|
||
| assert (req == 0); | ||
|
|
||
| return cout.str(); | ||
| } | ||
|
|
||
| struct countfefete | ||
| { | ||
| int n; | ||
|
|
||
| countfefete(int n): | ||
| n(n) | ||
| { } | ||
|
|
||
| Tree build(Tree acum) | ||
| { | ||
| while (true) { | ||
| assert (acum->size() <= n); // Too many nodes already. Abort! | ||
|
|
||
| if (acum->size() == n) { | ||
| return acum; | ||
| } | ||
|
|
||
| int rem = n - acum->size(); | ||
|
|
||
| assert (rem > 0); | ||
|
|
||
| // Base cases, because it's hard to calculate the exact node count | ||
| // for the fancier types of trees. | ||
| // | ||
| // +1 for rem because when two trees are merged, one of the nodes is dropped. | ||
| if (rem <= 5) { | ||
| acum = RandomTree::make(rem + 1) | ||
| .combine(acum); | ||
| continue; | ||
| } | ||
| if (rem <= 15) { | ||
| acum = WideRandomTree::make(rem + 1, rem / 2 + 1) | ||
| .combine(acum); | ||
| continue; | ||
| } | ||
|
|
||
| auto comb = [&] (Tree t) | ||
| { | ||
| if (t->size() <= rem + 1) { | ||
| acum = t.combine(acum); | ||
| } | ||
| assert (acum->size() <= n); | ||
| }; | ||
|
|
||
| vector<function<void()>> transitions = { | ||
| [&] () { | ||
| int lstar = random_int(2, 10); | ||
| comb(Star::make(lstar)); | ||
| }, | ||
| [&]() { | ||
| int lchain = random_int(2, 10); | ||
| comb(Chain::make(lchain)); | ||
| }, | ||
| [&]() { | ||
| comb(HTree::make_nchains(10, 3, 3)); | ||
| }, | ||
| [&]() { | ||
| comb(HTree::make_nchains(10, 5, 5)); | ||
| }, | ||
| [&]() { | ||
| comb(WideRandomTree::make(30, 10)); | ||
| }, | ||
| [&]() { | ||
| comb(RandomTree::make(30)); | ||
| }, | ||
| [&]() { | ||
| comb(RandomTree::make(10)); | ||
| }, | ||
| [&]() { | ||
| comb(WideRandomTree::make(50, 30)); | ||
| }, | ||
| [&]() { | ||
| comb(countfefete(sqrt(n)).build(Chain::make(2))); | ||
| } | ||
| }; | ||
|
|
||
| // Randomly choose a transition and then continue. | ||
| (*random_choice(transitions))(); | ||
| } | ||
| } | ||
|
|
||
| static Tree generate(int seed, int N, int maxval, double p) | ||
| { | ||
| init(seed); | ||
| int x = 1; | ||
| auto next_value = [&] () -> int | ||
| { | ||
| // Give it an old value. | ||
| if (random_flip(p)) { | ||
| return random_int(1, x); | ||
| } | ||
|
|
||
| // Otherwise give it a new, unique id. | ||
| // Note that this might be reused at a later point. | ||
| return x++; | ||
| }; | ||
|
|
||
| auto tree = countfefete(N).build(Chain::make(2)); | ||
|
|
||
| for (auto &node : tree.m_graph) { | ||
| node.data() = next_value(); | ||
| } | ||
|
|
||
| assert (tree->size() == N); | ||
|
|
||
| set<int> values_that_appear; | ||
|
|
||
| for (const auto & node : tree.m_graph) { | ||
| values_that_appear.insert(node.data()); | ||
| } | ||
|
|
||
| assert (maxval >= static_cast<int>(values_that_appear.size())); // Impossible to assign values otherwise. | ||
|
|
||
| // We want to keep the relative ordering of the node values. | ||
| // e.g. val_for_perm[x] <= val_for_perm[y] <-> x <= y. | ||
| // This is because of the way the trees are structured, built on layers upon layers. | ||
| // So the outermost leaves will have the lowest values, then their descendants and so on and so forth. | ||
| map<int, int> val_for_perm; | ||
| { | ||
|
|
||
| set<int> incl; | ||
| for (size_t i = 0; i < values_that_appear.size(); ++i) { | ||
| int val; | ||
| do{ | ||
| val = random_int(1, maxval); | ||
| } while (incl.count(val)); | ||
| incl.insert(val); | ||
| } | ||
|
|
||
| assert (incl.size() == values_that_appear.size()); | ||
|
|
||
| auto src = values_that_appear.begin(); | ||
| auto dst = incl.begin(); | ||
|
|
||
| while (src != values_that_appear.end() && | ||
| dst != incl.end()) { | ||
| val_for_perm[*src] = *dst; | ||
| ++src; | ||
| ++dst; | ||
| } | ||
|
|
||
| assert (src == values_that_appear.end()); | ||
| assert (dst == incl.end()); | ||
| } | ||
|
|
||
| for (auto & node : tree.m_graph) { | ||
| node.data() = val_for_perm[node.data()]; | ||
| assert (1 <= node.data() && node.data() <= maxval); | ||
| } | ||
|
|
||
| return tree; | ||
| } | ||
| }; | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| if (argc != 5) { | ||
| cerr << "Usage: " << argv[0] << " seed N maxval prob\n"; | ||
| cerr << "Generate a tree with N nodes, with values from 0..valmax in nodes.\n"; | ||
| cerr << "Each node has a probability of `prob` to be assigned an old value and (1 - prob) to be given a new one\n"; | ||
| exit(-1); | ||
| } | ||
| vector<string> arg(argv, argv + argc); | ||
| int seed = stoi(arg[1]); | ||
| int N = stoi(arg[2]); | ||
| int maxval = stoi(arg[3]); | ||
| double p = stod(arg[4]); | ||
|
|
||
| std::cout | ||
| << "Generating a countfefete input with " << N << " nodes takes " << | ||
| time_taken([&]() | ||
| { | ||
| countfefete::generate(seed, N, maxval, p); | ||
| }) << std::endl; | ||
|
|
||
| return 0; | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably run the benchmark without the debug checks?