- Mark
原本的Floyd-Warshall algorithm
將,沒有負環的情況會正確,可能有以下情況
題目的Floyd-Warshall algorithm
將,求可能的情況
Ans
有迴圈,從u出發走一圈回到u的路徑長,取min的情況下就會是
補充:可以有負權重的邊,這是可以同時成立的;但仍舊不能有負環,不然找不出最小路徑
- chung
Find a feasible solution or determine that no feasible solution exists for the following system of difference constraints:
x1 -x2 ≤4
x1 -x5 ≤5
x2 -x4 ≤-6
x3 -x2 ≤1
x4 -x1 ≤3
x4 -x3 ≤5
x4 -x5 ≤10
x5 -x3 ≤-4
x5 -x4 ≤-8
因為此題有負Weight,因此使用Bellman-Ford演算法檢驗是否存在負環,
如果有負環即表示無法得知各節點的最短路徑,feasible solution不存在。
【Bellman-Ford步驟】
由示意圖可知,x4 -> x2 -> x3 -> x5 -> x1 -> x4 會形成負環,
因此這個 constraints graph 沒有 feasible solution。
- songchiu
Give an efficient algorithm to find the length (number of edges) of a minimum-length negative-weight cycle in a graph.
【解題思路】
找 只要去找上的教的矩陣的對角線上,第一次出現負值的地方。
的就是題目所要求的
矩陣有可能會長的像這樣:
- Xian
Run the Floyd-Warshall algorithm on the weighted, directed graph of Figure and answer the following questions:
a. Show the matrix that results for each iteration of the outer loop.
b. List the vertices of one such shortest path from to .
【a小題】
【b小題】
根據a小題所解的答案,我們可以先從最後早發生更動的值開始尋找,因為這題要找,所以先找最早更改[6,1]的k值。
Ans:
- LXS
Use Johnson's algorithm to find the shortest paths between all pairs of vertices in the graph of Figure 25.2. Show the values of h and ŵ computed by the algorithm.
【作法】
d[v]
設為0),同時排除負迴圈的可能v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
d(v) | 0 | 0 | 0 | 0 | 0 | 0 |
v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
d(v) | -4 | -3 | 0 | 0 | -1 | -8 |
v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
d(v) | -4 | -3 | 0 | -1 | -5 | -8 |
v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
d(v) | -5 | -3 | 0 | -1 | -5 | -8 |
v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
p(v) | 4 | 6 | NIL | 2 | 4 | 3 |
d(v) | -5 | -3 | 0 | -1 | -6 | -8 |
得出 Shortest-path tree
v | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
h(v) | -5 | -3 | 0 | -1 | -6 | -8 |
W'(u,v) = W(u,v) + h(u) - h(v)
(u,v) | (1,5) | (2,1) | (2,4) | (3,2) | (3,6) | (4,1) | (4,5) | (5,2) | (6,2) | (6,3) |
---|---|---|---|---|---|---|---|---|---|---|
w(u,v) | -1 | 1 | 2 | 2 | -8 | -4 | 3 | 7 | 5 | 10 |
w'(u,v) | 0 | 3 | 0 | 5 | 0 | 0 | 8 | 4 | 0 | 2 |
做V次Dijkstra後
W(u,v) = W'(u,v) - h(u) + h(v)
- yee
Arbitrage is the use of discrepancies in currency exchange rates to transform one
unit of a currency into more than one unit of the same currency. For example,
suppose that 1 U.S. dollar buys 49 Indian rupees, 1 Indian rupee buys 2 Japanese yen,
and 1 Japanese yen buys 0.0107 U.S. dollars. Then, by converting currencies, a trader
can start with 1 U.S. dollar and buy 49×2×0.0107 = 1.0486 U.S. dollars, thus turning a
profit of 4.86 percent.
Suppose that we are given n currencies c1, c2, … cn and an n×n table R of exchange
rates, such that one unit of currency ci buys R[i, j ] units of currency cj .
a. Give an efficient algorithm to determine whether or not there exists a sequence of
currencies <ci1, ci2, …, cik> such that R[i1, i2 ]×R[i2, i3 ]×…×R[ik-1, ik ]×R[ik, i1 ] > 1 .
Analyze the running time of your algorithm.
b. Give an efficient algorithm to print out such a sequence if one exists. Analyze the
running time of your algorithm.
建立一個G需花而這個G有個edges,使得Bellman Ford需花上,
因為必定存在一負環,因此做完|V|次BELLMAN FORD後,必定會return False,並且可知哪點v可以被Relax,用𝜋[v]往前找,直到遇到第二次v停下就可以找到負環。
Bellman Ford要花,Print-Sequence要花,
- yee
Transitive Closure 代表 (u,v) u可移動到v,
先透過一個GTC[u,v]的boolean matrix來存放原先的狀態
GTC[u,v] = 1 表示 有Transitive Closure,即 u 可移動到 v
GTC[u,v] = 0 表示 沒有Transitive Closure,即 u 不可能移動到 v
透過這個boolean matrix來記錄每個點的狀態
原GTC:
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 1 | 1 | 1 | 1 |
2 | 0 | 0 | 1 | 1 |
3 | 0 | 0 | 0 | 1 |
4 | 0 | 0 | 0 | 1 |
增加edge(4,1):
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 1 | 1 | 1 | 1 |
2 | 1 | 1 | 1 | 1 |
3 | 1 | 1 | 1 | 1 |
4 | 1 | 1 | 1 | 1 |
worst case