SlideShare a Scribd company logo
Prepared by
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman 2
Shortest Path
Shortest Path = Path of minimum weight
δ(u,v)= min{ω(p) : u v}; if there is a path from u to v,
 otherwise.
p
Md. Shafiuzzaman 3
Shortest-Path Variants
• Shortest-Path problems
– Single-source shortest-paths problem: Find the shortest path from s
to each vertex v. (e.g. BFS)
– Single-destination shortest-paths problem: Find a shortest path to a
given destination vertex t from each vertex v.
– Single-pair shortest-path problem: Find a shortest path from u to v
for given vertices u and v.
– All-pairs shortest-paths problem: Find a shortest path from u to v
for every pair of vertices u and v.
Md. Shafiuzzaman 4
Negative-weight edges
• No problem, as long as no negative-weight cycles are
reachable from the source
• Otherwise, we can just keep going around it, and
get w(s, v) = −∞ for all v on the cycle.
Md. Shafiuzzaman 5
Relaxation
• Maintain d[v] for each v  V
• d[v] is called shortest-path weight estimate
and it is upper bound on δ(s,v)
INIT(G, s)
for each v  V do
d[v] ← ∞
π[v] ← NIL
d[s] ← 0
Md. Shafiuzzaman 6
Relaxation
RELAX(u, v)
if d[v] > d[u]+w(u,v) then
d[v] ← d[u]+w(u,v)
π[v] ← u
5
u v
vu
2
2
9
5 7
Relax(u,v)
5
u v
vu
2
2
6
5 6
Md. Shafiuzzaman 7
Properties of Relaxation
Algorithms differ in
 how many times they relax each edge, and
 the order in which they relax edges
Question: How many times each edge is relaxed in BFS?
Answer: Only once!
Md. Shafiuzzaman 8
Single-Source Shortest Paths in DAGs
• Shortest paths are always well-defined in dags
 no cycles => no negative-weight cycles even if
there are negative-weight edges
• Idea: If we were lucky
 To process vertices on each shortest path from left
to right, we would be done in 1 pass
Md. Shafiuzzaman 9
Single-Source Shortest Paths in DAGs
In a dag:
• Every path is a subsequence of the topologically sorted
vertex order
• If we do topological sort and process vertices in that order
• We will process each path in forward order
 Never relax edges out of a vertex until have processed
all edges into the vertex
• Thus, just 1 pass is sufficient
Md. Shafiuzzaman 10
Example
Md. Shafiuzzaman 11
Example
Md. Shafiuzzaman 12
Example
Md. Shafiuzzaman 13
Example
Md. Shafiuzzaman 14
Example
Md. Shafiuzzaman 15
Example
Md. Shafiuzzaman 16
Single-Source Shortest Paths in DAGs
DAG-SHORTEST PATHS(G, s)
TOPOLOGICALLY-SORT the vertices of G
INIT(G, s)
for each vertex u taken in topologically sorted order do
for each v  Adj[u] do
RELAX(u, v)
Md. Shafiuzzaman 17
Single-Source Shortest Paths in DAGs
Runs in linear time: Θ(V+E)
 topological sort: Θ(V+E)
 initialization: Θ(V+E)
 for-loop: Θ(V+E)
