SlideShare uma empresa Scribd logo
1 de 41
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

Mais conteúdo relacionado

Mais procurados

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptshanthishyam
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithmRezwan Siam
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
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 algorithmGaurav Kolekar
 

Mais procurados (20)

Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
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
 

Semelhante a SINGLE-SOURCE SHORTEST PATHS

Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
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
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqmontaser185
 
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-pairsBenjamin Sach
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras algdouglaslyon
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
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 AlgorithmFulvio Corno
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
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
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emadKazi Emad
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Bellman Ford Algorithm
Bellman Ford AlgorithmBellman Ford Algorithm
Bellman Ford Algorithmiqra593488
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 

Semelhante a SINGLE-SOURCE SHORTEST PATHS (20)

Shortest path
Shortest pathShortest path
Shortest path
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Graph 3
Graph 3Graph 3
Graph 3
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford 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
 
lecture 21
lecture 21lecture 21
lecture 21
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
 
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
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
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
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
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
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
Bellman Ford Algorithm
Bellman Ford AlgorithmBellman Ford Algorithm
Bellman Ford Algorithm
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 

Mais de Md. Shafiuzzaman Hira (20)

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

Último

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Último (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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