Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.o
bin/*
23 changes: 13 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Copy link
Copy Markdown
Owner

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?

./bin/benchmark
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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
17 changes: 2 additions & 15 deletions examples/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@

#include "../inputGenerator.hpp"

using namespace std::chrono;

template<class duration_type>
double time_elapsed(const duration_type & d) {
return duration_cast<duration<double>>(d).count();
}

template<class function>
double time_taken(function f) {
auto start = system_clock::now();
#include "benchmark.hpp"

f();
auto end = system_clock::now();

return time_elapsed(end - start);
}
using namespace std::chrono;

void testRandomInt() {
std::cout << "Generating 100.000.000 numbers in range 0 MAX_INT: " << std::endl;
Expand Down
18 changes: 18 additions & 0 deletions examples/benchmark.hpp
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);
}
239 changes: 239 additions & 0 deletions examples/countfefete.cpp
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;
}
1 change: 1 addition & 0 deletions inputGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
#include "src/tree.hpp"
#include "src/bipartite.hpp"
#include "src/undirected_graph.hpp"
#include "src/advanced_tree.hpp"
Loading