← comparison
tgen
Loading...
Searching...
No Matches
Hacks

Adversarial generation for specific problems. More...

Functions

std::string tgen::hack::abacaba (int n)
 Returns the prefix of the infinite word "abacabad...".
std::pair< std::string, std::string > tgen::hack::unsigned_polynomial_hash ()
 Returns two strings that force polynomial hash collision for power-of-two mod.
std::pair< std::string, std::string > tgen::hack::polynomial_hash (int alphabet_size, int base, int mod)
 Returns two strings that force polynomial hash collision given base and mod.
std::pair< std::string, std::string > tgen::hack::polynomial_hash (int alphabet_size, std::vector< int > bases, std::vector< int > mods)
 Returns two strings that force polynomial hash collision given bases and mods.
std::vector< long long > tgen::hack::std_unordered (int size)
 List of integers that tries to force collision on std::unordered_set.
std::vector< std::pair< int, int > > tgen::hack::mo_worst_case (int n, int q)
 Query list that forces asymptotic worst-case for Mo's algorithm.
std::vector< std::string > tgen::hack::string_set_worst_case (int size)
 List of strings that have high cost to insert in a std::set.
egraph< int >::value tgen::hack::non_strict_relaxation_dijkstra_bug (int n)
 Directed weighted graph for Dijkstra with non-strict relaxation.
egraph< int >::value tgen::hack::stale_heap_dijkstra_bug (int n)
 Directed weighted graph for Dijkstra without a stale-heap check.
egraph< int >::value tgen::hack::spfa (int n)
 Worst-case for FIFO-SPFA.
egraph< int >::value tgen::hack::dinitz_worst_case (int k, int l)
 Flow network for Edmonds-Karp and Dinitz worst-case.
std::vector< geometry::point< double > > tgen::hack::naive_rotating_calipers_max_dist_bug ()
 Convex polygon that breaks naive rotating calipers for maximum distance.
template<typename T>
std::vector< bool > tgen::hack::mt19937_xor_hash ()
 Mask that forces a zero XOR hash from std::mt19937 or std::mt19937_64.
std::pair< std::vector< int >, std::vector< std::vector< int > > > tgen::hack::segment_tree_beats_worst_case (int k, int q)
 Array and updates for worst case of segment tree beats.

Detailed Description

Adversarial generation for specific problems.

Examples

// Creates values that force collision on std::unordered_set.
std::vector<long long> unord_hack = tgen::hack::std_unordered(1e6);
// Creates queries that force worst-case for Mo's algorithm.
std::vector<std::pair<int, int>> mo_hack = tgen::hack::mo_worst_case(1e6, 1e6);
// Mask for XOR-hashing std::mt19937 outputs to zero.
std::vector<bool> xor_mask = tgen::hack::mt19937_xor_hash<int>();
// Mask for XOR-hashing std::mt19937_64 outputs to zero.
std::vector<bool> xor_mask_64 = tgen::hack::mt19937_xor_hash<long long>();
// Hack for segment tree beats: initial array and update list.
auto [arr, updates] = tgen::hack::segment_tree_beats_worst_case(3, 146);
std::vector< std::pair< int, int > > mo_worst_case(int n, int q)
Query list that forces asymptotic worst-case for Mo's algorithm.
Definition tgen.h:7159
std::vector< bool > mt19937_xor_hash()
Mask that forces a zero XOR hash from std::mt19937 or std::mt19937_64.
Definition tgen.h:7338
std::pair< std::vector< int >, std::vector< std::vector< int > > > segment_tree_beats_worst_case(int k, int q)
Array and updates for worst case of segment tree beats.
Definition tgen.h:7460
std::vector< long long > std_unordered(int size)
List of integers that tries to force collision on std::unordered_set.
Definition tgen.h:7137

Function Documentation

◆ abacaba()

std::string tgen::hack::abacaba ( int n)
inline

Returns the prefix of the infinite word "abacabad...".

Parameters
nLength of the prefix.
Returns
A string with the prefix of length n of "abacabad...".

