09 BellmanFord
09 BellmanFord
ngắn nhất
Bellman-Ford algorithm
Today
• Bellman-Ford Algorithm
• Bellman-Ford is a special case of Dynamic
Programming!
Sử dụng một phần tài liệu bài giảng CS161 Stanford University
2
• Weights on edges
Recall represent costs.
u
s 3 32
1 5
1
b v
13
a 2 2
1
16 t
4
Dijkstra Drawbacks
• Needs non-negative edge weights.
• If the weights change, we need to re-run the
whole thing.
5
Bellman-Ford algorithm
• (-) Slower than Dijkstra’s algorithm
6
Aside: Negative Cycles
• A negative cycle is a cycle whose edge weights sum to
a negative number.
• Shortest paths aren’t defined when there are negative
cycles!
A -10
C 1
7
Bellman-Ford algorithm
• (-) Slower than Dijkstra’s algorithm
• Bellman-Ford:
• Don’t bother finding the u with the smallest d[u]
• Everyone updates!
9
Bellman-Ford =
Gates 0
How far is a node from Gates?
Gates Packard CS161 Union Dish 1
CS161
d(0) 0
d(1) 1
Packard
d(2) 4
d(3)
22
d(4) Union
25
20
• For i=0,…,n-2:
• For v in V:
Dish
• d(i+1)[v] min( d(i)[v] , d(i)[u] + w(u,v) )
where we are also taking the min over all u in v.inNeighbors 10
Bellman-Ford Gates 0
How far is a node from Gates?
Gates Packard CS161 Union Dish 1
CS161
d(0) 0
d(1) 0 1 25 1
Packard
d(2) 4
d(3)
22
d(4) Union
25
20
• For i=0,…,n-2:
• For v in V:
Dish
• d(i+1)[v] min( d(i)[v] , d(i)[u] + w(u,v) )
where we are also taking the min over all u in v.inNeighbors 11
Bellman-Ford Gates 0
How far is a node from Gates?
Gates Packard CS161 Union Dish 1
CS161
d(0) 0
d(1) 0 1 25 1
Packard
d(2) 0 1 2 45 23
4
d(3)
22
d(4) Union
25
20
• For i=0,…,n-2:
• For v in V:
Dish
• d(i+1)[v] min( d(i)[v] , d(i)[u] + w(u,v) )
where we are also taking the min over all u in v.inNeighbors 12
Bellman-Ford Gates 0
How far is a node from Gates?
Gates Packard CS161 Union Dish 1
CS161
d(0) 0
d(1) 0 1 25 1
Packard
d(2) 0 1 2 45 23
4
d(3) 0 1 2 6 23
22
d(4) Union
25
20
• For i=0,…,n-2:
• For v in V:
Dish
• d(i+1)[v] min( d(i)[v] , d(i)[u] + w(u,v) )
where we are also taking the min over all u in v.inNeighbors 13
Bellman-Ford Gates 0
How far is a node from Gates?
Gates Packard CS161 Union Dish 1
CS161
d(0) 0
d(1) 0 1 25 1
Packard
d(2) 0 1 2 45 23
4
d(3) 0 1 2 6 23
22
d(4) 0 1 2 6 23 Union
25
These are the final distances!
20
• For i=0,…,n-2:
• For v in V:
Dish
• d(i+1)[v] min( d(i)[v] , d(i)[u] + w(u,v) )
where we are also taking the min over all u in v.inNeighbors 14
Interpretation of d (i) Gates 0
1
CS161
d(i)[v]
is equal to the cost of the
shortest path between s and v
with at most i edges. 1
Packard
Gates Packard CS161 Union Dish 4
d(0) 0
d(1) 0 1 25 22
Union
d(2) 0 1 2 45 23 25
d(3) 0 1 2 6 23 20
d(4) 0 1 2 6 23 Dish
15
Why does Bellman-Ford work?
• Inductive hypothesis:
• d(i)[v] is equal to the cost of the shortest path between s
and v with at most i edges.
• Conclusion:
• d(n-1)[v] is equal to the cost of the shortest path between
s and v with at most n-1 edges.
Do the base case and
inductive step!
16
Aside: simple paths
Assume there is no negative cycle.
• Then there is a shortest path from s to t, and
moreover there is a simple shortest path.
-2 10
s v
2 t
-5
This cycle isn’t helping.
x y Just get rid of it.
3
Bellman-Ford* algorithm
vertices and m edges.
Bellman-Ford*(G,s):
• Initialize arrays d(0),…,d(n-1) of length n
• d(0)[v] = for all v in V
Here, Dijkstra picked a special vertex u and
• d(0) [s] = 0 updated u’s neighbors – Bellman-Ford will
• For i=0,…,n-2: update all the vertices.
• For v in V:
• d(i+1)[v] min( d(i)[v] , minu in v.inNbrs{d(i)[u] + w(u,v)} )
• Now, dist(s,v) = d(n-1)[v] for all v in V.
• (Assuming no negative cycles)
d(1) 0 1 25
d(2) 0 1 2 45 23
21
BF with negative cycles Gates
d(1) 0 1 -3
1
d(2) 0 -5 2 7 -3
Packard
4
d(3) -4 -5 -4 6 -3
d(4) -4 -5 -4 6 -7 -2
Union
This is not looking good!
-3
10
• For i=0,…,n-2:
• For v in V: Dish
• d(i+1)[v] min( d(i)[v] , minu in v.nbrs{d(i)[u] + w(u,v)} ) 22
BF with negative cycles Gates
d(1) 0 1 -3
1
d(2) 0 -5 2 7 -3
Packard
4
d(3) -4 -5 -4 6 -3
d(4) -4 -5 -4 6 -7 -2
But we can tell that it’s not looking good: Union
d(5) -4 -9 -4 3 -7 -3
Some stuff changed! 10
• For i=0,…,n-2:
• For v in V: Dish
• d(i+1)[v] min( d(i)[v] , minu in v.nbrs{d(i)[u] + w(u,v)} ) 23
Negative cycles in Bellman-Ford
24
Bellman-Ford algorithm
Bellman-Ford*(G,s):
• d(0)[v] = for all v in V
• d(0)[s] = 0
• For i=0,…,n-1:
• For v in V:
• d(i+1)[v] min( d(i)[v] , minu in v.inNeighbors {d(i)[u] + w(u,v)} )
• If d(n-1) != d(n) :
• Return NEGATIVE CYCLE
• Otherwise, dist(s,v) = d(n-1)[v]