Successfully reported this slideshow.
Upcoming SlideShare
×

# Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

10,103 views

Published on

Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

Published in: Education, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

1. 1. Bellman-Ford-Moore AlgorithmSingle-source shortest path algorithm
2. 2. Bellman-Ford-Moore AlgorithmA.A. 2012/2013Tecniche di programmazione2 Solution to the single-source shortest path (SSSP)problem in graph theory Based on relaxation (for every vertex, relax all possibleedges) Does not work in presence of negative cycles but it is able to detect the problem O(V∙E)
3. 3. Bellman-Ford-Moore AlgorithmA.A. 2012/2013Tecniche di programmazione3dist[s] ← 0 (distance to source vertex is zero)for all v ∈ V–{s}do dist[v] ←∞ (set all other distances to infinity)for i← 0 to |V|for all (u, v) ∈ Edo if dist[v] > dist[u] + w(u, v) (if new shortest path found)then d[v] ←d[u] + w(u, v) (set new value of shortest path)(if desired, add traceback code)for all (u, v) ∈ E (sanity check)do if dist[v] > dist[u] + w(u, v)then PANIC!
4. 4. Dijkstra’s AlgorithmYet another SSSP algorithm
5. 5. Dijkstra’s algorithmA.A. 2012/2013Tecniche di programmazione5 Solution to the single-source shortest path (SSSP)problem in graph theory Works on both directed and undirected graphs All edges must have nonnegative weights the algorithm would miserably fail Greedy… but guarantees the optimum!
6. 6. Dijkstra’s algorithmA.A. 2012/2013Tecniche di programmazione6dist[s] ←0 (distance to source vertex is zero)for all v ∈ V–{s}do dist[v] ←∞ (set all other distances to infinity)S←∅ (S, the set of visited vertices is initially empty)Q←V (Q, the queue initially contains all vertices)while Q ≠∅ (while the queue is not empty)do u ← mindistance(Q,dist) (select e  Q with the min. distance)S←S∪{u} (add u to list of visited vertices)for all v ∈ neighbors[u]do if dist[v] > dist[u] + w(u, v) (if new shortest path found)then d[v] ←d[u] + w(u, v) (set new value of shortest path)(if desired, add traceback code)
7. 7. Dijkstra Animated Example
8. 8. Dijkstra Animated Example
9. 9. Dijkstra Animated Example
10. 10. Dijkstra Animated Example
11. 11. Dijkstra Animated Example
12. 12. Dijkstra Animated Example
13. 13. Dijkstra Animated Example
14. 14. Dijkstra Animated Example
15. 15. Dijkstra Animated Example
16. 16. Dijkstra Animated Example
17. 17. Why it worksA.A. 2012/2013Tecniche di programmazione17 A formal proof would take longer than this presentation,but we can understand how the argument worksintuitively Think of Djikstra’s algorithm as a water-filling algorithm Remember that all edge’s weights are positive
18. 18. Dijkstra efficiencyA.A. 2012/2013Tecniche di programmazione18 The simplest implementation is:O(𝐸 + 𝑉2) But it can be implemented more efficently:O(𝐸 + 𝑉 ∙ log 𝑉)Floyd–Warshall: O(V3)Bellman-Ford-Moore : O(V∙E)
19. 19. ApplicationsA.A. 2012/2013Tecniche di programmazione19 Dijkstra’s algorithm calculates the shortest path to everyvertex from vertex s (SSSP) It is about as computationally expensive to calculate theshortest path from vertex u to every vertex usingDijkstra’s as it is to calculate the shortest path to someparticular vertex t Therefore, anytime we want to know the optimal path tosome other vertex t from a determined origin s, we canuse Dijkstra’s algorithm (and stop as soon t exit from Q)
20. 20. ApplicationsA.A. 2012/2013Tecniche di programmazione20 Traffic Information Systems are most prominent use Mapping (Map Quest, Google Maps) Routing Systems
21. 21. 21Dijkstras Shortest Path Algorithm Find shortest path from s to ts3t267452418291415530204416116196s3t26745
22. 22. 22Dijkstras Shortest Path Algorithms3t2674524182914155302044161161960distance labelS = { }Q = { s, 2, 3, 4, 5, 6, 7, t }s3t26745
23. 23. 23Dijkstras Shortest Path Algorithms3t2674524182914155302044161161960distance labelS = { }Q = { s, 2, 3, 4, 5, 6, 7, t }delmin
24. 24. 24Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140distance labelS = { s }Q = { 2, 3, 4, 5, 6, 7, t }decrease keyXXXs3t26745
25. 25. 25Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140distance labelS = { s }Q = { 2, 3, 4, 5, 6, 7, t }XXXdelmins3t26745
26. 26. 26Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2 }Q = { 3, 4, 5, 6, 7, t }XXXs3t26745
27. 27. 27Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2 }Q = { 3, 4, 5, 6, 7, t }XXXdecrease keyX 33s3t26745
28. 28. 28Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2 }Q = { 3, 4, 5, 6, 7, t }XXXX 33delmins3t26745
29. 29. 29Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 6 }Q = { 3, 4, 5, 7, t }XXXX 3344XX32s3t26745
30. 30. 30Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 6 }Q = { 3, 4, 5, 7, t }XXX44XdelminX 33X32s3t26745
31. 31. 31Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 6, 7 }Q = { 3, 4, 5, t }XXX44X35X59 X24X 33X32s3t26745
32. 32. 32Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 6, 7 }Q = { 3, 4, 5, t }XXX44X35X59 XdelminX 33X32s3t26745
33. 33. 33Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 3, 6, 7 }Q = { 4, 5, t }XXX44X35X59 XX51X 34X 33X32s3t26745
34. 34. 34Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 3, 6, 7 }Q = { 4, 5, t }XXX44X35X59 XX51X 34delminX 33X3224s3t26745
35. 35. 35Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 3, 5, 6, 7 }Q = { 4, t }XXX44X35X59 XX51X 3424X50X45X 33X32s3t26745
36. 36. 36Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 3, 5, 6, 7 }Q = { 4, t }XXX44X35X59 XX51X 3424X50X45delminX 33X32s3t26745
37. 37. 37Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 3, 4, 5, 6, 7 }Q = { t }XXX44X35X59 XX51X 3424X50X45X 33X32s3t26745
38. 38. 38Dijkstras Shortest Path Algorithms3t2674518291415530204416116196159140S = { s, 2, 3, 4, 5, 6, 7 }Q = { t }XXX44X35X59 XX51X 34X50X45delminX 33X3224s3t26745
39. 39. 39Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 3, 4, 5, 6, 7, t }Q = { }XXX44X35X59 XX51X 34X50X45X 33X32s3t26745
40. 40. 40Dijkstras Shortest Path Algorithms3t267452418291415530204416116196159140S = { s, 2, 3, 4, 5, 6, 7, t }Q = { }XXX44X35X59 XX51X 34X50X45X 33X32s3t26745
41. 41. Shortest Paths wrap-upA.A. 2012/2013Tecniche di programmazione41Algorithm Problem Efficiency LimitationFloyd-Warshall AP O(𝑉3) No negative cyclesBellman–Ford SS O(𝑉 ∙ 𝐸) No negative cyclesRepeated- Bellman-Ford AP O(𝑉2∙ 𝐸) No negative cyclesDijkstra SS O(𝐸 + 𝑉 ∙ log 𝑉) No negative edgesRepeated Dijkstra AP O(𝑉 ∙ 𝐸 + 𝑉2 ∙ log 𝑉) No negative edges
42. 42. JGraphTA.A. 2012/2013Tecniche di programmazione42public class FloydWarshallShortestPaths<V,E>public class BellmanFordShortestPath<V,E>public class DijkstraShortestPath<V,E>// APSPList<GraphPath<V,E>> getShortestPaths(V v)GraphPath<V,E> getShortestPath(V a, V b)// SSSPsGraphPath<V,E> getPath()
43. 43. Licenza d’usoA.A. 2012/2013Tecniche di programmazione43 Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CCBY-NC-SA)” Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,rappresentare, eseguire e recitare questopera di modificare questopera Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dellopera agli autorioriginali e in modo tale da non suggerire che essi avallino te o il modo incui tu usi lopera. Non commerciale — Non puoi usare questopera per finicommerciali. Condividi allo stesso modo — Se alteri o trasformi questopera, o sela usi per crearne unaltra, puoi distribuire lopera risultante solo con unalicenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/3.0/