SlideShare uma empresa Scribd logo
1 de 43
Baixar para ler offline
Bellman-Ford-Moore Algorithm
Single-source shortest path algorithm
Bellman-Ford-Moore Algorithm
A.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 possible
edges)
 Does not work in presence of negative cycles
 but it is able to detect the problem
 O(V∙E)
Bellman-Ford-Moore Algorithm
A.A. 2012/2013Tecniche di programmazione3
dist[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) ∈ E
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)
for all (u, v) ∈ E (sanity check)
do if dist[v] > dist[u] + w(u, v)
then PANIC!
Dijkstra’s Algorithm
Yet another SSSP algorithm
Dijkstra’s algorithm
A.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!
Dijkstra’s algorithm
A.A. 2012/2013Tecniche di programmazione6
dist[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)
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Why it works
A.A. 2012/2013Tecniche di programmazione17
 A formal proof would take longer than this presentation,
but we can understand how the argument works
intuitively
 Think of Djikstra’s algorithm as a water-filling algorithm
 Remember that all edge’s weights are positive
Dijkstra efficiency
A.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)
Applications
A.A. 2012/2013Tecniche di programmazione19
 Dijkstra’s algorithm calculates the shortest path to every
vertex from vertex s (SSSP)
 It is about as computationally expensive to calculate the
shortest path from vertex u to every vertex using
Dijkstra’s as it is to calculate the shortest path to some
particular vertex t
 Therefore, anytime we want to know the optimal path to
some other vertex t from a determined origin s, we can
use Dijkstra’s algorithm (and stop as soon t exit from Q)
Applications
A.A. 2012/2013Tecniche di programmazione20
 Traffic Information Systems are most prominent use
 Mapping (Map Quest, Google Maps)
 Routing Systems
21
Dijkstra's Shortest Path Algorithm
 Find shortest path from s to t
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
s
3
t
2
6
7
4
5
22
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6







0
distance label
S = { }
Q = { s, 2, 3, 4, 5, 6, 7, t }
s
3
t
2
6
7
4
5
23
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6







0
distance label
S = { }
Q = { s, 2, 3, 4, 5, 6, 7, t }
delmin
24
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
distance label
S = { s }
Q = { 2, 3, 4, 5, 6, 7, t }
decrease key
X

X
X
s
3
t
2
6
7
4
5
25
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
distance label
S = { s }
Q = { 2, 3, 4, 5, 6, 7, t }
X

X
X
delmin
s
3
t
2
6
7
4
5
26
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }
X

X
X
s
3
t
2
6
7
4
5
27
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }
X

X
X
decrease key
X 33
s
3
t
2
6
7
4
5
28
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }
X

X
X
X 33
delmin
s
3
t
2
6
7
4
5
29
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
S = { s, 2, 6 }
Q = { 3, 4, 5, 7, t }
X

X
X
X 33
44
X
X
32
s
3
t
2
6
7
4
5
30
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 6 }
Q = { 3, 4, 5, 7, t }
X

X
X
44
X
delmin
X 33X
32
s
3
t
2
6
7
4
5
31
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 6, 7 }
Q = { 3, 4, 5, t }
X

X
X
44
X
35X
59 X
24
X 33X
32
s
3
t
2
6
7
4
5
32
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 6, 7 }
Q = { 3, 4, 5, t }
X

X
X
44
X
35X
59 X
delmin
X 33X
32
s
3
t
2
6
7
4
5
33
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 6, 7 }
Q = { 4, 5, t }
X

X
X
44
X
35X
59 XX51
X 34
X 33X
32
s
3
t
2
6
7
4
5
34
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 6, 7 }
Q = { 4, 5, t }
X

