-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdetect-arbitrage.py
104 lines (97 loc) · 3.65 KB
/
detect-arbitrage.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Detect Arbitrage
# 🟣 Very Hard
#
# https://www.algoexpert.io/questions/detect-arbitrage
#
# Tags: Graphs
import timeit
# For each currency, recursively compute the maximum amount of other
# currencies that we can obtain, every time that we best the amount of
# one currency, use it to check how many of all other currencies we
# could potentially buy using the new amount.
# TODO: Check the solution that uses Bellman-Ford, it may be better.
#
# Time complexity: O(n^3) - The outer loop goes over n elements, the
# middle loop iterates over all currencies that have been updated in
# the last iteration of the inner loop, the inner loop iterates over all
# currencies checking if we can use the updated amount of base currency
# to obtain more of this currency than previously we thought it was
# possible.
# Space complexity: O(n) - The exchange array x has length n and the
# updated set will grow to n-1 in the first loop, because all other
# currencies will be updated.
class Solution:
def detectArbitrage(self, exchangeRates) -> bool:
n = len(exchangeRates)
# Try starting with one unit of each currency.
for i in range(n):
# The max amount of other currencies that we have figured
# out how to get using 1 unit of i.
x = [float("-inf")] * n
x[i] = 1
# Exchange rates that have become better in the last
# iteration.
updated = set([i])
# Compute the exchange after one change.
while updated:
base = updated.pop()
# How many base units do we have?
amount = x[base]
# Iterate over all other currencies checking how many
# units we would get using amount units of base. If the
# transaction results in a worst result than previous
# transactions, cancel it.
for desired in range(n):
if desired == base:
continue
rate = exchangeRates[base][desired]
total = amount * rate
if total > x[desired]:
# If this is the base currency, we have detected
# arbitrage.
if desired == i:
return True
x[desired] = total
updated.add(desired)
# We have made all transactions possible and we have not detected
# arbitrage being a possibility.
return False
def test():
executors = [Solution]
tests = [
[[[1, 2], [0.4, 1]], False],
[
[
[1, 0.8631, 0.5903],
[1.1586, 1, 0.6849],
[1.6939, 1.46, 1],
],
True,
],
[
[
[1, 0.5, 0.25, 2],
[2, 1, 0.5, 4],
[4, 2, 1, 8],
[0.5, 0.25, 0.0125, 1],
],
False,
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.detectArbitrage(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()