SlideShare a Scribd company logo
1 of 17
*Shortest Path Algorithm*
Presented By
Farhana Shaikh || Kainat Memon
What is a Graph ?
A graph contains nodes and edges and a
directed graph 'G' represents each edge
'e' with a direction.
Shortest-Path Algorithm:
Let G be a directed graph with m nodes n1,
n2, n3 ... nm , Suppose G is weighted, that is
suppose each edge e in G is assigned a
nonnegative number w(e) called the
weight or length of the edge e.
Definitions
Shortest Path Directed Graph
5
7
S
R
1
T
U4
3
2
7
This represents an edge
This represents a node
And this represents the weight of the
edge i.e, the distance between the two
nodes
How we will procedd ?
• To find the shortest path between any two
nodes we will draw two tables.
Distance Table (D) and Sequence Table (S)
• We can also refer these tables as matrix.
• The distance table will hold the distance
between any two nodes.
• The sequence table will hold the name of
the nodes that will help in finding the
shortest path between two nodes.
How we will procedd ?
• We will use iterative approach to solve the
problem. If there are N nodes in a Graph
we will iterate N-1 times.
• For a graph having N nodes, our distance
table(D) and sequence table(S) will have
N rows and N columns.
Manual Steps
Create Distance and Sequence
Table
7 5 ∞ ∞
7 ∞ ∞ 2
∞ 3 ∞ ∞
4 ∞ 1 ∞
D0 R S T U
R
S
T
U
RR RS _ _
SR _ _ SU
_ TS _ _
UR _ UT _
S0 R S T U
R
S
T
U
Distance Table Sequence Table
This is a Cell CTU
Iteration = k = 0
Conditions
• We will fill the cells Cij in distance table Dk
using the following conditions:
• Is dij > dik + dkj { in distance table D k-1 }
• If YES then fill the cell Cij in Dk table with
the value dik+djk of Dk-1 table.
• If NO then fill the cell Cij in Dk table with
the value dij of Dk-1 table.
• That is we will always fill the Cij in Dk with
the smallest value.
7 5 ∞ ∞
7 12 ∞ 2
∞ 3 ∞ ∞
4 9 1 ∞
D1 R S T U
R
S
T
U
S1 R S T U
R
S
T
U
Distance Table Sequence Table
RR RS _ _
SR SR+RS _ SU
_ TS _ _
UR UR+RS UT _
* We have to remove the infinity sign and find the minimum
value.
Iteration = k = 1 , Row = i = 1 , Column = j =1
RR RS _ RS+SU
SR SR+RS _ SU
SR+TS TS _ TS+SU
UR UR+RS UT _
Create Distance and Sequence
Table
7 5 ∞ 7
7 12 ∞ 2
10 3 ∞ 5
4 9 1 ∞
D2 R S T U
R
S
T
U
S2 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 2 , Row = i = 2 , Column = j = 2
Create Distance and Sequence Table
7 5 ∞ 7
7 12 ∞ 2
10 3 ∞ 5
4 4 1 6
D3 R S T U
R
S
T
U
S3 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 3 , Row = i = 3 , Column = j = 3
RR RS _ RS+SU
SR SR+RS _ SU
SR+TS TS _ TS+SU
UR TS+UT UT UT+TU
Create Distance and Sequence Table
7 5 8 7
7 11 3 2
9 3 6 5
4 9 1 6
D4 R S T U
R
S
T
U
S4 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 4 , Row = i = 4 , Column = j = 4
RR RS UT+RU
RS+S
U
SR US+SU UT+SU SU
TU+UR TS UT+TU TS+SU
UR TS+UT UT
UT+T
U
Time to find the distance and the
shortest path
The shortest distance
between R and S = dRS
in D4= 5
The shortest distance
between R and T = dRT
in D4= 8
7 5 8 7
7 11 3 2
9 3 6 5
4 9 1 6
D4 R S T U
R
S
T
U
RS
RT
Algorithm with Complexity
 Shortest Path Algorithm A weighted graph G with M
