Skip to content

Commit ebf3209

Browse files
author
Hamid Gasmi
committed
Bellman-Ford algorithm description is updated
1 parent a2f4734 commit ebf3209

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

README.md

+47-19
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@
15041504
- **Edge List**:
15051505
- It consists of storing the graph as a list of edges
15061506
- Each edge is a pair of vertices,
1507-
- E.g., Edges List: (A, B) --> (A, C ) --> (A, D) --> (C , D)
1507+
- E.g., Edges List: (A, B) ——> (A, C ) ——> (A, D) ——> (C , D)
15081508
- **Adjacency Matrix**:
15091509
- Matrix[i,j] = 1 if there is an edge, 0 if there is not
15101510
- E.g. Undirected Directed
@@ -1553,7 +1553,6 @@
15531553
- Links between webpages
15541554
- Followers on social network
15551555
- Dependencies between tasks
1556-
- Any path with at least |V| edges contains a cycle
15571556
- For more details:
15581557
- UC San Diego Course: [Basics](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/blob/master/3-graph-algorithms/1_graph_decomposition/09_graph_decomposition_1_basics.pdf)
15591558
- UC San Diego Course: [Representation](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/blob/master/3-graph-algorithms/1_graph_decomposition/09_graph_decomposition_2_representations.pdf)
@@ -1714,13 +1713,13 @@
17141713
- Layer 1: contains all vertices which distance to ν is: 1
17151714
- ...
17161715
- E.g.: G: Layers Distance Layers from A Distance Layers from C
1717-
A -- B -- C 0 A C
1718-
| | / \
1719-
D 1 B B D
1720-
| |
1721-
2 C A
1722-
|
1723-
3 D
1716+
A B C 0 A C
1717+
| | / \
1718+
D 1 B B D
1719+
| |
1720+
2 C A
1721+
|
1722+
3 D
17241723
- In a Undirected graph, **Edges are possible between same layer nodes or adjacent layers nodes**
17251724
- In other words, there is no edge between nodes of a layer l and nodes of layers < l - 1 and layers > l + 1
17261725
- E.g. From example above:
@@ -1805,13 +1804,13 @@
18051804
- C is discovered while we were processing B, B is the previous vertex of B
18061805
- For every node, there is a previous node except for the node A
18071806
- G: G(A): Shortest Path Tree (A)
1808-
F--A--B A
1809-
|/ | | ↗↗ ↖↖
1810-
E--D--C B D E F
1811-
| / | ↑ ↑
1812-
G--H--I C G
1813-
↗ ↖
1814-
H I
1807+
F — A — B A
1808+
| / | | ↗↗ ↖↖
1809+
E — D — C B D E F
1810+
| / | ↑ ↑
1811+
G — H — I C G
1812+
↗ ↖
1813+
H I
18151814
- The **Shortest Path** from A to any node ν:
18161815
- We use the Shortest Path Tree
18171816
- We build a path from the node ν to the node A, by going to the top of the tree until A is reached
@@ -1918,7 +1917,7 @@
19181917
<summary>Bellman-Ford algorithm</summary>
19191918

19201919
- A **Negative weight cycle**:
1921-
- In the example below the negate cycle is: A → B → C → A => Lω(A → B → C → A) = 1 - 3 - 2 = -4
1920+
- In the example below the negative cycle is: A → B → C → A => Lω(A → B → C → A) = 1 - 3 - 2 = -4
19221921
- -2
19231922
A ←←← C
19241923
4↗ 1↘ ↗-3
@@ -1944,12 +1943,29 @@
19441943
- ReconstructPath(A, μ, prev):
19451944
Same as BFS ReconstructPath algorithm
19461945
- Time Complexity: **O(|V||E|)**
1946+
- Properties:
1947+
- After k iterations of relaxations, for any node u, dist[u] is the smallest length of a path from S to u that contains at most k edges
1948+
- Any path with at least |V| edges contains a cycle
1949+
- This cycle can be removed without making the path longer (because the cycle weight isn't negative)
1950+
- Shortest path contains at most V − 1 edges and will be found after V − 1 iterations
1951+
- Bellman–Ford algorithm correctly finds dist[u] = d (S, u):
1952+
- If there is no negative weight cycle reachable from S and
1953+
- u is reachable from this negative weight cycle
19471954
- Find Negative Cycles:
1948-
- A graph G contains a negative weight cycle if and only if |V|-th (additional) iteration of BellmanFord(G , S) updates some dist-value
1955+
- A graph G contains a negative weight cycle if and only if |V|-th (additional) iteration of BellmanFord(G , S) updates some dist-value:
1956+
- Run |V| iterations of Bellman-Ford algorithm
1957+
- Save node v related on the last iteration:
1958+
- v isn't necessary on the cycle
1959+
- but it's reachable from the cycle
1960+
- Otherwise, it couldn't be relaxed on the |V|-th iteration!
1961+
- In fact, it means that there is a path length which is shorter than any path which contains few edges and
1962+
- this path with at least |V| edges contains a cycle and if we remove the cycle, the path must become longer
1963+
- Start from x ← v, follow the link x ← prev[x] for |V| times — will be definitely on the cycle
1964+
- Save y ← x and go x ← prev[x] until x = y again
19491965
- Related Problems:
19501966
- Maximum product over paths:
19511967
- 0.88 0.84 8.08
1952-
US -----> EUR ------> GBP ------> ... -> NOK ------> RUB
1968+
US —————> EUR —————> GBP —————> ... > NOK ————————> RUB
19531969
$1 €1 * 0.88 £1 * 0.88 * 0.84 ₽1 * 0.88 * 0.84 * ... * 8.08 (Product)
19541970
- Input: Currency exchange graph with weighted directed edges ei between some pairs of currencies with weights rei corresponding to the exchange rate
19551971
- Output: k
@@ -1962,6 +1978,18 @@
19621978
Max ∏ rej <=> Max ∑ log(rej) <=> Min ∑ ( - log(rej))
19631979
j=1 j=1 j=1
19641980
- The new problem is: Find the shortest path between USD and RUR in a weighted graph where **ω(rei) = (− log(rei))**
1981+
- Find if an **Infinite Arbitrage** is possible from a currency S to a currency u:
1982+
- It's possible to get any (+∞) amount of currency u from currency S if and only if u is reachable from some node w for which dist[w] decreased on iteration V of Bellman-Ford (there is a negative cycle in a graph)
1983+
- Do |V| iterations of Bellman–Ford
1984+
- Save all nodes relaxed on V-th iteration — set A
1985+
- Put all nodes from A in queue Q
1986+
- Do BFS with queue Q and find all nodes reachable from A: all those nodes and only those can have infinite arbitrage
1987+
- Reconstruct the Infinite Arbitrage from a currency S to a currency u:
1988+
- See problem above
1989+
- During BFS, remember the parent of each visited node
1990+
- Reconstruct the path to u from some node w relaxed on iteration V
1991+
- Go back from w to find negative cycle from which w is reachable
1992+
- Use this negative cycle to achieve infinite arbitrage from S to u
19651993
- [Detecting Anomalies in Currency Exchange Rates](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/134)
19661994
- [Exchanging Money Optimally](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/135)
19671995
- For more details:

0 commit comments

Comments
 (0)