each vertex processed exactly once
=> each edge processed exactly once: Θ(V+E)
Md. Shafiuzzaman 18
• Non-negative edge weight
• Like BFS: If all edge weights are equal, then use BFS,
otherwise use this algorithm
• Use Q = priority queue keyed on d[v] values
(note: BFS uses FIFO)
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 19
Example
3
 
 
0s
u v
yx
10
5
1
2 9
4 6
7
2
Md. Shafiuzzaman 20
Example
2
10 
5 
0s
u v
yx
10
5
1
3
9
4 6
7
2
Md. Shafiuzzaman 21
Example
2
8 14
5 7
0s
yx
10
5
1
3 9
4 6
7
2
u v
Md. Shafiuzzaman 22
Example
10
8 13
5 7
0s
u v
yx
5
1
2 3 9
4 6
7
2
Md. Shafiuzzaman 23
Example
8
u v
9
5 7
0
yx
10
5
1
2 3 9
4 6
7
2
s
Md. Shafiuzzaman 24
Example
u
5
8 9
5 7
0
v
yx
10
1
2 3 9
4 6
7
2
s
Md. Shafiuzzaman 25
DIJKSTRA(G, s)
INIT(G, s)
S←Ø > set of discovered nodes
Q←V[G]
while Q ≠Ø do
u←EXTRACT-MIN(Q)
S←S U {u}
for each v  Adj[u] do
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v])
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 26
Observe :
• Each vertex is extracted from Q and inserted into S
exactly once
• Each edge is relaxed exactly once
• S = set of vertices whose final shortest paths have already
been determined
 i.e. , S = {v  V: d[v] = δ(s, v) ≠ ∞ }
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 27
• Similar to BFS algorithm: S corresponds to the set of
black vertices in BFS which have their correct breadth-
first distances already computed
• Greedy strategy: Always chooses the closest(lightest)
vertex in Q = V-S to insert into S
• Relaxation may reset d[v] values thus updating
Q = DECREASE-KEY operation.
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 28
• Similar to Prim’s MST algorithm: Both algorithms
use a priority queue to find the lightest vertex outside a
given set S
• Insert this vertex into the set
• Adjust weights of remaining adjacent vertices outside the
set accordingly
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 29
Example: Run algorithm on a sample graph
43
210
5 1
s0
∞ 5
∞ 10 9 8
∞ 6
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 30
• Look at different Q implementation, as did for Prim’s
algorithm
• Initialization (INIT) : Θ(V) time
• While-loop:
• EXTRACT-MIN executed |V| times
• DECREASE-KEY executed |E| times
• Time T = |V| x TE-MIN +|E| x TD-KEY
Running Time Analysis of
Dijkstra’s Algorithm
Md. Shafiuzzaman 31
Bellman-Ford Algorithm for Single
Source Shortest Paths
• More general than Dijkstra’s algorithm:
 Edge-weights can be negative
• Detects the existence of negative-weight cycle(s)
reachable from s.
Md. Shafiuzzaman 32
Bellman-Ford Algorithm
Example
5
 
 
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
Md. Shafiuzzaman 33
Bellman-Ford Algorithm
Example
6 
7 
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
Md. Shafiuzzaman 34
Bellman-Ford Algorithm
Example
6 4
7 2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
Md. Shafiuzzaman 35
Bellman-Ford Algorithm
Example
2 4
7 2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
Md. Shafiuzzaman 36
Bellman-Ford Algorithm
Example
2 4
7 -2
0s
zy
6
7
8
-3
7
2
9
-2
xt
-4
5
Md. Shafiuzzaman 37
BELMAN-FORD( G, s )
INIT( G, s )
for i ←1 to |V|-1 do
for each edge (u, v)  E do
RELAX( u, v )
for each edge ( u, v )  E do
if d[v] > d[u]+w(u,v) then
return FALSE > neg-weight cycle
return TRUE
Bellman-Ford Algorithm for Single
Source Shortest Paths
Md. Shafiuzzaman 38
Observe:
• First nested for-loop performs |V|-1 relaxation passes;
relax every edge at each pass
• Last for-loop checks the existence of a negative-weight
cycle reachable from s
Bellman-Ford Algorithm for Single
Source Shortest Paths
Md. Shafiuzzaman 39
• Running time = O(VxE)
Constants are good; it’s simple, short code(very
practical)
• Example: Run algorithm on a sample graph with
no negative weight cycles.
Bellman-Ford Algorithm for Single
Source Shortest Paths
Md. Shafiuzzaman 40
• Converges in just 2 relaxation passes
• Values you get on each pass & how early converges
depend on edge process order
• d value of a vertex may be updated more than once in a
pass
Bellman-Ford Algorithm for Single
Source Shortest Paths
Assignment
• Implement Dijkstra’s Algorithm and
Bellman–Ford Algorithm
• Input: Take number of vertices, adjacency
matrix and starting node as input
• Output: Print distance from each node and
the path
Md. Shafiuzzaman 41
Ad

More Related Content