nodes is maintained in mermory by its weight matrix W.
This algorithm finds a matrix Q such that Q[I, J] is the
length of shortest path from node NI to NJ . INFINITY is a
very large number, and MIN is the minimum value function.
1. Repeat for I, J = 1, 2 .... M: [Initialize Q]
W[I, J] = 0, then: Set Q[I, J] := INFINITY;
Else: Set Q[I, J]:= W[I, J].
[End of Loop]
2. Repeat Steps 3 and 4 for K= 1, 2 ... M: [Update Q]
3. Repeat Step 4 for I = 1, 2 ... M:
4. Repeat for J= 1, 2 ... M:
Set Q[I, J] := MIN(Q[I, J], Q[I, K] + Q[K,J])
[End of loop]
[End of Step 3 loop]
[End of Step 2 loop]
5. Exit.
• Space Complexity O(n2
)
• Running Time Complexity O(n3
)
If any question ?
You may ask.

More Related Content

What's hot (20)

Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Shortest path
Shortest pathShortest path
Shortest path
 
Graph Theory: Trees
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Euler graph
Euler graphEuler graph
Euler graph
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
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
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 

Viewers also liked

ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...
ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...
ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...Waqas Nawaz
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpathCarlostheran
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathMadhumita Tamhane
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 

Viewers also liked (6)

ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...
ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...
ICDE-2015 Shortest Path Traversal Optimization and Analysis for Large Graph C...
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest path
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
IEEE Presentation
IEEE PresentationIEEE Presentation
IEEE Presentation
 

Similar to Shortest path

Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsrLinawati Adiman
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
CPSC125 ch6 sec3
CPSC125 ch6 sec3CPSC125 ch6 sec3
CPSC125 ch6 sec3David Wood
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...ijceronline
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...ijceronline
 
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
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersAnimesh Chaturvedi
 
Applied III Chapter 4(1).pdf
Applied III  Chapter 4(1).pdfApplied III  Chapter 4(1).pdf
Applied III Chapter 4(1).pdfDawitThomas
 
The Fourth Largest Estrada Indices for Trees
The Fourth Largest Estrada Indices for TreesThe Fourth Largest Estrada Indices for Trees
The Fourth Largest Estrada Indices for Treesinventionjournals
 
6.sequences and series Further Mathematics Zimbabwe Zimsec Cambridge
6.sequences and series   Further Mathematics Zimbabwe Zimsec Cambridge6.sequences and series   Further Mathematics Zimbabwe Zimsec Cambridge
6.sequences and series Further Mathematics Zimbabwe Zimsec Cambridgealproelearning
 

Similar to Shortest path (20)

Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Networks dijkstra's algorithm- pgsr
Networks  dijkstra's algorithm- pgsrNetworks  dijkstra's algorithm- pgsr
Networks dijkstra's algorithm- pgsr
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
CPSC125 ch6 sec3
CPSC125 ch6 sec3CPSC125 ch6 sec3
CPSC125 ch6 sec3
 
Cpsc125 ch6sec3
Cpsc125 ch6sec3Cpsc125 ch6sec3
Cpsc125 ch6sec3
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 
D4 trigonometrypdf
D4 trigonometrypdfD4 trigonometrypdf
D4 trigonometrypdf
 
Network Theory
Network TheoryNetwork Theory
Network Theory
 
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
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph
GraphGraph
Graph
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
12_Graph.pptx
12_Graph.pptx12_Graph.pptx
12_Graph.pptx
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Applied III Chapter 4(1).pdf
Applied III  Chapter 4(1).pdfApplied III  Chapter 4(1).pdf
Applied III Chapter 4(1).pdf
 
The Fourth Largest Estrada Indices for Trees
The Fourth Largest Estrada Indices for TreesThe Fourth Largest Estrada Indices for Trees
The Fourth Largest Estrada Indices for Trees
 
6.sequences and series Further Mathematics Zimbabwe Zimsec Cambridge
6.sequences and series   Further Mathematics Zimbabwe Zimsec Cambridge6.sequences and series   Further Mathematics Zimbabwe Zimsec Cambridge
6.sequences and series Further Mathematics Zimbabwe Zimsec Cambridge
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 

Recently uploaded

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 

Recently uploaded (20)

CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 