X
X
44
X
35X
59 XX51
X 34
delmin
X 33X
32
24
s
3
t
2
6
7
4
5
35
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 5, 6, 7 }
Q = { 4, t }
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
X 33X
32
s
3
t
2
6
7
4
5
36
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 5, 6, 7 }
Q = { 4, t }
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
delmin
X 33X
32
s
3
t
2
6
7
4
5
37
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 4, 5, 6, 7 }
Q = { t }
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
X 33X
32
s
3
t
2
6
7
4
5
38
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 4, 5, 6, 7 }
Q = { t }
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
delmin
X 33X
32
24
s
3
t
2
6
7
4
5
39
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 4, 5, 6, 7, t }
Q = { }
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
X 33X
32
s
3
t
2
6
7
4
5
40
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
S = { s, 2, 3, 4, 5, 6, 7, t }
Q = { }
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
X 33X
32
s
3
t
2
6
7
4
5
Shortest Paths wrap-up
A.A. 2012/2013Tecniche di programmazione41
Algorithm Problem Efficiency Limitation
Floyd-Warshall AP O(𝑉3
) No negative cycles
Bellman–Ford SS O(𝑉 ∙ 𝐸) No negative cycles
Repeated- Bellman-Ford AP O(𝑉2
∙ 𝐸) No negative cycles
Dijkstra SS O(𝐸 + 𝑉 ∙ log 𝑉) No negative edges
Repeated Dijkstra AP O(𝑉 ∙ 𝐸 + 𝑉2 ∙ log 𝑉) No negative edges
JGraphT
A.A. 2012/2013Tecniche di programmazione42
public class FloydWarshallShortestPaths<V,E>
public class BellmanFordShortestPath<V,E>
public class DijkstraShortestPath<V,E>
// APSP
List<GraphPath<V,E>> getShortestPaths(V v)
GraphPath<V,E> getShortestPath(V a, V b)
// SSSPs
GraphPath<V,E> getPath()
Licenza d’uso
A.A. 2012/2013Tecniche di programmazione43
 Queste diapositive sono distribuite con licenza Creative Commons
“Attribuzione - Non commerciale - Condividi allo stesso modo (CC
BY-NC-SA)”
 Sei libero:
 di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
 di modificare quest'opera
 Alle seguenti condizioni:
 Attribuzione — Devi attribuire la paternità dell'opera agli autori
originali e in modo tale da non suggerire che essi avallino te o il modo in
cui tu usi l'opera.
 Non commerciale — Non puoi usare quest'opera per fini
commerciali.
 Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se
la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una
licenza identica o equivalente a questa.
 http://creativecommons.org/licenses/by-nc-sa/3.0/

Mais conteúdo relacionado

Mais procurados

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen ProblemSukrit Gupta
 
Bellman ford
Bellman fordBellman ford
Bellman fordKiran K
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHCool Guy
 
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWELattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWEPriyanka Aash
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
Vertex cover problem
Vertex cover problemVertex cover problem
Vertex cover problemGinusaju
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approachAkash Sethiya
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic ProgrammingFenil Shah
 
implementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity pptimplementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity pptAntaraBhattacharya12
 

Mais procurados (20)

Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
8 queen problem
8 queen problem8 queen problem
8 queen problem
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Huffman Encoding Pr
Huffman Encoding PrHuffman Encoding Pr
Huffman Encoding Pr
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCH
 
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWELattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Vertex cover problem
Vertex cover problemVertex cover problem
Vertex cover problem
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
implementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity pptimplementation of travelling salesman problem with complexity ppt
implementation of travelling salesman problem with complexity ppt
 

Semelhante a Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s Krish_ver2
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batchJaimin Jani
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest pathMohammad Akbarizadeh
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emadKazi Emad
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problempooja saini
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfShiwani Gupta
 
Session 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptxSession 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptxSATHWIKCHAKRI
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 

Semelhante a Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm (20)

2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s
 
Jaimin chp-5 - network layer- 2011 batch
Jaimin   chp-5 - network layer- 2011 batchJaimin   chp-5 - network layer- 2011 batch
Jaimin chp-5 - network layer- 2011 batch
 
dijkstras example.ppt
dijkstras example.pptdijkstras example.ppt
dijkstras example.ppt
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
Design and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problemDesign and Analysis of Algorithm -Shortest paths problem
Design and Analysis of Algorithm -Shortest paths problem
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
Lecture#9
Lecture#9Lecture#9
Lecture#9
 
12_Graph.pptx
12_Graph.pptx12_Graph.pptx
12_Graph.pptx
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Session 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptxSession 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptx
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 

Último

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 

