0% found this document useful (0 votes)
29 views26 pages

09 BellmanFord

The Bellman-Ford algorithm can be used to find the shortest paths from a single source node to all other nodes in a graph, even if the graph contains negative edge weights, which may cause negative cycles. It works by iteratively updating the distance of each node from the source for n-1 rounds, where n is the number of nodes. This allows it to detect if a negative cycle exists to a node, in which case the shortest path is undefined.

Uploaded by

huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views26 pages

09 BellmanFord

The Bellman-Ford algorithm can be used to find the shortest paths from a single source node to all other nodes in a graph, even if the graph contains negative edge weights, which may cause negative cycles. It works by iteratively updating the distance of each node from the source for n-1 rounds, where n is the number of nodes. This allows it to detect if a negative cycle exists to a node, in which case the shortest path is undefined.

Uploaded by

huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Đồ thị: Đường đi

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.

• The cost of a path is the


• A weighted directed graph: sum of the weights
along that path.

• A shortest path from s


u to t is a directed path
s 3 32 from s to t with the
1 5 smallest cost.
1
This is a b v • The single-source
path from 13 shortest path problem is
a s to t of to find the shortest path
cost 22. 21 2
from s to v for all v in
the graph.
16
t This is a path from s to t of
cost 10. It is the shortest
path from s to t. 3
Last time
• Dijkstra’s algorithm!
• Solves the single-source shortest path problem in weighted
graphs.

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

• (+) Can handle negative edge weights.


• Can be useful if you want to say that some edges are
actively good to take, rather than costly.
• Can be useful as a building block in other algorithms.

• (+) Allows for some flexibility if the weights change.


• We’ll see what this means later

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

2 B The shortest path from A to B


has cost…negative infinity?

C 1
7
Bellman-Ford algorithm
• (-) Slower than Dijkstra’s algorithm

• (+) Can handle negative edge weights.


• Can detect negative cycles!
• Can be useful if you want to say that some edges are
actively good to take, rather than costly.
• Can be useful as a building block in other algorithms.

• (+) Allows for some flexibility if the weights change.


• We’ll see what this means later
8
Bellman-Ford vs. Dijkstra
• Dijkstra:
• Find the u with the smallest d[u]
• Update u’s neighbors: d[v] = min( d[v], d[u] + w(u,v) )

• 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

• A simple path in a graph with n vertices has at most


n-1 edges in it.
s u “Simple” means
Can’t add another edge that the path has
without making a cycle! t v no cycles in it.

• So there is a shortest path with at most n-1 edges 17


Why does it 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.
• If there are no negative cycles, d(n-1)[v] is equal to the
cost of the shortest path.

Notice that negative edge weights are fine.


Just not negative cycles. 18
G = (V,E) is a graph with n

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)

*Slightly different than some versions of Bellman-Ford…but


19
this way is pedagogically convenient for today’s lecture.
Note on implementation
• Don’t actually keep all n arrays around.
• Just keep two at a time: “last round” and “this round”

Gates Packard CS161 Union Dish


d(0) 0

d(1) 0 1 25

d(2) 0 1 2 45 23

d(3) 0 1 2 6 23 Only need these


two in order to
d(4) 0 1 2 6 23 compute d(4)
20
Bellman-Ford take-aways
• Running time is O(mn)
• For each of n rounds, update m edges.
• Works fine with negative edges.
• Does not work with negative cycles.
• No algorithm can – shortest paths aren’t defined if there
are negative cycles.
• B-F can detect negative cycles!
• See skipped slides to see how, or think about it on your
own!

21
BF with negative cycles Gates

Gates Packard CS161 Union Dish


d(0) 0 1
CS161

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

Gates Packard CS161 Union Dish


d(0) 0 1
CS161

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

• If there are no negative cycles:


• Everything works as it should, and stabilizes in n-1 rounds.
• If there are negative cycles:
• Not everything works as it should…
• The d[v] values will keep changing.
• Solution:
• Go one round more and see if things change.

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]

Running time: O(mn)


25
Important thing about B-F Gates 0
for the rest of this lecture
1
d(i)[v] is equal to the cost of the CS161

shortest path between s and v


with at most i edges. an example of… 1
Dynamic Programming! Packard
4
Gates Packard CS161 Union Dish
d(0) 0
22
d(1) 0 1 25 Union
25
d(2) 0 1 2 45 23
20
d(3) 0 1 2 6 23
Dish
d(4) 0 1 2 6 23
26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy