SlideShare a Scribd company logo
1 of 23
Download to read offline
Shortest Path
Algorithm
 DIJIKSTRA ALGORITHM
 BELLMEN ALGORITHM
 FLOYED WARSHALL ALGORITHM
Shortest Path Algorithm
 An algorithm that is designed essentially to
find a path of minimum length between two
specified vertices of a connected weighted
graph.
Single source Shortest path algorithm
o It is defined as
Cost of shortest path from a source
vertex u to a destination v.
s a
b c
Dijkstra’s Algorithm
Dijkstra’s algorithm is a greedy algorithm for
solving single source shortest path problem
that provide us with the shortest path from one
particular source node to all other nodes in the
given graph .
Rules of Dijkstra’s algorithm
 It can be applied on both directed and undirected
graph.
 It is applied on weighted graph.
 For the algorithm to be applicable the graph must
be connected.
a 3 b
4 5 1
c d
4
Cont…
 Dijkstra’s algorithm does not work for graphs with
negative weight edges. For graphs with negative
weight edges, Bellman_ford algorithm can be used.
 In Dijkstra’s algorithm always assign source vertex to
zero distance.
 Assign every other node a tentative distance.
Dijkstra’s Algorithm
Dijkstra’s(G,W,S)
INITIALIZE-SINGLE-SOURCE(G,s)
1. for each vertex v ∈ 𝐺. 𝑉
2. v.d = ∞
3. v.𝜋 = 𝑁𝐼𝐿
4. s.d = 0
5. S = ∅
6. Q = G.V
7. while (Q≠ ∅)
8. u = dequeue_min(Q)
9. S = S ∪ 𝑢
10. for each vertex v∈ 𝐴𝑑𝑗 𝑢
11. if( v.d > u.d+ w(u,v) )
12. v.d = u.d + w (u,v) // update distance of v
13. v. 𝜋 = u
14. return v.d
BellMan-Algorithm
 Bellman–Ford 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 digraph. ... Negative edge
weights are found in various applications of graphs,
hence the usefulness of this algorithm.
Cont..
 It is similar to Dijkstra's algorithm but it can work
with graphs in which edges can have negative
weights.
 Negative weight edges can create negative weight
cycles i.e. a cycle which will reduce the total path
distance by coming back to the same point.
Cont..
 Shortest path algorithms
like Dijkstra's Algorithm that
aren't able to detect such a
cycle can give an incorrect
result because they can go
through a negative weight
cycle and reduce the path
length.
A B D E
2
1 3
2 -4
C
Uses of Algorithm
 This algorithm can be used on both weighted and
unweighted graphs.
 Like Dijkstra's shortest path algorithm, the Bellman-Ford
algorithm is guaranteed to find the shortest path in a graph.
 Though it is slower than Dijkstra's algorithm, Bellman-Ford is
capable of handling graphs that contain negative edge
weights, so it is more versatile.
ALGORITHM
1. for v in V:
2. v.distance = infinity
3. v.p = None
4. source.distance = 0
5. for i from 1 to |V| - 1:
6. for (u, v) in E:
7. relax(u, v)
Explanation
 The first for loop sets the distance to each vertex in
the graph to infinity. This is later changed for the
source vertex to equal zero. Also in that first for loop,
the p value for each vertex is set to nothing. This
value is a pointer to a predecessor vertex so that we
can create a path later.
 The next for loop simply goes through each edge (u,
v) in E and relaxes it. This process is done |V| - 1 times.
Relaxation Equation
 Relaxation is the most important step in Bellman-
Ford. It is what increases the accuracy of the
distance to any given vertex. Relaxation works by
continuously shortening the calculated distance
between vertices comparing that distance with
other known distances.
Example
 Take the baseball example from earlier. Let's say I think the
distance to the baseball stadium is 20 miles. However, I know
that the distance to the corner right before the stadium is 10
miles, and I know that from the corner to the stadium, the
distance is 1 mile. Clearly, the distance from me to the
stadium is at most 11 miles. So, I can update my belief to
reflect that. That is one cycle of relaxation, and it's done over
and over until the shortest paths are found
Relax Equation
1. relax(u, v):
2. if v.distance > u.distance + weight(u, v):
v.distance = u.distance + weight(u, v)
3. v.p = u
Negative Cycle
 Detecting Negative Cycles
 A very short and simple addition to the Bellman-Ford
algorithm can allow it to detect negative cycles, something
that is very important because it disallows shortest-path
finding altogether. After the Bellman-Ford algorithm
shown above has been run, one more short loop is required
to check for negative weight cycles.
 This psuedo-code is written as a high level descrpition of the
algorithm, not an implementation.
Cont..
1. for v in V:
2. v.distance = infinity
3. v.p = None
4. source.distance = 0
5. for i from 1 to |V| - 1:
6. for (u, v) in E:
7. relax(u, v) for (u, v) in E:
8. if v.distance > u.distance + weight(u, v):
9. print "A negative weight cycle exists"
Floyed Warshall ALgorithm
 Floyd–Warshall algorithm is an algorithm for
finding shortest paths in a weighted graph
 The Graph may be weighted with positive or
negative edge weights .
 The Graph may be directed or undirected.
Cont..
 If a node has not a direct path or
link to any other node then the
distance between these nodes
are infinite.
 If only one path between the two
nodes or vertices then it said to
be the shortest distance.
 The distance of a vertex to itself is
0.
Vo 10 V3
5 1
3
V1 V2
Example
0 1 2 3
0 0 5 inf 10
1 Inf 0 3 Inf
2 Inf Inf 0 1
3 inf inf Inf 0
Vo V3
V1 V2
5
10
1
3
Uses Of Shortest Path Algorithm
 Weighted graph
 Distance function or array
 Priority Queue
 Relaxation
Applications of Shortest Path Algorithm
 It is used in Google Map.
 It is used in finding Shortest Path.
 It is used in geographical Maps.
 To find locations of Map which refers to vertices of
graph.
 Distance between the location refers to edges.
 It is used in IP routing to find Open shortest Path First.
 It is used in the telephone network.

More Related Content

Similar to shortestpathalgorithm-180109112405 (1).pdf

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path AlgorithmAnish Ansari
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfzefergaming
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqmontaser185
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emadKazi Emad
 
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
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfiftakhar8
 

Similar to shortestpathalgorithm-180109112405 (1).pdf (20)

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path Algorithm
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 
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
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Floyd aaaaaa
Floyd aaaaaaFloyd aaaaaa
Floyd aaaaaa
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
06_AJMS_334_21.pdf
06_AJMS_334_21.pdf06_AJMS_334_21.pdf
06_AJMS_334_21.pdf
 
path
pathpath
path
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
 
Algo
Algo Algo
Algo
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 

Recently uploaded

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