What's hot (20)

Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
Subid Biswas
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
ami_01
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
Bhavik Vashi
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
Arafat Hossan
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptx
ArbabMaalik
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
Rajandeep Gill
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
Narendra Singh Patel
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
MdSajjadulislamBappi
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
Dr Sandeep Kumar Poonia
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 

Similar to SINGLE-SOURCE SHORTEST PATHS (20)

Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
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
Roshan Tailor
 
Graph 3
Graph 3Graph 3
Graph 3
International Islamic University
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
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
DKTaxation
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
montaser185
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
douglaslyon
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
shanthishyam
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Fulvio Corno
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
lec03 lec03 lec03 lec03 lec03 lec03 lec03
lec03 lec03 lec03 lec03 lec03 lec03 lec03lec03 lec03 lec03 lec03 lec03 lec03 lec03
lec03 lec03 lec03 lec03 lec03 lec03 lec03
tasefi1870
 
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
pooja saini
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
Kazi Emad
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
Hanif Durad
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
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
Roshan Tailor
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
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
DKTaxation
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
montaser185
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
douglaslyon
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
shanthishyam
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Fulvio Corno
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
lec03 lec03 lec03 lec03 lec03 lec03 lec03
lec03 lec03 lec03 lec03 lec03 lec03 lec03lec03 lec03 lec03 lec03 lec03 lec03 lec03
lec03 lec03 lec03 lec03 lec03 lec03 lec03
tasefi1870
 
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
pooja saini
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
Kazi Emad
 
Ad

More from Md. Shafiuzzaman Hira (20)

Introduction to Web development
Introduction to Web developmentIntroduction to Web development
Introduction to Web development
Md. Shafiuzzaman Hira
 
Software measurement and estimation
Software measurement and estimationSoftware measurement and estimation
Software measurement and estimation
Md. Shafiuzzaman Hira
 
Why do we test software?
Why do we test software?Why do we test software?
Why do we test software?
Md. Shafiuzzaman Hira
 
Software Requirements engineering
Software Requirements engineeringSoftware Requirements engineering
Software Requirements engineering
Md. Shafiuzzaman Hira
 
Software architectural patterns
Software architectural patternsSoftware architectural patterns
Software architectural patterns
Md. Shafiuzzaman Hira
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
Md. Shafiuzzaman Hira
 
Class diagram
Class diagramClass diagram
Class diagram
Md. Shafiuzzaman Hira
 
State diagram
State diagramState diagram
State diagram
Md. Shafiuzzaman Hira
 
Use case Modeling
Use case ModelingUse case Modeling
Use case Modeling
Md. Shafiuzzaman Hira
 
User stories
User storiesUser stories
User stories
Md. Shafiuzzaman Hira
 
Dev ops
Dev opsDev ops
Dev ops
Md. Shafiuzzaman Hira
 
Agile Methodology
Agile MethodologyAgile Methodology
Agile Methodology
Md. Shafiuzzaman Hira
 
Software Process Model
Software Process ModelSoftware Process Model
Software Process Model
Md. Shafiuzzaman Hira
 
Introduction to Software Engineering Course
Introduction to Software Engineering CourseIntroduction to Software Engineering Course
Introduction to Software Engineering Course
Md. Shafiuzzaman Hira
 
C files
C filesC files
C files
Md. Shafiuzzaman Hira
 
C pointers
C pointersC pointers
C pointers
Md. Shafiuzzaman Hira
 
C structures
C structuresC structures
C structures
Md. Shafiuzzaman Hira
 
How to Create Python scripts
How to Create Python scriptsHow to Create Python scripts
How to Create Python scripts
Md. Shafiuzzaman Hira
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
Md. Shafiuzzaman Hira
 
Password locker project
Password locker project Password locker project
Password locker project
Md. Shafiuzzaman Hira
 
Ad

Recently uploaded (20)

AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 

