【软件】行动手册 🚩

我们终于迎来了黎明的曙光

https://gist.github.com/anilpai/fe4e11b5c59d8c02813900813396400b

https://gist.github.com/anilpai/dd00a9671c062390c848952eaddbbe1e

How do we solve this?

Graph data structure

Weighted directed graphs can be represented as an adjacency matrix. For a graph with |V| X |V| vertices, an adjacency matrix is a |V| times |V| matrix of values, where the entry in row i & column j is a non-zero integer if and only if the edge (i, j) is in the graph. If you want to indicate an edge weight, put it in the row i, column j entry, and reserve a special value (perhaps None) to indicate an absent edge.

Finding arbitrage

Arbitrage opportunities arise when a cycle is determined such that the edge weights satisfy the following expression

w1 * w2 * w3 * … * wn > 1

The above constraint of finding the cycles is harder in graphs. Instead we must transform the edge weights of the graph such that the standard graph algorithms can be applied.

Let’s take the logarithm on both sides, such that

log(w1) + log(w2) + log(w3) + … + log(wn) > 0

Taking the negative log, this becomes

(-log(w1)) + (-log(w2)) + (-log(w3)) + … + (-log(wn)) < 0

Therefore we can conclude that if we can find a cycle of vertices such that the sum of their weights if negative, then we can conclude there exists an opportunity for currency arbitrage. Luckily, Bellman-Ford algorithm is a standard graph algorithm that can be used to easily detect negative weight cycles in O(|V*E|) time.

Bellman Ford Algorithm

Let G(V, E) be a graph with vertices, V, and edges, E.

Let w(x) denote the weight of vertex x.

Let w(i, j) denote the weight of the edge from source vertex i to destination vertex j.

Let p(j) denote the predecessor of vertex j.

The Bellman-Ford algorithm seeks to solve the single-source shortest path problem. It is used in situations where a source vertex is selected and the shortest paths to every other vertex in the graph need to be determined. After applying Bellman-Ford algorithm on a graph, each vertex maintains the weight of the shortest path from the source vertex to itself and the vertex which precedes it in the shortest path. In each iteration, all edges are relaxed if [w(i) + w(i, j) < w(j)] and the weight of each vertex is updated accordingly. After the i-th iteration, the algorithm finds all shortest paths consisting of at most i edges.

Once all shortest paths have been identified, the algorithm loops through all of the edges and looks for edges that can further decrease the value of the shortest path. If we can still relax the edges, then a negative weight cycle has been found since a path can have at most |V-1| edges.

Printing a negative weight cycle is done to show the arbitrage opportunity. We use the predecessor chain to print the cycle. Now that we have an edge which can be further relaxed, we have found the source & destination vertex of such an edge. Let’s`` ``start from the source vertex and go backwards until you see the source vertex again or any other vertex that predecessor chain has already shown us while printing the negative weighted cycle.

我们发现了一个术语叫 predecessor chain,这很重要。

运行结果:

看完之后如果有帮助,请 打赏 **** 我。

现在你能回答以下这几个问题了吗? 1,BFM-find 和 BFM-detect 步骤有什么不同? 2,BFM-find 和 BFM-detect 的时间复杂度有什么不同? 3,BFM-find 里面的步骤涉及前驱链,什么是前驱链? 涉及前驱链的步骤到底做了什么? 4,为什么算法运行前要对汇率取-ln? 5,为什么汇率在取-ln之前要移动小数点再乘以几百? 6,BFM 算法基于图的邻接矩阵表示。什么是图的邻接矩阵表示?BFM 算法的步骤中如何使用图的邻接矩阵表示?

依然不能?回去,重看:

【算法】算法描述