Montaser hamza
MSC Student
University of Erciyes
FROM IRAQ
Montaser_hamza@yahoo.com
 The algorithm is named after two of its developers, Richard
Bellman and Lester Ford, Jr., who published it in 1958 and 1956,
respectively; however, Edward F. Moore also published the same
algorithm in 1957, and for this reason it is also sometimes called
the Bellman–Ford–Moore algorithm.
 The Bellman–Ford algorithm is an algorithm that computes
shortest paths from a single source vertex to all of the other
vertices in a weighted graph . It is slower than Dijkstra's
algorithm for the same problem, but more versatile, as it is
capable of handling graphs in which some of the edge weights
are negative numbers.
 Negative edge weights are found in various applications of
graphs, hence the usefulness of this algorithm. If a graph contains
a "negative cycle" (i.e. a cycle whose edges sum to a negative
value) that is reachable from the source, then there is no
cheapest path: any path can be made cheaper by one more walk
around the negative cycle. In such a case, the Bellman–Ford
algorithm can detect negative cycles and report their existence.
 Shortest path network
Directed graph
Source s , Destination t
cost( v-u) cost of using edge from v to u
Shortest path problem
Find shortest directed path from s to t
Cost of path = sum of arc cost in path
 Networks (Routing ).
 Robot Navigation.
 Urban Traffic Planning.
 Telemarketer operator scheduling.
 Routing of Communication messages.
 Optimal truck routing through given traffic congestion
pattern.
 OSPF routing protocol for IP.
 Given graph (directed or undirected) G = (V,E) with
weight function w : E  R and a vertex s  V,
 find for all vertices v  V the minimum possible weight for
path from s to v.
 There are two algorithms:
1- Dijkstra’s Algorithm.
2- Bellman-Ford algorithm.
 Maintain d[v] for each v  V
 d[v] is called shortest-path weight estimate
INITIALIZE (G, s)
for each v  V do
d[v] ← ∞
π[v] ← NIL
d[s] ← 0
 Dijkstra's Algorithm fails when there is negative edge
Solution is Bellman Ford Algorithm which can work on negative edges
s-x-y-v
BELMAN-FORD( G, s )
INITIALIZE SINGLE-SOURCE ( G, s )
for i ←1 to |V|-1 do
for each edge (u, v)  G.E do
RELAX( u, v )
for each edge ( u, v )  E do
if d[v] > d[u]+w(u,v) then
return FALSE
return TRUE
computaion
check
 RELAX(u ,v)
 if d[v] > d[u]+w(u,v) then
 d[v] = d[u]+w(u,v)
 π[v] = [u]
5
2
2
9
5 7
Relax(u,v)
5
2
2
6
5 6
If has achieved the
condition
If not has achieved
the condition
s a b t
0 0 ∞ ∞ ∞
1 0
2 0
3 0
s
a
b
t
5
-2
4
6
-9
∞
∞
∞
0
What is the shortest path from s to b
s a b t
0 0 ∞ ∞ ∞
1 0 5 4 ∞
2 0
3 0
s
a
b
t
5
-2
4
6
-9
5
4
∞0
s a b t
0 0 ∞ ∞ ∞
1 0 5 4 ∞
2 0 5 3 11
3 0
s
a
b
t
5
-2
4
6
-9
3
11
5
0
s a b t
0 0 ∞ ∞ ∞
1 0 5 4 ∞
2 0 5 3 11
3 0 5 2 11
s
a
b
t
5
-2
4
6
-9
2
11
5
0
So the shortest path from s to b s-a-t-b
S A B C D E
0 0 ∞ ∞ ∞ ∞ ∞
1 0
2 0
3 0
4 0
E
S
B
A
8
1 - 4
10
-9
C
D
2
-2-1
 What is the shortest path from S to B