SINGLE-SOURCE SHORTEST PATHS

  • 1. Prepared by Md. Shafiuzzaman Lecturer, Dept. of CSE, JUST SINGLE-SOURCE SHORTEST PATHS
  • 2. Md. Shafiuzzaman 2 Shortest Path Shortest Path = Path of minimum weight δ(u,v)= min{ω(p) : u v}; if there is a path from u to v,  otherwise. p
  • 3. Md. Shafiuzzaman 3 Shortest-Path Variants • Shortest-Path problems – Single-source shortest-paths problem: Find the shortest path from s to each vertex v. (e.g. BFS) – Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v. – Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. – All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v.
  • 4. Md. Shafiuzzaman 4 Negative-weight edges • No problem, as long as no negative-weight cycles are reachable from the source • Otherwise, we can just keep going around it, and get w(s, v) = −∞ for all v on the cycle.
  • 5. Md. Shafiuzzaman 5 Relaxation • Maintain d[v] for each v  V • d[v] is called shortest-path weight estimate and it is upper bound on δ(s,v) INIT(G, s) for each v  V do d[v] ← ∞ π[v] ← NIL d[s] ← 0
  • 6. Md. Shafiuzzaman 6 Relaxation RELAX(u, v) if d[v] > d[u]+w(u,v) then d[v] ← d[u]+w(u,v) π[v] ← u 5 u v vu 2 2 9 5 7 Relax(u,v) 5 u v vu 2 2 6 5 6
  • 7. Md. Shafiuzzaman 7 Properties of Relaxation Algorithms differ in  how many times they relax each edge, and  the order in which they relax edges Question: How many times each edge is relaxed in BFS? Answer: Only once!
  • 8. Md. Shafiuzzaman 8 Single-Source Shortest Paths in DAGs • Shortest paths are always well-defined in dags  no cycles => no negative-weight cycles even if there are negative-weight edges • Idea: If we were lucky  To process vertices on each shortest path from left to right, we would be done in 1 pass
  • 9. Md. Shafiuzzaman 9 Single-Source Shortest Paths in DAGs In a dag: • Every path is a subsequence of the topologically sorted vertex order • If we do topological sort and process vertices in that order • We will process each path in forward order  Never relax edges out of a vertex until have processed all edges into the vertex • Thus, just 1 pass is sufficient
  • 16. Md. Shafiuzzaman 16 Single-Source Shortest Paths in DAGs DAG-SHORTEST PATHS(G, s) TOPOLOGICALLY-SORT the vertices of G INIT(G, s) for each vertex u taken in topologically sorted order do for each v  Adj[u] do RELAX(u, v)
  • 17. Md. Shafiuzzaman 17 Single-Source Shortest Paths in DAGs Runs in linear time: Θ(V+E)  topological sort: Θ(V+E)  initialization: Θ(V+E)  for-loop: Θ(V+E) each vertex processed exactly once => each edge processed exactly once: Θ(V+E)
  • 18. Md. Shafiuzzaman 18 • Non-negative edge weight • Like BFS: If all edge weights are equal, then use BFS, otherwise use this algorithm • Use Q = priority queue keyed on d[v] values (note: BFS uses FIFO) Dijkstra’s Algorithm For Shortest Paths
  • 19. Md. Shafiuzzaman 19 Example 3     0s u v yx 10 5 1 2 9 4 6 7 2
  • 20. Md. Shafiuzzaman 20 Example 2 10  5  0s u v yx 10 5 1 3 9 4 6 7 2
  • 21. Md. Shafiuzzaman 21 Example 2 8 14 5 7 0s yx 10 5 1 3 9 4 6 7 2 u v
  • 22. Md. Shafiuzzaman 22 Example 10 8 13 5 7 0s u v yx 5 1 2 3 9 4 6 7 2
  • 23. Md. Shafiuzzaman 23 Example 8 u v 9 5 7 0 yx 10 5 1 2 3 9 4 6 7 2 s
  • 24. Md. Shafiuzzaman 24 Example u 5 8 9 5 7 0 v yx 10 1 2 3 9 4 6 7 2 s
  • 25. Md. Shafiuzzaman 25 DIJKSTRA(G, s) INIT(G, s) S←Ø > set of discovered nodes Q←V[G] while Q ≠Ø do u←EXTRACT-MIN(Q) S←S U {u} for each v  Adj[u] do RELAX(u, v) > May cause > DECREASE-KEY(Q, v, d[v]) Dijkstra’s Algorithm For Shortest Paths
  • 26. Md. Shafiuzzaman 26 Observe : • Each vertex is extracted from Q and inserted into S exactly once • Each edge is relaxed exactly once • S = set of vertices whose final shortest paths have already been determined  i.e. , S = {v  V: d[v] = δ(s, v) ≠ ∞ } Dijkstra’s Algorithm For Shortest Paths
  • 27. Md. Shafiuzzaman 27 • Similar to BFS algorithm: S corresponds to the set of black vertices in BFS which have their correct breadth- first distances already computed • Greedy strategy: Always chooses the closest(lightest) vertex in Q = V-S to insert into S • Relaxation may reset d[v] values thus updating Q = DECREASE-KEY operation. Dijkstra’s Algorithm For Shortest Paths
  • 28. Md. Shafiuzzaman 28 • Similar to Prim’s MST algorithm: Both algorithms use a priority queue to find the lightest vertex outside a given set S • Insert this vertex into the set • Adjust weights of remaining adjacent vertices outside the set accordingly Dijkstra’s Algorithm For Shortest Paths
  • 29. Md. Shafiuzzaman 29 Example: Run algorithm on a sample graph 43 210 5 1 s0 ∞ 5 ∞ 10 9 8 ∞ 6 Dijkstra’s Algorithm For Shortest Paths
  • 30. Md. Shafiuzzaman 30 • Look at different Q implementation, as did for Prim’s algorithm • Initialization (INIT) : Θ(V) time • While-loop: • EXTRACT-MIN executed |V| times • DECREASE-KEY executed |E| times • Time T = |V| x TE-MIN +|E| x TD-KEY Running Time Analysis of Dijkstra’s Algorithm
  • 31. Md. Shafiuzzaman 31 Bellman-Ford Algorithm for Single Source Shortest Paths • More general than Dijkstra’s algorithm:  Edge-weights can be negative • Detects the existence of negative-weight cycle(s) reachable from s.
  • 32. Md. Shafiuzzaman 32 Bellman-Ford Algorithm Example 5     0s zy 6 7 8 -3 7 2 9 -2 xt -4
  • 33. Md. Shafiuzzaman 33 Bellman-Ford Algorithm Example 6  7  0s zy 6 7 8 -3 7 2 9 -2 xt -4 5
  • 34. Md. Shafiuzzaman 34 Bellman-Ford Algorithm Example 6 4 7 2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5
  • 35. Md. Shafiuzzaman 35 Bellman-Ford Algorithm Example 2 4 7 2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5
  • 36. Md. Shafiuzzaman 36 Bellman-Ford Algorithm Example 2 4 7 -2 0s zy 6 7 8 -3 7 2 9 -2 xt -4 5
  • 37. Md. Shafiuzzaman 37 BELMAN-FORD( G, s ) INIT( G, s ) for i ←1 to |V|-1 do for each edge (u, v)  E do RELAX( u, v ) for each edge ( u, v )  E do if d[v] > d[u]+w(u,v) then return FALSE > neg-weight cycle return TRUE Bellman-Ford Algorithm for Single Source Shortest Paths
  • 38. Md. Shafiuzzaman 38 Observe: • First nested for-loop performs |V|-1 relaxation passes; relax every edge at each pass • Last for-loop checks the existence of a negative-weight cycle reachable from s Bellman-Ford Algorithm for Single Source Shortest Paths
  • 39. Md. Shafiuzzaman 39 • Running time = O(VxE) Constants are good; it’s simple, short code(very practical) • Example: Run algorithm on a sample graph with no negative weight cycles. Bellman-Ford Algorithm for Single Source Shortest Paths
  • 40. Md. Shafiuzzaman 40 • Converges in just 2 relaxation passes • Values you get on each pass & how early converges depend on edge process order • d value of a vertex may be updated more than once in a pass Bellman-Ford Algorithm for Single Source Shortest Paths
  • 41. Assignment • Implement Dijkstra’s Algorithm and Bellman–Ford Algorithm • Input: Take number of vertices, adjacency matrix and starting node as input • Output: Print distance from each node and the path Md. Shafiuzzaman 41
  翻译: