Bellmanford . montaser hamza.iraq

39
BELLMAN - FORD ALGORITHM Montaser hamza MSC Student University of Erciyes FROM IRAQ [email protected]

Transcript of Bellmanford . montaser hamza.iraq

Page 1: Bellmanford . montaser hamza.iraq

BELLMAN - FORD ALGORITHM

Montaser hamza

MSC Student

University of ErciyesFROM IRAQ

[email protected]

Page 2: Bellmanford . montaser hamza.iraq

BELLMAN - FORD

Page 3: Bellmanford . montaser hamza.iraq

BELLMAN - FORD ALGORITHM 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.

Page 4: Bellmanford . montaser hamza.iraq

BELLMAN - FORD ALGORITHM

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.

Page 5: Bellmanford . montaser hamza.iraq

SHORTEST PATH PROBLEM Shortest path network

Directed graphSource s , Destination tcost( v-u) cost of using edge from v to u

Shortest path problem Find shortest directed path from s to tCost of path = sum of arc cost in path

Page 6: Bellmanford . montaser hamza.iraq

APPLICATIONS 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.

Page 7: Bellmanford . montaser hamza.iraq

SINGLE SOURCE SHORTEST PATH 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.

Page 8: Bellmanford . montaser hamza.iraq

RELAXATION 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

Page 9: Bellmanford . montaser hamza.iraq

BELLMAN FORD Dijkstra's Algorithm fails when there is negative edge

Solution is Bellman Ford Algorithm which can work on negative edges

s-x-y-v

Page 10: Bellmanford . montaser hamza.iraq

PSUEDOCODE 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

Page 11: Bellmanford . montaser hamza.iraq

RELAXATION

RELAX(u ,v) if d[v] > d[u]+w(u,v) then d[v] = d[u]+w(u,v) π[v] = [u]

52

2

9

5 7

Relax(u,v)

52

2

6

5 6

If has achieved the condition

If not has achieved the condition

Page 12: Bellmanford . montaser hamza.iraq

EXAMPLE 1 :

s a b t

0 0 ∞ ∞ ∞

1 0

2 0

3 0s

a

b

t

5

-2

4

6

-9

0

What is the shortest path from s to b

Page 13: Bellmanford . montaser hamza.iraq

EXAMPLE 1 :

s a b t

0 0 ∞ ∞ ∞

1 0 5 4 ∞

2 0

3 0s

a

b

t

5

-2

4

6

-9

5

4

∞0

Page 14: Bellmanford . montaser hamza.iraq

EXAMPLE 1 :

s a b t

0 0 ∞ ∞ ∞

1 0 5 4 ∞

2 0 5 3 11

3 0s

a

b

t

5

-2

4

6

-9

3

11

5

0

Page 15: Bellmanford . montaser hamza.iraq

EXAMPLE 1 :

s a b t

0 0 ∞ ∞ ∞

1 0 5 4 ∞

2 0 5 3 11

3 0 5 2 11s

a

b

t

5

-2

4

6

-9

2

11

5

0

So the shortest path from s to b s-a-t-b

Page 16: Bellmanford . montaser hamza.iraq

EXAMPLE 2 :

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

D2

-2-1

What is the shortest path from S to B0

Page 17: Bellmanford . montaser hamza.iraq

EXAMPLE 2 :

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

D2

-2-1

AE

B

C

D

0

8

9

12

10

10

Page 18: Bellmanford . montaser hamza.iraq

EXAMPLE 2 :

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

D2

-2-1

AE

B

C

D

0

9 10

58

8

Page 19: Bellmanford . montaser hamza.iraq

EXAMPLE 2 :

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

D2

-2-1

AE

B

C

D

0

9 5

58

7

Page 20: Bellmanford . montaser hamza.iraq

EXAMPLE 2 :

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

D2

-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

Page 21: Bellmanford . montaser hamza.iraq

EXAMPLE 3 :

S A B C D

0 0 ∞ ∞ ∞ ∞

1 0

2 0

3 0A

S

D

B

4

-5

3

6

1

C2

What is the shortest path from S to C

0

∞∞

∞ ∞

Page 22: Bellmanford . montaser hamza.iraq

EXAMPLE 3 :

S A B C D

0 0 ∞ ∞ ∞ ∞

1 0 4 6 ∞ ∞

2 0

3 0A

S

D

B

4

-5

3

6

1

C2

64

∞ ∞

0

Page 23: Bellmanford . montaser hamza.iraq

EXAMPLE 3 :

S A B C D

0 0 ∞ ∞ ∞ ∞

1 0 4 6 ∞ ∞

2 0 1 6 7 7

3 0A

S

D

B

4

-5

3

6

1

C2

61

7 7

0

Page 24: Bellmanford . montaser hamza.iraq

EXAMPLE 3 :

S A B C D

0 0 ∞ ∞ ∞ ∞

1 0 4 6 ∞ ∞

2 0 1 6 7 7

3 0 1 6 4 6A

S

D

B

4

-5

3

6

1

C2

61

4 6

0

So the shortest path from S to C S-B-A-C

Page 25: Bellmanford . montaser hamza.iraq

NEGATIVE CYCLES :

Figure : Negative Cycle.

AS B V

-ve

-7

2

1= - ∞

Page 26: Bellmanford . montaser hamza.iraq
Page 27: Bellmanford . montaser hamza.iraq
Page 28: Bellmanford . montaser hamza.iraq
Page 29: Bellmanford . montaser hamza.iraq
Page 30: Bellmanford . montaser hamza.iraq
Page 31: Bellmanford . montaser hamza.iraq
Page 32: Bellmanford . montaser hamza.iraq
Page 33: Bellmanford . montaser hamza.iraq
Page 34: Bellmanford . montaser hamza.iraq
Page 35: Bellmanford . montaser hamza.iraq
Page 36: Bellmanford . montaser hamza.iraq
Page 37: Bellmanford . montaser hamza.iraq

Johnson’s algorithm :w*(v,u) =w(v,u)+p(v)–p(u)

Page 38: Bellmanford . montaser hamza.iraq

REFERENCE :

https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm

https://www.youtube.com/watch?v=obWXjtg0L64

https://www.youtube.com/watch?v=dp-Ortfx1f4

Page 39: Bellmanford . montaser hamza.iraq

ANY QUESTION?

Thank you