summaryrefslogtreecommitdiff
path: root/test/graph/dijkstra.cpp
blob: d79e70090eaad7e6e3e638b381c618594054393a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "../util.h"
constexpr ll INF = LL::INF;
#include <graph/dijkstra.cpp>
struct edge {
	int from, to;
	ll cost;
};
#include <graph/bellmannFord.cpp>

void stress_test(int LIM) {
	ll queries = 0;
	for (int tries = 0; tries < LIM; tries++) {
		int n = Random::integer<int>(2, 30);
		int m = Random::integer<int>(n-1, max<int>(n, min<int>(500, n*(n-1) / 2 + 1)));

		vector<vector<pair<int, ll>>> adj(n);
		vector<edge> edges;

		Graph<NoData, true> g(n);
		g.erdosRenyi(m);
		g.forEdges([&](int a, int b){
			ll w = Random::integer<ll>(1, 1'000'000'000'000ll);
			adj[a].emplace_back(b, w);
			edges.push_back({a, b, w});
		});

		for (int i = 0; i < n; i++) {
			auto got = dijkstra(adj, i);
			auto expected = bellmannFord(n, edges, i);

			if (got != expected) cerr << "error" << FAIL;
			queries += n;
		}
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 500'000;
constexpr int M = 3'000'000;
void performance_test() {
	timer t;
	Graph<NoData> g(N);
	g.erdosRenyi(M);
	vector<vector<pair<int, ll>>> adj(N);
	g.forEdges([&](int a, int b){
		ll w1 = Random::integer<ll>(1, 1'000'000'000'000ll);
		ll w2 = Random::integer<ll>(1, 1'000'000'000'000ll);
		adj[a].emplace_back(b, w1);
		adj[b].emplace_back(a, w2);
	});

	t.start();
	auto got = dijkstra(adj, 0);
	t.stop();
	hash_t hash = 0;
	for (auto x : got) hash += x;
	if (t.time > 1000) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test(10'000);
	if (!sanitize) stress_test(100'000);
	if (!sanitize) performance_test();
}