-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
arbitrage.cc
63 lines (53 loc) · 1.89 KB
/
arbitrage.cc
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
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
#include "test_framework/generic_test.h"
using std::numeric_limits;
using std::vector;
bool BellmanFord(const vector<vector<double>>& graph, int source);
bool IsArbitrageExist(vector<vector<double>> graph) {
// Transforms each edge in graph.
for_each(begin(graph), end(graph), [](vector<double>& edge_list) {
for_each(begin(edge_list), end(edge_list),
[](double& edge) { edge = -log10(edge); });
});
// Uses Bellman-Ford to find negative weight cycle.
return BellmanFord(graph, 0);
}
bool BellmanFord(const vector<vector<double>>& graph, int source) {
vector<double> dis_to_source(size(graph), numeric_limits<double>::infinity());
dis_to_source[source] = 0;
for (int times = 1; times < size(graph); ++times) {
bool have_update = false;
for (int i = 0; i < size(graph); ++i) {
for (int j = 0; j < size(graph[i]); ++j) {
if (dis_to_source[i] != numeric_limits<double>::infinity() &&
dis_to_source[j] > dis_to_source[i] + graph[i][j]) {
have_update = true;
dis_to_source[j] = dis_to_source[i] + graph[i][j];
}
}
}
// No update in this iteration means no negative cycle.
if (have_update == false) {
return false;
}
}
// Detects cycle if there is any further update.
for (int i = 0; i < size(graph); ++i) {
for (int j = 0; j < size(graph[i]); ++j) {
if (dis_to_source[i] != numeric_limits<double>::infinity() &&
dis_to_source[j] > dis_to_source[i] + graph[i][j]) {
return true;
}
}
}
return false;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"graph"};
return GenericTestMain(args, "arbitrage.cc", "arbitrage.tsv",
&IsArbitrageExist, DefaultComparator{}, param_names);
}