Shortest path

  • 1. *Shortest Path Algorithm* Presented By Farhana Shaikh || Kainat Memon
  • 2. What is a Graph ? A graph contains nodes and edges and a directed graph 'G' represents each edge 'e' with a direction. Shortest-Path Algorithm: Let G be a directed graph with m nodes n1, n2, n3 ... nm , Suppose G is weighted, that is suppose each edge e in G is assigned a nonnegative number w(e) called the weight or length of the edge e. Definitions
  • 3. Shortest Path Directed Graph 5 7 S R 1 T U4 3 2 7 This represents an edge This represents a node And this represents the weight of the edge i.e, the distance between the two nodes
  • 4. How we will procedd ? • To find the shortest path between any two nodes we will draw two tables. Distance Table (D) and Sequence Table (S) • We can also refer these tables as matrix. • The distance table will hold the distance between any two nodes. • The sequence table will hold the name of the nodes that will help in finding the shortest path between two nodes.
  • 5. How we will procedd ? • We will use iterative approach to solve the problem. If there are N nodes in a Graph we will iterate N-1 times. • For a graph having N nodes, our distance table(D) and sequence table(S) will have N rows and N columns.
  • 7. Create Distance and Sequence Table 7 5 ∞ ∞ 7 ∞ ∞ 2 ∞ 3 ∞ ∞ 4 ∞ 1 ∞ D0 R S T U R S T U RR RS _ _ SR _ _ SU _ TS _ _ UR _ UT _ S0 R S T U R S T U Distance Table Sequence Table This is a Cell CTU Iteration = k = 0
  • 8. Conditions • We will fill the cells Cij in distance table Dk using the following conditions: • Is dij > dik + dkj { in distance table D k-1 } • If YES then fill the cell Cij in Dk table with the value dik+djk of Dk-1 table. • If NO then fill the cell Cij in Dk table with the value dij of Dk-1 table. • That is we will always fill the Cij in Dk with the smallest value.
  • 9. 7 5 ∞ ∞ 7 12 ∞ 2 ∞ 3 ∞ ∞ 4 9 1 ∞ D1 R S T U R S T U S1 R S T U R S T U Distance Table Sequence Table RR RS _ _ SR SR+RS _ SU _ TS _ _ UR UR+RS UT _ * We have to remove the infinity sign and find the minimum value. Iteration = k = 1 , Row = i = 1 , Column = j =1
  • 10. RR RS _ RS+SU SR SR+RS _ SU SR+TS TS _ TS+SU UR UR+RS UT _ Create Distance and Sequence Table 7 5 ∞ 7 7 12 ∞ 2 10 3 ∞ 5 4 9 1 ∞ D2 R S T U R S T U S2 R S T U R S T U Distance Table Sequence Table Iteration = k = 2 , Row = i = 2 , Column = j = 2
  • 11. Create Distance and Sequence Table 7 5 ∞ 7 7 12 ∞ 2 10 3 ∞ 5 4 4 1 6 D3 R S T U R S T U S3 R S T U R S T U Distance Table Sequence Table Iteration = k = 3 , Row = i = 3 , Column = j = 3 RR RS _ RS+SU SR SR+RS _ SU SR+TS TS _ TS+SU UR TS+UT UT UT+TU
  • 12. Create Distance and Sequence Table 7 5 8 7 7 11 3 2 9 3 6 5 4 9 1 6 D4 R S T U R S T U S4 R S T U R S T U Distance Table Sequence Table Iteration = k = 4 , Row = i = 4 , Column = j = 4 RR RS UT+RU RS+S U SR US+SU UT+SU SU TU+UR TS UT+TU TS+SU UR TS+UT UT UT+T U
  • 13. Time to find the distance and the shortest path The shortest distance between R and S = dRS in D4= 5 The shortest distance between R and T = dRT in D4= 8 7 5 8 7 7 11 3 2 9 3 6 5 4 9 1 6 D4 R S T U R S T U RS RT
  • 15.  Shortest Path Algorithm A weighted graph G with M nodes is maintained in mermory by its weight matrix W. This algorithm finds a matrix Q such that Q[I, J] is the length of shortest path from node NI to NJ . INFINITY is a very large number, and MIN is the minimum value function. 1. Repeat for I, J = 1, 2 .... M: [Initialize Q] W[I, J] = 0, then: Set Q[I, J] := INFINITY; Else: Set Q[I, J]:= W[I, J]. [End of Loop] 2. Repeat Steps 3 and 4 for K= 1, 2 ... M: [Update Q] 3. Repeat Step 4 for I = 1, 2 ... M: 4. Repeat for J= 1, 2 ... M: Set Q[I, J] := MIN(Q[I, J], Q[I, K] + Q[K,J]) [End of loop] [End of Step 3 loop] [End of Step 2 loop] 5. Exit.
  • 16. • Space Complexity O(n2 ) • Running Time Complexity O(n3 )
  • 17. If any question ? You may ask.