|
1504 | 1504 | - **Edge List**:
|
1505 | 1505 | - It consists of storing the graph as a list of edges
|
1506 | 1506 | - 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) |
1508 | 1508 | - **Adjacency Matrix**:
|
1509 | 1509 | - Matrix[i,j] = 1 if there is an edge, 0 if there is not
|
1510 | 1510 | - E.g. Undirected Directed
|
|
1553 | 1553 | - Links between webpages
|
1554 | 1554 | - Followers on social network
|
1555 | 1555 | - Dependencies between tasks
|
1556 |
| -- Any path with at least |V| edges contains a cycle |
1557 | 1556 | - For more details:
|
1558 | 1557 | - 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)
|
1559 | 1558 | - 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 | 1713 | - Layer 1: contains all vertices which distance to ν is: 1
|
1715 | 1714 | - ...
|
1716 | 1715 | - 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 |
1724 | 1723 | - In a Undirected graph, **Edges are possible between same layer nodes or adjacent layers nodes**
|
1725 | 1724 | - In other words, there is no edge between nodes of a layer l and nodes of layers < l - 1 and layers > l + 1
|
1726 | 1725 | - E.g. From example above:
|
|
1805 | 1804 | - C is discovered while we were processing B, B is the previous vertex of B
|
1806 | 1805 | - For every node, there is a previous node except for the node A
|
1807 | 1806 | - 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 |
1815 | 1814 | - The **Shortest Path** from A to any node ν:
|
1816 | 1815 | - We use the Shortest Path Tree
|
1817 | 1816 | - We build a path from the node ν to the node A, by going to the top of the tree until A is reached
|
|
1918 | 1917 | <summary>Bellman-Ford algorithm</summary>
|
1919 | 1918 |
|
1920 | 1919 | - 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 |
1922 | 1921 | - -2
|
1923 | 1922 | A ←←← C
|
1924 | 1923 | 4↗ 1↘ ↗-3
|
|
1944 | 1943 | - ReconstructPath(A, μ, prev):
|
1945 | 1944 | Same as BFS ReconstructPath algorithm
|
1946 | 1945 | - 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 |
1947 | 1954 | - 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 |
1949 | 1965 | - Related Problems:
|
1950 | 1966 | - Maximum product over paths:
|
1951 | 1967 | - 0.88 0.84 8.08
|
1952 |
| - US -----> EUR ------> GBP ------> ... -> NOK ------> RUB |
| 1968 | + US —————> EUR —————> GBP —————> ... —> NOK ————————> RUB |
1953 | 1969 | $1 €1 * 0.88 £1 * 0.88 * 0.84 ₽1 * 0.88 * 0.84 * ... * 8.08 (Product)
|
1954 | 1970 | - Input: Currency exchange graph with weighted directed edges ei between some pairs of currencies with weights rei corresponding to the exchange rate
|
1955 | 1971 | - Output: k
|
|
1962 | 1978 | Max ∏ rej <=> Max ∑ log(rej) <=> Min ∑ ( - log(rej))
|
1963 | 1979 | j=1 j=1 j=1
|
1964 | 1980 | - 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 |
1965 | 1993 | - [Detecting Anomalies in Currency Exchange Rates](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/134)
|
1966 | 1994 | - [Exchanging Money Optimally](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/135)
|
1967 | 1995 | - For more details:
|
|
0 commit comments