Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single Source Shortest Paths Bellman-Ford Algorithm

Similar presentations


Presentation on theme: "Single Source Shortest Paths Bellman-Ford Algorithm"— Presentation transcript:

1 Single Source Shortest Paths Bellman-Ford Algorithm
2 4 1 1 b -3 -5 3 source a b c e f 2 -3 1 3 4 -5 What is a shortest path from a to b ? a -> b = 2 a -> e -> c -> b = 0

2 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Initially: The source is assigned d-attribute 0, every other vertex is assigned ∞. No vertex has a parent vertex. current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

3 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: Each edge is visited and relaxed once, starting with edge (a,b). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL ->2,NIL->a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

4 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (a,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a , NIL ->-3,NIL->a We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

5 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (f,a). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π still 0, NIL 2, a , NIL -3, a We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

6 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (b,c). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a ->3,NIL->b -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

7 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (c,b). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL still 2, a 3, b -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

8 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (e,c). This results in a second update of the attributes of c within the same phase. current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a 3->-1,b->e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

9 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (c,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a -1, e still -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

10 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (f,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a -1, e still -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

11 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 1: We are relaxing the next edge, (f,c). This completes Phase 1. current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a still -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

12 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: Each edge is visited and relaxed once, starting with edge (a,b). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL still 2, a -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

13 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (a,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a -1, e still -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

14 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (f,a). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π still 0, NIL 2, a -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

15 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (b,c). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2, a still -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

16 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (c,b). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 2->0,a->c -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

17 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (e,c). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 0, c still -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

18 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (c,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 0, c -1, e still -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

19 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (f,e). current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 0, c -1, e still -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

20 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 2: We are relaxing the next edge, (f,c). This completes Phase 2. current attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 0, c still -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

21 Bellman-Ford Algorithm
c f 2 4 1 1 b -3 -5 3 source Phase 3: As in every phase, each edge is relaxed once. In this phase, no successful relaxation occurs, thus, we have found the final result. final attributes d and π a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 0, NIL 0, c -1, e -3, a , NIL We implement the algorithm to visit the edges in each phase in this order: (a,b), (a,e), (f,a), (b,c), (c,b), (e,c), (c,e), (f,e), (f,c)

22 The Shortest Path Tree rooted at a found by Bellman-Ford
c f 2 4 1 1 b -3 -5 3 source Vertex f is not part of the shortest path tree as it is not reachable from the source.


Download ppt "Single Source Shortest Paths Bellman-Ford Algorithm"

Similar presentations


Ads by Google