Add random example

This commit is contained in:
stefiosif
2022-10-14 18:38:40 +03:00
parent 92cf8950c2
commit 04ab33888e

43
src/random_example.cc Normal file
View File

@@ -0,0 +1,43 @@
#include "roditty_zwick.h"
#include <random>
int main() {
graph::Digraph<std::uint16_t> G;
auto gen = std::bind(std::uniform_real_distribution<>(0, 1),
std::default_random_engine());
std::vector<std::pair<std::uint16_t, std::uint16_t>> dl_set;
std::vector<std::pair<std::uint16_t, std::uint16_t>> qr_set;
for (int i = 0; i <= 100; ++i) {
for (int j = 0; j <= 100; ++j) {
auto rand = gen();
if (rand < 0.5) {
G.insert(i, j);
if (rand < 0.25)
dl_set.push_back({ i, j });
if (rand < 0.125)
qr_set.push_back({ i, j });
}
}
}
// Initialize the Roditty and Zwick's dynamic reachability algorithm
// for general directed graphs inspired by Henzinger and King
algo::HenzingerKing<std::uint16_t>* dr = new algo::HenzingerKing(G);
dr->init();
// Remove a random collection of edges from the digraph
dr->remove(dl_set);
// Answer queries using the maintained transitive closure, if the
// queried edge has been deleted, insert it into the digraph again
for (const auto& q : qr_set) {
if (dr->query(q.first, q.second))
;
else
dr->insert(q.first, q.second);
}
return 0;
}