tags:

views:

395

answers:

2

Arbitrage is the process of using discrepancies in currency exchange values to earn profit.
Consider a person who starts with some amount of currency X, goes through a series of exchanges and finally ends up with more amount of X(than he initially had).
Given n currencies and a table (nxn) of exchange rates, devise an algorithm that a person should use to avail maximum profit assuming that he doesn't perform one exchange more than once.

I have thought of a solution like this:

  1. Use modified Dijkstra's algorithm to find single source longest product path.
  2. This gives longest product path from source currency to each other currency.
  3. Now, iterate over each other currency and multiply to the maximum product so far, w(curr,source)(weight of edge to source).
  4. Select the maximum of all such paths.

While this appears good, i still doubt of correctness of this algorithm and the completeness of the problem.(i.e Is the problem NP-Complete?) as it somewhat resembles the traveling salesman problem.

Looking for your comments and better solutions(if any) for this problem.

Thanks.

EDIT:
Google search for this topic took me to this here, where arbitrage detection has been addressed but the exchanges for maximum arbitrage is not.This may serve a reference.

+4  A: 

Dijkstra's cannot be used here because there is no way to modify Dijkstra's to return the longest path, rather than the shortest. In general, the longest path problem is in fact NP-complete as you suspected, and is related to the Travelling Salesman Problem as you suggested.

What you are looking for (as you know) is a cycle whose product of edge weights is greater than 1, i.e. w1 * w2 * w3 * ... > 1. We can reimagine this problem to change it to a sum instead of a product if we take the logs of both sides:

log (w1 * w2 * w3 ... ) > log(1)

=> log(w1) + log(w2) + log(w3) ... > 0

And if we take the negative log...

=> log(w1) + log(w2) + log(w3) ... < 0 (note the inequality flipped)

So we are now just looking for a negative cycle in the graph, which can be solved using the Bellman-Ford algorithm (or, if you don't need the know the path, the Floyd-Warshall algorihtm)

First, we transform the graph:

for (int i = 0; i < N; ++i)
  for (int j = 0; j < N; ++j)
    w[i][j] = -log(w[i][j]);

Then we perform a standard Bellman-Ford

double dis[N], pre[N];

for (int i = 0; i < N; ++i)
   dis[i] = INF, pre[i] = -1;

dis[source] = 0;

for (int k = 0; k < N; ++k)
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      if (dis[i] + w[i][j] < dis[j])
        dis[j] = dis[i] + w[i][j], pre[j] = i;

Now we check for negative cycles:

for (int i = 0; i < N; ++i)
  for (int j = 0; j < N; ++j)
    if (dis[i] + w[i][j] < dis[j])
      // Node j is part of a negative cycle

You can then use the pre array to find the negative cycles. Start with pre[source] and work your way back.

Peter Alexander
who said I need to start from every possible node?.The starting currency is X(fixed) and ending is also X.
Neeraj
My apologies, I misread the question. Ah, so you are looking for an opportunity cycle in the graph. That calls for the Bellman-Ford algorithm with the edge weights transformed such that the `w' = -ln(w)`. The reason for that transformation is that it reduces the problem to finding a negative cycle in the graph. I will update my solution with the correct code.
Peter Alexander
not just a negative cycle but a cycle with minimum sum(after that transformation). Just curious, do you have that MIT problem handout, as the idea of log transformation is a bit unlikely to strike at first.;-)
Neeraj
@Neeraj: But it's pretty well-known that `log (A*B) = log A + log B`…
KennyTM
@Neeraj, I knew of the log transformation because this is a well-known problem with a well-known solution. As Kenny says, it is also well known that you can transform products to sums using logarithms.
Peter Alexander
@Kenny, yeah i know that, i even use log(n!) = log(n)+log(n-1).. to calculate factorials of large numbers.:P
Neeraj
@Poita_, thanks for your answer man, but standard Bellman ford AFAIK, returns a boolean value to indicate that no solution for shortest path exists if there is a negative cycle and a shortest path otherwise.Evaluating every cycle would definitely then be NP complete.
Neeraj
@Neeraj, I can assure you that this is the correct solution, and evaluating the cycles is not NP Complete. You are correct in saying that there is no shortest path if there are negative cycles, but if there are no negative cycles then the answer to your question is to simply perform no exchanges as you need a cycle (to get back to the original currency) and anything other than a negative cycle would be unprofitable. If you don't believe me, simply google "arbitrage problem" and see for yourself. As I said, this is a well-known problem and this is the solution.
Peter Alexander
@Poita_, yes i believe you but at the same time i am not convinced of completeness as the same logic can be applied to TSP, evaluate each cycle and output the cycle with minimum sum, which is well known to be NP-Complete. If you have a source feel free to paste it here.I sincerely appreciate your effort for this.Thanks.
Neeraj
http://www.cs.ucdavis.edu/~amenta/f05/hw5.pdfThe difference is that you are not evaluating every cycle, just select ones.
Peter Alexander
A: 

Take the log of the conversion rates. Then you are trying to find the cycle starting at X with the largest sum in a graph with positive, negative or zero-weighted edges. This is an NP-hard problem, as the simpler problem of finding the largest cycle in an unweighted graph is NP-hard.

Amit Kumar
that is correct,solving it that way would be NP-Complete, but my question is, Is my solution(which is polynomial time) guaranteed to perform correctly for all cases?
Neeraj
It is very unlikely that you found a polynomial time solution for an NP-hard *problem*. Note that Amit said NP-hard *problem* (not algorithm, whatever you think that means). Also note that he said NP-*hard* problem (not complete).
@rgrig.. yes the problem if modeled that way would be NP-complete but it may be possible that there are alternate ways to model the problem which may not be NP-complete.
Neeraj
@Neeraj No there isn't.
Amit Kumar
@Neeraj To clarify: Finding a way to make profit i.e. finding a positive cycle is in P (Poita_ has a solution for that). But finding the highest profit path is NP-hard.
Amit Kumar