Daniel Sutantyo, Department of Computing, Macquarie University
8.2 - Bellman-Ford Algorithm
8.2 - Bellman-Ford Algorithm
(the algorithm is actually very simple, relax every edge \(|V|-1\) times
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(\infty\)
\(\infty\)
\(\infty\)
\(\infty\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
s | b | c | d | e |
---|---|---|---|---|
0 | inf | inf | inf | inf |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(\infty\)
\(\infty\)
\(\infty\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
s | b | c | d | e |
---|---|---|---|---|
0 | inf | inf | inf | -4 |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(8\)
\(\infty\)
\(\infty\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | inf | inf | -4 |
\(\delta(b) = 8\)
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(8\)
\(\infty\)
\(\infty\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | inf | inf | -4 |
\(\delta(b) = 8\)
no change
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(8\)
\(17\)
\(\infty\)
\(-4\)
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | inf | 17 | -4 |
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
no change
\(8\)
\(17\)
\(\infty\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | inf | 17 | -4 |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
no change
\(\delta(c) = 12\)
\(8\)
\(17\)
\(12\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | 12 | 17 | -4 |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
no change
\(\delta(c) = 12\)
\(\delta(d) = -3\)
\(8\)
\(-3\)
\(12\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | 12 | -3 | -4 |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(\delta(e) = -4\)
\(\delta(b) = 8\)
no change
\(\delta(d) = 17\)
no change
\(\delta(c) = 12\)
\(\delta(d) = -3\)
\(8\)
\(-3\)
\(12\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | 12 | -3 | -4 |
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(8\)
\(-3\)
\(12\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | 12 | -3 | -4 |
The weight of the shortest path to from \(s\) to \(c\) is -8, however, this requires us to relax \((s,e)\), \((e,d)\), and then \((d,c)\)
We didn't relax the paths in this order
So what do we do now? Keep going
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(8\)
\(-3\)
\(-8\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | 8 | -8 | -3 | -4 |
no change
no change
\(\delta(c) = -8\)
no change
no change
no change
no change
8.2 - Bellman-Ford Algorithm
e
b
s
d
c
8
3
9
-5
-4
1
7
\(0\)
\((s,e)\)
\((b,e)\)
\((s,b)\)
\((b,d)\)
\((c,b)\)
\((d,c)\)
\((e,d)\)
\(-1\)
\(-3\)
\(-8\)
\(-4\)
s | b | c | d | e |
---|---|---|---|---|
0 | -1 | -8 | -3 | -4 |
no change
no change
no change
no change
no change
\(\delta(b) = -1\)
no change
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
1
2
3
4
1
2
3
4
1
2
3
4
8.2 - Bellman-Ford Algorithm
(the algorithm is actually very simple, relax every edge \(|V|-1\) times
8.2 - Bellman-Ford Algorithm
8.2 - Bellman-Ford Algorithm
8.2 - Bellman-Ford Algorithm