shortestpathalgorithm-180109112405 (1).pdf

  • 1. Shortest Path Algorithm  DIJIKSTRA ALGORITHM  BELLMEN ALGORITHM  FLOYED WARSHALL ALGORITHM
  • 2. Shortest Path Algorithm  An algorithm that is designed essentially to find a path of minimum length between two specified vertices of a connected weighted graph.
  • 3. Single source Shortest path algorithm o It is defined as Cost of shortest path from a source vertex u to a destination v. s a b c
  • 4. Dijkstra’s Algorithm Dijkstra’s algorithm is a greedy algorithm for solving single source shortest path problem that provide us with the shortest path from one particular source node to all other nodes in the given graph .
  • 5. Rules of Dijkstra’s algorithm  It can be applied on both directed and undirected graph.  It is applied on weighted graph.  For the algorithm to be applicable the graph must be connected. a 3 b 4 5 1 c d 4
  • 6. Cont…  Dijkstra’s algorithm does not work for graphs with negative weight edges. For graphs with negative weight edges, Bellman_ford algorithm can be used.  In Dijkstra’s algorithm always assign source vertex to zero distance.  Assign every other node a tentative distance.
  • 7. Dijkstra’s Algorithm Dijkstra’s(G,W,S) INITIALIZE-SINGLE-SOURCE(G,s) 1. for each vertex v ∈ 𝐺. 𝑉 2. v.d = ∞ 3. v.𝜋 = 𝑁𝐼𝐿 4. s.d = 0 5. S = ∅ 6. Q = G.V 7. while (Q≠ ∅) 8. u = dequeue_min(Q) 9. S = S ∪ 𝑢 10. for each vertex v∈ 𝐴𝑑𝑗 𝑢 11. if( v.d > u.d+ w(u,v) ) 12. v.d = u.d + w (u,v) // update distance of v 13. v. 𝜋 = u 14. return v.d
  • 8. BellMan-Algorithm  Bellman–Ford 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 digraph. ... Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm.
  • 9. Cont..  It is similar to Dijkstra's algorithm but it can work with graphs in which edges can have negative weights.  Negative weight edges can create negative weight cycles i.e. a cycle which will reduce the total path distance by coming back to the same point.
  • 10. Cont..  Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a cycle can give an incorrect result because they can go through a negative weight cycle and reduce the path length. A B D E 2 1 3 2 -4 C
  • 11. Uses of Algorithm  This algorithm can be used on both weighted and unweighted graphs.  Like Dijkstra's shortest path algorithm, the Bellman-Ford algorithm is guaranteed to find the shortest path in a graph.  Though it is slower than Dijkstra's algorithm, Bellman-Ford is capable of handling graphs that contain negative edge weights, so it is more versatile.
  • 12. ALGORITHM 1. for v in V: 2. v.distance = infinity 3. v.p = None 4. source.distance = 0 5. for i from 1 to |V| - 1: 6. for (u, v) in E: 7. relax(u, v)
  • 13. Explanation  The first for loop sets the distance to each vertex in the graph to infinity. This is later changed for the source vertex to equal zero. Also in that first for loop, the p value for each vertex is set to nothing. This value is a pointer to a predecessor vertex so that we can create a path later.  The next for loop simply goes through each edge (u, v) in E and relaxes it. This process is done |V| - 1 times.
  • 14. Relaxation Equation  Relaxation is the most important step in Bellman- Ford. It is what increases the accuracy of the distance to any given vertex. Relaxation works by continuously shortening the calculated distance between vertices comparing that distance with other known distances.
  • 15. Example  Take the baseball example from earlier. Let's say I think the distance to the baseball stadium is 20 miles. However, I know that the distance to the corner right before the stadium is 10 miles, and I know that from the corner to the stadium, the distance is 1 mile. Clearly, the distance from me to the stadium is at most 11 miles. So, I can update my belief to reflect that. That is one cycle of relaxation, and it's done over and over until the shortest paths are found
  • 16. Relax Equation 1. relax(u, v): 2. if v.distance > u.distance + weight(u, v): v.distance = u.distance + weight(u, v) 3. v.p = u
  • 17. Negative Cycle  Detecting Negative Cycles  A very short and simple addition to the Bellman-Ford algorithm can allow it to detect negative cycles, something that is very important because it disallows shortest-path finding altogether. After the Bellman-Ford algorithm shown above has been run, one more short loop is required to check for negative weight cycles.  This psuedo-code is written as a high level descrpition of the algorithm, not an implementation.
  • 18. Cont.. 1. for v in V: 2. v.distance = infinity 3. v.p = None 4. source.distance = 0 5. for i from 1 to |V| - 1: 6. for (u, v) in E: 7. relax(u, v) for (u, v) in E: 8. if v.distance > u.distance + weight(u, v): 9. print "A negative weight cycle exists"
  • 19. Floyed Warshall ALgorithm  Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph  The Graph may be weighted with positive or negative edge weights .  The Graph may be directed or undirected.
  • 20. Cont..  If a node has not a direct path or link to any other node then the distance between these nodes are infinite.  If only one path between the two nodes or vertices then it said to be the shortest distance.  The distance of a vertex to itself is 0. Vo 10 V3 5 1 3 V1 V2
  • 21. Example 0 1 2 3 0 0 5 inf 10 1 Inf 0 3 Inf 2 Inf Inf 0 1 3 inf inf Inf 0 Vo V3 V1 V2 5 10 1 3
  • 22. Uses Of Shortest Path Algorithm  Weighted graph  Distance function or array  Priority Queue  Relaxation
  • 23. Applications of Shortest Path Algorithm  It is used in Google Map.  It is used in finding Shortest Path.  It is used in geographical Maps.  To find locations of Map which refers to vertices of graph.  Distance between the location refers to edges.  It is used in IP routing to find Open shortest Path First.  It is used in the telephone network.