Arbitrage Using Bellman-Ford Algorithm
Arbitrage Using Bellman-Ford Algorithm
Arbitrage
Application of Negative Cycles in a Graph and Bellman-Ford Algorithm
🙏
জয় শ্রী রাম
🕉
Algorithms and Data Structures: TheAlgorist.com
System Design: www.System.Design
Low Level Design: LowLevelDesign.io
Frontend Engineering: FrontendEngineering.io
Suppose you are given a set of exchange rates among certain currencies and you want to determine if an arbitrage is
possible, i.e, if there is a way by which you can start with one unit of some currency C and perform a series of barters which
results in having more than one unit of C. Let's assume that
transaction costs are zero
exchange rates do not fluctuate
fractional quantities of items can be sold
Let's we have two currencies A and B and let's consider the below few scenarios:
1. let's suppose exchange rate A -> B = 1.5 and B -> A = 0.6667
Note that the product of the exchange rates 1.5 * 0.67 = 1.
So if we exchange 1 unit of A with B and whatever we get we again convert to A we get 1 unit of A back.
2. Now let's suppose exchange rate A -> B = 1.5 and B -> A = 0.5
Multiplication of exchange rates = 1.5 * 0.5 < 1
In this case if we convert 1 unit of A into and then convert back to A, we will end up with less than 1 unit of A even
though we started with 1 unit of A, So in short we will lose in this scenario with profit < 0.
3. If exchange rate from A to B is 1.5 and B to A is 0.8 then since 1.5 * 0.8 > 1 and we will end up with more than 1 unit of
A if we start with 1 unit of A, convert to B, and whatever we get after conversion, we convert that back from B to Profit
> 0.
We can extend this observation to more than two currencies as well.
From our above observation, we go on converting a currency to other currencies in sequence and convert back that
same current we started with (say, we started with 1 unit of currency A and hen converted it to B, then whatever we
got after the conversion, we converted that to C and finally whatever we got from conversion to C we converted that
amount from C to back to A), then whether we would make any profit or not depends on the result of the
multiplication of the exchange rates involved. If the product of the exchange rates is greater than 1 then we would
make profit, if less than 1 then loss and it is zero then we make no profit i.e, we we started with 1 unit of A then we will
get back 1 unit of A. A, B, C, A clearly creates a cycle.
From above discussion we can say that if we represent relationship between different currencies through a graph,
as discussed above, then an arbitrage situation will take place when the result of the multiplication of the weights of
all the edges forming the cycle is greater than 1.
Say, the exchange rate from A to B is a, B to C is b, and C to A is c.
Then we have a arbitrage only if
a*b*c>1
In the above graph exchange rates of
USD to EUR is 0.741,
EUR to CAD is 1.366,
CAD to USD is 0.995
https://www.thealgorists.com/Algo/ShortestPaths/Arbitrage 2/6
6/10/24, 6:34 PM Arbitrage using Bellman-Ford Algorithm
https://www.thealgorists.com/Algo/ShortestPaths/Arbitrage 3/6
6/10/24, 6:34 PM Arbitrage using Bellman-Ford Algorithm
We have earlier seen that USD -> EUR -> CAD -> USD forms an arbitrage. Let's see what the summation of the edge
weights yield now.
Edge weight for USD -> EUR = ln (0.741) = -0.2998
Edge weight for EUR -> CAD = ln (1.366) = 0.3119
Edge weight for CAD -> USD = ln (0.995) = -0.005
(-0.2998) + (0.3119) + (-0.005) = 0.0071 > 0
So we got the summation to be greater than ZERO, as expected.
So now the solution is reduced to finding a positive weight cycle in the modified graph
We just discussed in previous two chapters how Bellman-Ford Algorithm very efficiently finds negative weight cycle in a
graph. So why not take advantage of that? We could modify the graph to convert the problem from finding positive weight
cycle to a fining negative weight cycle. We can achieve this by negating weights of all the edges.
Algorithm:
1. Create a directed graph in which the vertices are the currencies and the weight of the edges are the negated
logarithm of the exchange rate of the currencies represented by the vertices the edge connect.
For example, if exchange rate for currency A to currency B is E then in the graph the directed edge connected A to B
will have weight of (-lnE).
2. Find if there is a negative weight cycle.
Implementation #1
This implementation is based on our discussion in the Bellman-Ford Algorithm & Negative Cycle Detection chapter.
Implementation #2
The below implementation is heavily based on the implementation discussion we had in Optimized Bellman-Ford Algorithm
chapter.
https://www.thealgorists.com/Algo/ShortestPaths/Arbitrage 4/6
Time Complexity:
6/10/24, 6:34 PM Arbitrage using Bellman-Ford Algorithm
same as that of Bellman-Ford ALgorithm: O(EV). The graph representing relationship between currencies are dense graph
so E = O(V * V). So overall time complexity O(V3), where V = total number of vertices in the graph, E = total number of
edges in the graph. V = total number different kinds of currencies in the given problem.
Note:
Even in the absence of an arbitrage, the above understanding will come handy.
Say, we want to convert currency A to currency Z and there are several ways of reaching Z from A. Basically if we have a
graph with vertices representing the currencies and the edge weights the exchange rates, the graph has more than one
distinct path from A to Z. In this case, we are to find which path would be the most profitable then that would mean
maximizing the result of the multiplication of the exchange rates.
This would mean if we modify the graph make the edge weights represent the logarithm of the exchange rates then this
would mean that the longest path from A to Z would give us the most profitable way to convert.
When we modify the graph further and make the edge weights represent negated logarithm of the exchange rates then the
shortest path between two currencies will give you the most profitable path of conversion between the two currencies.
It's amazing how the Arbitrage problem reduces currency conversion to a shortest-paths problem which helps us solve (1)
finding existence of any arbitrage, as well as (2) finding most profitable way to convert currency A to currency B.
Related Chapters:
1. Dijkstra's Algorithm
2. Longest Paths Algorithm
3. Topological Sort w/ Vertex Relaxation
4. Bellman-Ford Algorithm & Negative Cycle Detection
5. Optimized Bellman-Ford
6. Sequential Job Scheduling
7. Parallel Job Scheduling
8. Parallel Job Scheduling
w/ Relative Deadlines
Instructor:
Abhishek Dey
Senior SDE | Chief Architect
AWS | Microsoft | University of Florida
View LinkedIn profile
https://www.thealgorists.com/Algo/ShortestPaths/Arbitrage 5/6
6/10/24, 6:34 PM Arbitrage using Bellman-Ford Algorithm
https://www.thealgorists.com/Algo/ShortestPaths/Arbitrage 6/6