16
Zain Zahid (bscs) Air university, islamabad [email protected] * BELLMAN FORD ALGO (PART- II)

Bellman ford (part-ii)

Embed Size (px)

Citation preview

Page 1: Bellman ford (part-ii)

Zain Zahid (bscs)Air university, [email protected]

* BELLMAN FORD ALGO (PART-II)

Page 2: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

Vertices

Distance

Parent

A 0 0B ∞ 0C ∞ 0D ∞ 0E ∞ 0F ∞ 0G ∞ 0H ∞ 0 ∞

0

∞∞

∞∞

2

For demonstration purpose, we would consider the following graph:• Considering A as the source, assign it the distance zero.• For all vertices except A assign a distance infinity.

Page 3: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

∞∞

∞∞

Vertices

Distance

Parent

A 0 0B 8 ACDEFGH

2

Page 4: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

6∞

∞∞

Vertices

Distance

Parent

A 0 0B 8 ACDE 6 AFGH

2

Page 5: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

6∞

∞14

Vertices

Distance

Parent

A 0 0B 8 AC 14 BDE 6 AFGH

2

Page 6: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

69

∞14

Vertices

Distance

Parent

A 0 0B 8 AC 14 BDE 6 AF 9 EGH

2

Page 7: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

69

8

∞14

Vertices

Distance

Parent

A 0 0B 8 AC 14 BDE 6 AF 9 EG 8 EH

2

Page 8: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

∞14

Vertices

Distance

Parent

A 0 0B 8 AC 14 BD 9 GE 6 AF 9 EG 8 EH

2

Page 9: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

1814

Vertices

Distance

Parent

A 0 0B 8 AC 14 BD 9 GE 6 AF 9 EG 8 EH 18 C

2

Page 10: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

1814

Vertices

Distance

Parent

A 0 0B 8 AC 14 BD 9 GE 6 AF 9 EG 8 EH 18 C

2

STEP-2 START CHECKING EACH VERTEX AGAIN

B D E F G H remains same

Page 11: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

187

Vertices

Distance

Parent

A 0 0B 8 AC 7 GD 9 GE 6 AF 9 EG 8 EH 18 C

2

Change will be only in C

Page 12: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

117

Vertices

Distance

Parent

A 0 0B 8 AC 7 GD 9 GE 6 AF 9 EG 8 EH 11 C

2

STEP-3 START CHECKING EACH VERTEX AGAIN

Change will be only in H

Page 13: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

117

Vertices

Distance

Parent

A 0 0B 8 AC 7 GD 9 GE 6 AF 9 EG 8 EH 11 C

2

And B C D E F G remains same

Page 14: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

117

Vertices

Distance

Parent

A 0 0B 8 AC 7 GD 9 GE 6 AF 9 EG 8 EH 11 C

2

STEP-4 START CHECKING EACH VERTEX AGAIN

This time there is no change and all vertices remain sameSo we can stop here

Page 15: Bellman ford (part-ii)

A

EF

G

C

D

BH

6

-1

4

6

2

3

1

-2

6

8

8

0

9

69

8

117

Vertices

Distance

Parent

A 0 0B 8 AC 7 GD 9 GE 6 AF 9 EG 8 EH 11 C

2

Shortest Paths are:B=AB=8C=AEGC=7D=AEGD=9E=AE=6F=AEF=9G=AEG=8H=AEGCH=11

Page 16: Bellman ford (part-ii)

*There are two cases in iterations

*We might not notice any change in distance for any vertex in later iterations. It would be good to stop at that point for making the algorithm efficient.

*We might endlessly notice changes in distances of one or the other vertex in later iterations. This will be the case of negative weight cycles. Hence, we need to stop at some point and that would be the after |V| – 1