Complexity

O(n).

Examples

// Prints "abacaba".
std::cout << tgen::hack::abacaba(7) << std::endl;
std::string abacaba(int n)
Returns the prefix of the infinite word "abacabad...".
Definition tgen.h:7064

Definition at line 7064 of file tgen.h.

◆ dinitz_worst_case()

egraph< int >::value tgen::hack::dinitz_worst_case ( int k,
int l )
inline

Flow network for Edmonds-Karp and Dinitz worst-case.

Parameters
kCore size.
lSource and sink chain length.
Precondition
k >= 1, l >= 1.

Builds the Zadeh (1972) anti-shortest-paths network, a worst-case instance for both Edmonds-Karp and Dinitz. The source is vertex 0 and the sink is vertex 4l + 2k + 1.

Zadeh anti-shortest-paths flow network
Returns
A directed tgen::egraph<int>::value with n = 4l + 2k + 2 vertices and m = 6l + 4k + k^2 - 4 edges. Source s = 0, sink t = 4l + 2k + 1.

References:

Complexity

O(l + k^2).

Examples

std::cout << tgen::hack::dinitz_worst_case(100, 100).print_nm();
egraph< int >::value dinitz_worst_case(int k, int l)
Flow network for Edmonds-Karp and Dinitz worst-case.
Definition tgen.h:7291

Definition at line 7291 of file tgen.h.

◆ mo_worst_case()

std::vector< std::pair< int, int > > tgen::hack::mo_worst_case ( int n,
int q )
inline

Query list that forces asymptotic worst-case for Mo's algorithm.

Parameters
nSize of the array operated upon.
qNumber of queries to be returned.

Forces Theta(q sqrt n) pointer moves for any ordering.

Complexity

O(n log n + q).

Examples

// Creates queries that force worst-case for Mo.
std::vector<std::pair<int, int>> hack = tgen::hack::mo_worst_case(1e6, 1e6);

Definition at line 7159 of file tgen.h.

◆ mt19937_xor_hash()

template<typename T>
std::vector< bool > tgen::hack::mt19937_xor_hash ( )

Mask that forces a zero XOR hash from std::mt19937 or std::mt19937_64.

Template Parameters
Tint for std::mt19937, long long for std::mt19937_64.
Returns
A mask of length 19938.

For any seed, if rng is the corresponding engine and

std::mt19937 rng(42);
int hash = 0;
for (bool use : tgen::hack::mt19937_xor_hash<T>()) {
if (use) hash ^= rng();
}

then hash = 0.

Reference:

Complexity

O(1).

Examples

std::mt19937 rng(seed);
uint32_t hash = 0;
if (use) hash ^= rng();

Definition at line 7338 of file tgen.h.

◆ naive_rotating_calipers_max_dist_bug()

std::vector< geometry::point< double > > tgen::hack::naive_rotating_calipers_max_dist_bug ( )
inline

Convex polygon that breaks naive rotating calipers for maximum distance.

Returns a strictly convex polygon on which the naive rotating-calipers loop (advance j while dist(i, next(j)) > dist(i, j)) reports a diameter smaller than the true one.

Returns
Six vertices of a strictly convex polygon (clockwise).

Reference:

Complexity

O(1).

Examples

std::vector<tgen::geometry::point<double>> poly = tgen::hack::naive_rotating_calipers_max_dist_bug();
std::vector< geometry::point< double > > naive_rotating_calipers_max_dist_bug()
Convex polygon that breaks naive rotating calipers for maximum distance.
Definition tgen.h:7383

Definition at line 7383 of file tgen.h.

◆ non_strict_relaxation_dijkstra_bug()

egraph< int >::value tgen::hack::non_strict_relaxation_dijkstra_bug ( int n)
inline

Directed weighted graph for Dijkstra with non-strict relaxation.

Parameters
nNumber of vertices.
Precondition
n >= 3.

Forces Omega(2^{n/2}) time on Dijkstra from 0 if relaxation uses d + w <= dist[j] instead of <.

