Bellmanford . montaser hamza.iraq

Preview:

Citation preview

BELLMAN - FORD ALGORITHM

Montaser hamza

MSC Student

University of ErciyesFROM IRAQ

Montaser_hamza@yahoo.com

BELLMAN - FORD

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.

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.

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

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

∞∞

∞ ∞

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

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

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

NEGATIVE CYCLES :

Figure : Negative Cycle.

AS B V

-ve

-7

2

1= - ∞

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

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

ANY QUESTION?

Thank you

Recommended