0
∞
∞
∞
∞
∞
S A B C D E
0 0 ∞ ∞ ∞ ∞ ∞
1 0 10 10 12 9 8
2 0
3 0
4 0
E
S
8
1 - 4
10
-9
C
D
2
-2-1
AE
B
C
D
0
8
9
12
10
10
S A B C D E
0 0 ∞ ∞ ∞ ∞ ∞
1 0 10 10 12 9 8
2 0 5 10 8 9 8
3 0
4 0
E
S
8
1 - 4
10
-9
C
D
2
-2-1
AE
B
C
D
0
9
10
58
8
S A B C D E
0 0 ∞ ∞ ∞ ∞ ∞
1 0 10 10 12 9 8
2 0 5 10 8 9 8
3 0 5 5 7 9 8
4 0
E
S
8
1 - 4
10
-9
C
D
2
-2-1
AE
B
C
D
0
9
5
58
7
S A B C D E
0 0 ∞ ∞ ∞ ∞ ∞
1 0 10 10 12 9 8
2 0 5 10 8 9 8
3 0 5 5 7 9 8
4 0 5 5 7 9 8
E
S
8
1 - 4
10
-9
C
D
2
-2-1
AE
B
C
D
0
9
5
58
7
So the shortest path from S to B S-E-D-A-C-B
S A B C D
0 0 ∞ ∞ ∞ ∞
1 0
2 0
3 0
A
S
D
B
4
-5
3
6
1
C
2
 What is the shortest path from S to C
0
∞∞
∞ ∞
S A B C D
0 0 ∞ ∞ ∞ ∞
1 0 4 6 ∞ ∞
2 0
3 0
A
S
D
B
4
-5
3
6
1
C
2
64
∞ ∞
0
S A B C D
0 0 ∞ ∞ ∞ ∞
1 0 4 6 ∞ ∞
2 0 1 6 7 7
3 0
A
S
D
B
4
-5
3
6
1
C
2
61
7 7
0
S A B C D
0 0 ∞ ∞ ∞ ∞
1 0 4 6 ∞ ∞
2 0 1 6 7 7
3 0 1 6 4 6
A
S
D
B
4
-5
3
6
1
C
2
61
4 6
0
So the shortest path from S to C S-B-A-C

Figure : Negative Cycle.
AS B V
-ve
-7
2
1= - ∞
Johnson’s algorithm :
w*(v,u) =w(v,u)+p(v)–p(u)
 https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algori
thm
 https://www.youtube.com/watch?v=obWXjtg0L64
 https://www.youtube.com/watch?v=dp-Ortfx1f4
Thank you