Returns
An egraph<int>::value with m = 2(n - 2) directed edges, all with weight 1.

Complexity

O(n).

Examples

egraph< int >::value non_strict_relaxation_dijkstra_bug(int n)
Directed weighted graph for Dijkstra with non-strict relaxation.
Definition tgen.h:7213

Definition at line 7213 of file tgen.h.

◆ polynomial_hash() [1/2]

std::pair< std::string, std::string > tgen::hack::polynomial_hash ( int alphabet_size,
int base,
int mod )
inline

Returns two strings that force polynomial hash collision given base and mod.

Parameters
alphabet_sizeAlphabet size.
baseBase.
modMod.
Precondition
0 < base < mod

Computes two strings that have the same polynomial hash for given base and mod.
Characters are in [a, a + alphabet_size). The larger the alphabet, the smaller the colliding strings.

Returns
A std::pair with the colliding strings.

Reference:

Complexity

O(sqrt(mod) log mod).

Examples

// Prints polynomial hash hack.
std::cout << tgen::print(tgen::hack::polynomial_hash(2, 31, 1e9+7), '\n') << std::endl;
std::pair< std::string, std::string > polynomial_hash(int alphabet_size, int base, int mod)
Returns two strings that force polynomial hash collision given base and mod.
Definition tgen.h:7094
Printer helper for standard types.
Definition tgen.h:485

Definition at line 7094 of file tgen.h.

◆ polynomial_hash() [2/2]

std::pair< std::string, std::string > tgen::hack::polynomial_hash ( int alphabet_size,
std::vector< int > bases,
std::vector< int > mods )
inline

Returns two strings that force polynomial hash collision given bases and mods.

Parameters
alphabet_sizeAlphabet size.
basesBases.
modsMods.
Precondition
|bases| = |mods| <= 2.
0 < bases[i] < mods[i].

Computes two strings that have the same polynomial hash for given bases and mods, up to 2 (base, mod) pairs.
Characters are in [a, a + alphabet_size). The larger the alphabet, the smaller the colliding strings.

Returns
A std::pair with the colliding strings.

Reference:

Complexity

O(sqrt(mod) log^2 mod), with mod = max(mods).

Examples

// Prints polynomial hash hack for double hash.
std::cout << tgen::print(tgen::hack::polynomial_hash(2, {31, 33}, {1000000007, 1000000009}), '\n') << std::endl;

Definition at line 7114 of file tgen.h.

◆ segment_tree_beats_worst_case()

std::pair< std::vector< int >, std::vector< std::vector< int > > > tgen::hack::segment_tree_beats_worst_case ( int k,
int q )
inline

Array and updates for worst case of segment tree beats.

Parameters
kHack order (defines the size N of the array).
qNumber of updates to generate.
Precondition
1 <= k <= 7 (largest k with fib(2k+1) < 10^3).
Returns
A std::pair(array, updates) that yields Theta(log^2 N) amortized time per update.
  • array: initial std::vector<int>, length N = fib(2k+1)^3, entries in [0, fib(2k+2) - 2].
  • updates: exactly q updates to apply in order (0-indexed, half-open intervals). Each update is a std::vector<int>:
    • {0, l, r, x}: add x on [l, r).
    • {1, l, r, x}: subtract x on [l, r).
    • {2, l, r, x}: chmax x on [l, r).
    • {3, i, x}: set array[i] = x.

Reference:

Complexity

O(fib(2k+1)^3 + q).

Examples

auto [arr, updates] = tgen::hack::segment_tree_beats_worst_case(3, 146);
for (const auto &u : updates) {
if (u[0] == 0)
for (int i = u[1]; i < u[2]; ++i) arr[i] += u[3];
else if (u[0] == 1)
for (int i = u[1]; i < u[2]; ++i) arr[i] -= u[3];
else if (u[0] == 2)
for (int i = u[1]; i < u[2]; ++i) arr[i] = std::max(arr[i], u[3]);
else if (u[0] == 3)
arr[u[1]] = u[2];
}

