-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcurrencyArbitrage.py
More file actions
37 lines (26 loc) · 1.11 KB
/
currencyArbitrage.py
File metadata and controls
37 lines (26 loc) · 1.11 KB
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
from math import log
def currencyArbitrage(exchange):
all_weights = [[-log(rate) for rate in row] for row in exchange]
start_cost = all_weights[0]
costs = start_cost[::]
for _ in exchange:
# take a note of current state
temp = tuple(costs)
# loop through source nodes
for curr, node_weights in enumerate(all_weights):
# loop through destination nodes
for target, weight in enumerate(node_weights):
# figure out cost to get to dest from source
from_curr = costs[curr] + weight
# if this path is shorter, use it instead
if from_curr < costs[target]:
costs[target] = from_curr
# check if we found a negative weight cycle
if costs[0] < start_cost[0]:
return True
# if there are no neg weight cycles and we're stationary
elif tuple(costs) == temp:
return False
# we're not stationary, but if the graph had no negative weight cycles,
# we would be. therefore, there is a negative weight cycle.
return True