Último (20)

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 

Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

  • 2. Bellman-Ford-Moore Algorithm A.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 possible edges)  Does not work in presence of negative cycles  but it is able to detect the problem  O(V∙E)
  • 3. Bellman-Ford-Moore Algorithm A.A. 2012/2013Tecniche di programmazione3 dist[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) ∈ E 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) for all (u, v) ∈ E (sanity check) do if dist[v] > dist[u] + w(u, v) then PANIC!
  • 5. Dijkstra’s algorithm A.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. Dijkstra’s algorithm A.A. 2012/2013Tecniche di programmazione6 dist[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)
  • 17. Why it works A.A. 2012/2013Tecniche di programmazione17  A formal proof would take longer than this presentation, but we can understand how the argument works intuitively  Think of Djikstra’s algorithm as a water-filling algorithm  Remember that all edge’s weights are positive
  • 18. Dijkstra efficiency A.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. Applications A.A. 2012/2013Tecniche di programmazione19  Dijkstra’s algorithm calculates the shortest path to every vertex from vertex s (SSSP)  It is about as computationally expensive to calculate the shortest path from vertex u to every vertex using Dijkstra’s as it is to calculate the shortest path to some particular vertex t  Therefore, anytime we want to know the optimal path to some other vertex t from a determined origin s, we can use Dijkstra’s algorithm (and stop as soon t exit from Q)
  • 20. Applications A.A. 2012/2013Tecniche di programmazione20  Traffic Information Systems are most prominent use  Mapping (Map Quest, Google Maps)  Routing Systems
  • 21. 21 Dijkstra's Shortest Path Algorithm  Find shortest path from s to t s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 s 3 t 2 6 7 4 5
  • 22. 22 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6        0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } s 3 t 2 6 7 4 5
  • 23. 23 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6        0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } delmin
  • 24. 24 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } decrease key X  X X s 3 t 2 6 7 4 5
  • 25. 25 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } X  X X delmin s 3 t 2 6 7 4 5
  • 26. 26 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X  X X s 3 t 2 6 7 4 5
  • 27. 27 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X  X X decrease key X 33 s 3 t 2 6 7 4 5
  • 28. 28 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X  X X X 33 delmin s 3 t 2 6 7 4 5
  • 29. 29 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X  X X X 33 44 X X 32 s 3 t 2 6 7 4 5
  • 30. 30 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X  X X 44 X delmin X 33X 32 s 3 t 2 6 7 4 5
  • 31. 31 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X  X X 44 X 35X 59 X 24 X 33X 32 s 3 t 2 6 7 4 5
  • 32. 32 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X  X X 44 X 35X 59 X delmin X 33X 32 s 3 t 2 6 7 4 5
  • 33. 33 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X  X X 44 X 35X 59 XX51 X 34 X 33X 32 s 3 t 2 6 7 4 5
  • 34. 34 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X  X X 44 X 35X 59 XX51 X 34 delmin X 33X 32 24 s 3 t 2 6 7 4 5
  • 35. 35 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X  X X 44 X 35X 59 XX51 X 34 24 X50 X45 X 33X 32 s 3 t 2 6 7 4 5
  • 36. 36 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X  X X 44 X 35X 59 XX51 X 34 24 X50 X45 delmin X 33X 32 s 3 t 2 6 7 4 5
  • 37. 37 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X  X X 44 X 35X 59 XX51 X 34 24 X50 X45 X 33X 32 s 3 t 2 6 7 4 5
  • 38. 38 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X  X X 44 X 35X 59 XX51 X 34 X50 X45 delmin X 33X 32 24 s 3 t 2 6 7 4 5
  • 39. 39 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } X  X X 44 X 35X 59 XX51 X 34 X50 X45 X 33X 32 s 3 t 2 6 7 4 5
  • 40. 40 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } X  X X 44 X 35X 59 XX51 X 34 X50 X45 X 33X 32 s 3 t 2 6 7 4 5
  • 41. Shortest Paths wrap-up A.A. 2012/2013Tecniche di programmazione41 Algorithm Problem Efficiency Limitation Floyd-Warshall AP O(𝑉3 ) No negative cycles Bellman–Ford SS O(𝑉 ∙ 𝐸) No negative cycles Repeated- Bellman-Ford AP O(𝑉2 ∙ 𝐸) No negative cycles Dijkstra SS O(𝐸 + 𝑉 ∙ log 𝑉) No negative edges Repeated Dijkstra AP O(𝑉 ∙ 𝐸 + 𝑉2 ∙ log 𝑉) No negative edges
  • 42. JGraphT A.A. 2012/2013Tecniche di programmazione42 public class FloydWarshallShortestPaths<V,E> public class BellmanFordShortestPath<V,E> public class DijkstraShortestPath<V,E> // APSP List<GraphPath<V,E>> getShortestPaths(V v) GraphPath<V,E> getShortestPath(V a, V b) // SSSPs GraphPath<V,E> getPath()
  • 43. Licenza d’uso A.A. 2012/2013Tecniche di programmazione43  Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”  Sei libero:  di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico, rappresentare, eseguire e recitare quest'opera  di modificare quest'opera  Alle seguenti condizioni:  Attribuzione — Devi attribuire la paternità dell'opera agli autori originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.  Non commerciale — Non puoi usare quest'opera per fini commerciali.  Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.  http://creativecommons.org/licenses/by-nc-sa/3.0/