Definition at line 7460 of file tgen.h.

◆ spfa()

egraph< int >::value tgen::hack::spfa ( int n)
inline

Worst-case for FIFO-SPFA.

Parameters
nNumber of vertices.
Precondition
n >= 2, n even.

Builds two parallel chains of length k = n/2 (upper a_1..a_k, lower b_1..b_k):

  • Upper chain a_i -> a_{i+1}, weight 1;
  • Lower chain b_i -> b_{i+1}, weight 0;
  • Vertical a_i -> b_i, weight 0;
  • Cross b_i -> a_{i+1}, weight 1.

The upper chain reaches each a_{i+1} with a loose estimate first; once b_i is settled on the zero-weight lower chain, the cross edge can strictly improve a_{i+1} and re-enqueue it in FIFO-SPFA.

Forces Omega(n^2) time on FIFO-SPFA from vertex 0 (Theta(n * m).

Returns
A directed tgen::egraph<int>::value with n vertices and m = 2n - 3 edges.

Complexity

O(n).

Examples

auto g = tgen::hack::spfa(1e5);
egraph< int >::value spfa(int n)
Worst-case for FIFO-SPFA.
Definition tgen.h:7267

Definition at line 7267 of file tgen.h.

◆ stale_heap_dijkstra_bug()

egraph< int >::value tgen::hack::stale_heap_dijkstra_bug ( int n)
inline

Directed weighted graph for Dijkstra without a stale-heap check.

Parameters
nNumber of vertices.
Precondition
n >= 4.

Forces Omega(n^2) time on Dijkstra from 0 if the heap pop does not skip stale entries (if (d > dist[i]) continue).

Returns
An egraph<int>::value with m = n + floor(n/2) - 3 directed edges, with weights at most n.

Complexity

O(n).

Examples

egraph< int >::value stale_heap_dijkstra_bug(int n)
Directed weighted graph for Dijkstra without a stale-heap check.
Definition tgen.h:7243

Definition at line 7243 of file tgen.h.

◆ std_unordered()

std::vector< long long > tgen::hack::std_unordered ( int size)
inline

List of integers that tries to force collision on std::unordered_set.

Parameters
sizeSize of the list.

Creates hack for both std::unordered_set and std::unordered_map, forcing quadratic behavior. The hack might not be effective due to differences in C++ implementations.

Note
The hack might not be effective due to differences in C++ implementations.
This function benefits from tgen::set_compiler and tgen::set_cpp_version information.

Complexity

O(size).

Examples

// Creates values that force collision.
std::vector<long long> hack = tgen::hack::std_unordered(1e6);

Definition at line 7137 of file tgen.h.

◆ string_set_worst_case()

std::vector< std::string > tgen::hack::string_set_worst_case ( int size)
inline

List of strings that have high cost to insert in a std::set.

Parameters
sizeTotal number of characters.

Inserting these strings in a std::set takes Theta(size log(size)) time, and that is asymptotically the maximum one can achieve.

Complexity

O(size log(size)).

Examples

// Creates strings that have high cost to insert in std::set.
std::vector<std::string> hack = tgen::hack::string_set_worst_case(1e6);
std::vector< std::string > string_set_worst_case(int size)
List of strings that have high cost to insert in a std::set.
Definition tgen.h:7192

Definition at line 7192 of file tgen.h.

◆ unsigned_polynomial_hash()

std::pair< std::string, std::string > tgen::hack::unsigned_polynomial_hash ( )
inline

Returns two strings that force polynomial hash collision for power-of-two mod.

Computes two strings that have the same polynomial hash for any base and power-of-two mod (up to 2^64).
Characters are in [a, b].

Returns
A std::pair with the colliding strings.

Reference:

Complexity

O(1).

Examples

// Prints unsigned polynomial hash hack.
std::pair< std::string, std::string > unsigned_polynomial_hash()
Returns two strings that force polynomial hash collision for power-of-two mod.
Definition tgen.h:7081

Definition at line 7081 of file tgen.h.