Bellmanford . montaser hamza.iraq

  • 1.
    Montaser hamza MSC Student Universityof Erciyes FROM IRAQ Montaser_hamza@yahoo.com
  • 3.
     The algorithmis named after two of its developers, Richard Bellman and Lester Ford, Jr., who published it in 1958 and 1956, respectively; however, Edward F. Moore also published the same algorithm in 1957, and for this reason it is also sometimes called the Bellman–Ford–Moore algorithm.  The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted graph . It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.
  • 4.
     Negative edgeweights are found in various applications of graphs, hence the usefulness of this algorithm. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect negative cycles and report their existence.
  • 5.
     Shortest pathnetwork Directed graph Source s , Destination t cost( v-u) cost of using edge from v to u Shortest path problem Find shortest directed path from s to t Cost of path = sum of arc cost in path
  • 6.
     Networks (Routing).  Robot Navigation.  Urban Traffic Planning.  Telemarketer operator scheduling.  Routing of Communication messages.  Optimal truck routing through given traffic congestion pattern.  OSPF routing protocol for IP.
  • 7.
     Given graph(directed or undirected) G = (V,E) with weight function w : E  R and a vertex s  V,  find for all vertices v  V the minimum possible weight for path from s to v.  There are two algorithms: 1- Dijkstra’s Algorithm. 2- Bellman-Ford algorithm.
  • 8.
     Maintain d[v]for each v  V  d[v] is called shortest-path weight estimate INITIALIZE (G, s) for each v  V do d[v] ← ∞ π[v] ← NIL d[s] ← 0
  • 9.
     Dijkstra's Algorithmfails when there is negative edge Solution is Bellman Ford Algorithm which can work on negative edges s-x-y-v
  • 10.
    BELMAN-FORD( G, s) INITIALIZE SINGLE-SOURCE ( G, s ) for i ←1 to |V|-1 do for each edge (u, v)  G.E do RELAX( u, v ) for each edge ( u, v )  E do if d[v] > d[u]+w(u,v) then return FALSE return TRUE computaion check
  • 11.
     RELAX(u ,v) if d[v] > d[u]+w(u,v) then  d[v] = d[u]+w(u,v)  π[v] = [u] 5 2 2 9 5 7 Relax(u,v) 5 2 2 6 5 6 If has achieved the condition If not has achieved the condition
  • 12.
    s a bt 0 0 ∞ ∞ ∞ 1 0 2 0 3 0 s a b t 5 -2 4 6 -9 ∞ ∞ ∞ 0 What is the shortest path from s to b
  • 13.
    s a bt 0 0 ∞ ∞ ∞ 1 0 5 4 ∞ 2 0 3 0 s a b t 5 -2 4 6 -9 5 4 ∞0
  • 14.
    s a bt 0 0 ∞ ∞ ∞ 1 0 5 4 ∞ 2 0 5 3 11 3 0 s a b t 5 -2 4 6 -9 3 11 5 0
  • 15.
    s a bt 0 0 ∞ ∞ ∞ 1 0 5 4 ∞ 2 0 5 3 11 3 0 5 2 11 s a b t 5 -2 4 6 -9 2 11 5 0 So the shortest path from s to b s-a-t-b
  • 16.
    S A BC D E 0 0 ∞ ∞ ∞ ∞ ∞ 1 0 2 0 3 0 4 0 E S B A 8 1 - 4 10 -9 C D 2 -2-1  What is the shortest path from S to B 0 ∞ ∞ ∞ ∞ ∞
  • 17.
    S A BC D E 0 0 ∞ ∞ ∞ ∞ ∞ 1 0 10 10 12 9 8 2 0 3 0 4 0 E S 8 1 - 4 10 -9 C D 2 -2-1 AE B C D 0 8 9 12 10 10
  • 18.
    S A BC D E 0 0 ∞ ∞ ∞ ∞ ∞ 1 0 10 10 12 9 8 2 0 5 10 8 9 8 3 0 4 0 E S 8 1 - 4 10 -9 C D 2 -2-1 AE B C D 0 9 10 58 8
  • 19.
    S A BC D E 0 0 ∞ ∞ ∞ ∞ ∞ 1 0 10 10 12 9 8 2 0 5 10 8 9 8 3 0 5 5 7 9 8 4 0 E S 8 1 - 4 10 -9 C D 2 -2-1 AE B C D 0 9 5 58 7
  • 20.
    S A BC D E 0 0 ∞ ∞ ∞ ∞ ∞ 1 0 10 10 12 9 8 2 0 5 10 8 9 8 3 0 5 5 7 9 8 4 0 5 5 7 9 8 E S 8 1 - 4 10 -9 C D 2 -2-1 AE B C D 0 9 5 58 7 So the shortest path from S to B S-E-D-A-C-B
  • 21.
    S A BC D 0 0 ∞ ∞ ∞ ∞ 1 0 2 0 3 0 A S D B 4 -5 3 6 1 C 2  What is the shortest path from S to C 0 ∞∞ ∞ ∞
  • 22.
    S A BC D 0 0 ∞ ∞ ∞ ∞ 1 0 4 6 ∞ ∞ 2 0 3 0 A S D B 4 -5 3 6 1 C 2 64 ∞ ∞ 0
  • 23.
    S A BC D 0 0 ∞ ∞ ∞ ∞ 1 0 4 6 ∞ ∞ 2 0 1 6 7 7 3 0 A S D B 4 -5 3 6 1 C 2 61 7 7 0
  • 24.
    S A BC D 0 0 ∞ ∞ ∞ ∞ 1 0 4 6 ∞ ∞ 2 0 1 6 7 7 3 0 1 6 4 6 A S D B 4 -5 3 6 1 C 2 61 4 6 0 So the shortest path from S to C S-B-A-C
  • 25.
     Figure : NegativeCycle. AS B V -ve -7 2 1= - ∞
  • 37.
  • 38.
  • 39.