SlideShare a Scribd company logo
1 of 27
Lecture 10: Dijkstra’s Shortest Path
Algorithm
CLRS 24.3
Outline of this Lecture

Recalling the BFS solution of the shortest path
problem for unweighted (di)graphs.

The shortest path problem for weighted
digraphs.
Dijkstra’s algorithm.
Given for digraphs but easily modified to work
on undirected graphs.

1
Recall: Shortest Path Problem for
Graphs
Let

be a (di)graph.

The shortest path between two vertices is a path
with the shortest length (least number of
edges). Call this the link-distance.
Breadth-first-search is an algorithm for finding
shortest (link-distance) paths from a single source
ver- tex to all other vertices.
BFS processes vertices in increasing order of
their
distance from the root vertex.
BFS has running time

.

2
Shortest Path Problem for Weighted
Graphs
Let
function
weights. If

be a weighted digraph, with weight
mapping edges to real-valued
, we write
for
.

The length of a path
is the
sum of the weights of its constituent edges:
length

The distance from to , denoted
, is the
length
if there is a
length of the minimum
path
path from to ; and is
otherwise.

length(
distance from

to is

3
Single-Source Shortest-Paths
Problem
The Problem: Given a digraph with non-negative
edge
weights
and a distinguished source
,
vertex,
determine the distance and a shortest path from the
source vertex to every vertex
in

the digraph.

Question: How do you design an efficient algorithm
for this problem?

4
Single-Source Shortest-Paths
Problem
Important Observation: Any subpath of a shortest
path must also be a shortest path. Why?
Example: In the following
digraph, The subpath
est path.
length(
distance from

is a shortis also a shortest path.

to is

Observation Extending this idea we observe the existence of a shortest path tree in which distance
from to
source
is length of shortest path from
vertex in original tree.
source
to vertex

5
Intuition behind Dijkstra’s
Algorithm
Report the vertices in increasing order of their
distance from the source vertex.
Construct the shortest path tree edge by edge;
at
each step adding one new edge, corresponding
to construction of shortest path to the current
new
vertex.

6
The Rough Idea of Dijkstra’s
Algorithm
Maintain an estimate
of the length
the shortest path for each
.
vertex

of

Always
and
equals the length
of a known
path
(
if we have no paths so far).
Initially
and all the other
values are
set to . The algorithm will then process the
vertices one by one in some order.
The processed vertex’s estimate will be
validated
as being real shortest distance, i.e.
paths and updating
Here “processing a vertex allmeans finding necfor ”
if new
essary. The process by which an estimate is updated is called relaxation.
When all vertices have been processed,
for all .
7
The Rough Idea of Dijkstra’s
Algorithm
Question 1: How does the algorithm find new paths
and do the relaxation?
Question 2: In which order does the algorithm process the vertices one by one?

8
Answer to Question 1

Finding new paths. When processing a vertex ,
the algorithm will examine all vertices
.
For each vertex
, a new path from to
is found (path
to + new edge).
from
Relaxation. If the length of the new path from
to is shorter than
, then update
to the
length of this new path.

Remark: Whenever we
to a finite value,
set
there
exists a path of that length. Therefore
(Note: If

.

, then further relaxations cannot change

its value.)

9
Implementing the Idea of Relaxation
Consider an edge from a vertex to whose weight is
.
Suppose that we have already processed so that we know
and also computed a current estimate for
.
Then
There is a (shortest) path from
There is a path from

to

to

with length

with length

.

.

Combining this path from to with the edge
another path from to with length

, we obtain
.

If
, then we replace the old
with the new shorter path
. Hence we update
path

(originally,

s

).

w

d[v]
v

u
d[u]
10
The Algorithm for Relaxing an Edge
Relax(u,v)
if (

)
;
;

Remark: The predecessor pointer
mining the shortest paths.

is for deter-

11
Idea of Dijkstra’s Algorithm: Repeated
Relaxation
Dijkstra’s algorithm operates by maintaining a
sub- vertices,
set of
, for which we know the
true
distance, that is
.
Initially
, the empty set, and we set
for all others vertices . One by
and
to add to .
one select vertices from
we
The set can be implemented using an array of
vertex colors. Initially all vertices are white, and
we set
black to indicate that
.

12
The Selection in Dijkstra’s
Algorithm
Recall Question 2: What is the best order in which
to process vertices, so that the estimates are
guaranteed to converge to the true distances.
That is,the verticesthe algorithm select next? vertex
among how does of
to process which
Answer: We use a greedy algorithm. For each ver, we have computed a distance estex in
. The next vertex processed is always a
timate
for which
is minimum, that is,
vertex
we take the unprocessed vertex that is closest (by
our
estimate) to .
Question: How do we implement this selection of
vertices efficiently?
13
The Selection in Dijkstra’s
Algorithm
Question: How do we perform this selection
efficiently?
Answer: We store the vertices of
queue, where the key value of each
vertex

in a priority
is
.

[Note: if we implement the priority queue using a
heap,
we can perform the operations Insert(), time.]
Extract
and Decrease Key(), each in
Min(),

14
Review of Priority Queues
A Priority Queue is a data structure (can be implemented as a heap) which supports the following
operations:
insert(

): Insert

with the key value

in

.

u = extractMin(): Extract the item with the minimum
key value in .

decreaseKey(
.

-

): Decrease ’s key value
to

Remark: Priority Queues can be implementad such
that each operation takes
. See CLRS!
time
15
Description of Dijkstra’sAlgorithm
Dijkstra(G,w,s)
% Initialize
for (each

)
;
white;

;
NIL;
(queue with all vertices);
while (Non-Empty( ))
Extract-Min
for (each
if (

% Process all vertices
;
)

% Find new vertex
)

% If estimate improves
;

Decrease-Key
;

relax
;

black;

16
Dijkstra’s Algorithm
Example:
1

b inf

inf c

7
0
s

3

8

2

4

5

2
a

inf

inf

5

d

Step 0: Initialization.
s
0
ni

a

c

d

nil
W

l

b
nil
W

nil
W

nil
W

W
Priority Queue:

s
0

a

b

c

d
17
Dijkstra’s Algorithm
Example:
1

b 7

inf c

7
0
s

3

2

8
4

5

2
a

2

inf

5

Step 1: As
update information.

, work on

s
0
n
il
B
Priority Queue:

d

a

and

c

d

s s
W W
a

b

nil
W

and

nil
W

b

c

d
18
Dijkstra’s Algorithm
Example:
1

b 5

10 c

7
0
s

3

2

8
4

5

2
a

2

7

5

Step 2: After Step 1,
priority queue. As
and update
information.

has the minimum key in the
, work on , ,
s
0
n
il
B

Priority Queue:

d

a

c

d

s a
B W
b

b

a
W

a
W

c

d
19
Dijkstra’s Algorithm
Example:
1

b 5

6 c

7
0
s

3

2

8
4

5

2
a

2

7

5

Step 3: After Step 2,
priority queue. As
update
information.

has the minimum key in the
, work on , and
s
0
n
il
B

Priority Queue:

d

a

b

c

d

s a b a
B B W W
c

d
20
Dijkstra’s Algorithm
Example:
1

b 5

6 c

7
0
s

3

2

8
4

5

2
a

2

Step 4: After Step
3,
ority queue. As
information.

Priority Queue:

5

7

d

has the minimum key in the
pri-, work on and update
s a b c d
0
n s a b a
il B B B W
B
d
21
Dijkstra’s Algorithm
Example:
1

b 5

6 c

7
0
s

3

2

8
4

5

2
a

2

Step 5: After Step
4,
ority queue. As
information.

Priority Queue:

7

5

d

has the minimum key in the
pri-, work on and update
s
0
n
il
B
.

a

b

c

d

s a b a
B B B B

We are done.
22
Dijkstra’s Algorithm
Shortest Path Tree:
The array

is used to build the tree.

b 5

0
s

, where

1

6 c

3
2
a

2

7

5

d

Example:
s
0
nil

a

b

c

d

s

a

b

a

23
Correctness of Dijkstra’s
Algorithm

Lemma: When a vertex
queue),
.

is added to

(i.e., dequeued from the

Proof: Suppose to the contrary that at some point Dijkstra’s algorithm first attempts to add a vertex to for which
. By our observations about relaxation,
.
Consider the situation just prior to the insertion of . Consider
the true shortest path from to . Because
and
,
at some point this path must fi rst take a jump out of .
Let
be the edge taken by the path, where
and
(
it
may be
and/
).

that

or

u

S

s

x

y

24
Correctness of Dijkstra’s Algorithm –
Continued

We now prove that
when processing , so

. We have done relaxation
(1)

Since

is added to

earlier, by hypothesis,
(2)

Since

is subpath of a shortest path, by (2)
(3)

By (1) and (3),
Hence
So

(because we suppose

).

Now observe that since appears midway on the path from
, and all subsequent edges are non-negative, we have

to

, and thus
Thus

, which means would have been added to
,
befto be in contradiction to our assumption that is the next
vertex
added to .

ore

25
Proof of the Correctness of Dijkstra’s
Algorithm
By the lemma,
into , that is when we set

when

is added
black.

At the end of the algorithm, all vertices are in
then all distance estimates are correct.

26

,
Analysis of Dijkstra’s
Algorithm:
The initialization uses only

time.

Each vertex is processed exactly
once so Non-Empty()
and Extract-Min() are called exactly once,
e.g., inner loop for (each
The times in total.
) is called once
for each edge in the graph. Each call of the inner
loop
does
work plus, possibly, one
operation.

Decrease-Key

Recalling that all of the priority queue operations retime we have that the
quire
algorithm uses

time.
27

More Related Content

What's hot

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Inversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert TransformInversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert Transforminventionjournals
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithmMalinga Perera
 
A comparative study on multicast routing using dijkstra’s
A comparative study on multicast routing using dijkstra’sA comparative study on multicast routing using dijkstra’s
A comparative study on multicast routing using dijkstra’siaemedu
 
A comparative study on multicast routing using dijkstra’s, prims and ant colo...
A comparative study on multicast routing using dijkstra’s, prims and ant colo...A comparative study on multicast routing using dijkstra’s, prims and ant colo...
A comparative study on multicast routing using dijkstra’s, prims and ant colo...IAEME Publication
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Spike sorting: What is it? Why do we need it? Where does it come from? How is...
Spike sorting: What is it? Why do we need it? Where does it come from? How is...Spike sorting: What is it? Why do we need it? Where does it come from? How is...
Spike sorting: What is it? Why do we need it? Where does it come from? How is...NeuroMat
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning treeAlona Salva
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsAmr E. Mohamed
 
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...Nick Pruehs
 
Quantum Computation and Information
Quantum Computation and InformationQuantum Computation and Information
Quantum Computation and InformationXequeMateShannon
 
32 -longest-common-prefix
32 -longest-common-prefix32 -longest-common-prefix
32 -longest-common-prefixSanjeev Gupta
 
CiE 2010 talk
CiE 2010 talkCiE 2010 talk
CiE 2010 talkilyaraz
 
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmIntro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmkhalid Shah
 
Ram minimum spanning tree
Ram   minimum spanning treeRam   minimum spanning tree
Ram minimum spanning treeRama Prasath A
 

What's hot (20)

Quantum Noise and Error Correction
Quantum Noise and Error CorrectionQuantum Noise and Error Correction
Quantum Noise and Error Correction
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Inversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert TransformInversion Theorem for Generalized Fractional Hilbert Transform
Inversion Theorem for Generalized Fractional Hilbert Transform
 
Advances in Directed Spanners
Advances in Directed SpannersAdvances in Directed Spanners
Advances in Directed Spanners
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithm
 
A comparative study on multicast routing using dijkstra’s
A comparative study on multicast routing using dijkstra’sA comparative study on multicast routing using dijkstra’s
A comparative study on multicast routing using dijkstra’s
 
A comparative study on multicast routing using dijkstra’s, prims and ant colo...
A comparative study on multicast routing using dijkstra’s, prims and ant colo...A comparative study on multicast routing using dijkstra’s, prims and ant colo...
A comparative study on multicast routing using dijkstra’s, prims and ant colo...
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Spike sorting: What is it? Why do we need it? Where does it come from? How is...
Spike sorting: What is it? Why do we need it? Where does it come from? How is...Spike sorting: What is it? Why do we need it? Where does it come from? How is...
Spike sorting: What is it? Why do we need it? Where does it come from? How is...
 
Hierachical clustering
Hierachical clusteringHierachical clustering
Hierachical clustering
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
 
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...
Implementation of Thorup's Linear Time Algorithm for Undirected Single Source...
 
Quantum Computation and Information
Quantum Computation and InformationQuantum Computation and Information
Quantum Computation and Information
 
32 -longest-common-prefix
32 -longest-common-prefix32 -longest-common-prefix
32 -longest-common-prefix
 
CiE 2010 talk
CiE 2010 talkCiE 2010 talk
CiE 2010 talk
 
J42036572
J42036572J42036572
J42036572
 
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmIntro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithm
 
Ram minimum spanning tree
Ram   minimum spanning treeRam   minimum spanning tree
Ram minimum spanning tree
 
Chua's circuit
Chua's circuitChua's circuit
Chua's circuit
 

Similar to Dijkstra's Shortest Path Algorithm Lecture

01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfzefergaming
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
Algo labpresentation a_group
Algo labpresentation a_groupAlgo labpresentation a_group
Algo labpresentation a_groupUmme habiba
 
Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path AlgorithmAnish Ansari
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
 
shortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdfshortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdfzefergaming
 

Similar to Dijkstra's Shortest Path Algorithm Lecture (20)

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
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall 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
 
dijkstra algo.ppt
dijkstra algo.pptdijkstra algo.ppt
dijkstra algo.ppt
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
Algo labpresentation a_group
Algo labpresentation a_groupAlgo labpresentation a_group
Algo labpresentation a_group
 
Graph 3
Graph 3Graph 3
Graph 3
 
Shortest Path Algorithm
Shortest Path AlgorithmShortest Path Algorithm
Shortest Path Algorithm
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
shortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdfshortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdf
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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
 

Recently uploaded (20)

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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
 

Dijkstra's Shortest Path Algorithm Lecture

  • 1. Lecture 10: Dijkstra’s Shortest Path Algorithm CLRS 24.3 Outline of this Lecture Recalling the BFS solution of the shortest path problem for unweighted (di)graphs. The shortest path problem for weighted digraphs. Dijkstra’s algorithm. Given for digraphs but easily modified to work on undirected graphs. 1
  • 2. Recall: Shortest Path Problem for Graphs Let be a (di)graph. The shortest path between two vertices is a path with the shortest length (least number of edges). Call this the link-distance. Breadth-first-search is an algorithm for finding shortest (link-distance) paths from a single source ver- tex to all other vertices. BFS processes vertices in increasing order of their distance from the root vertex. BFS has running time . 2
  • 3. Shortest Path Problem for Weighted Graphs Let function weights. If be a weighted digraph, with weight mapping edges to real-valued , we write for . The length of a path is the sum of the weights of its constituent edges: length The distance from to , denoted , is the length if there is a length of the minimum path path from to ; and is otherwise. length( distance from to is 3
  • 4. Single-Source Shortest-Paths Problem The Problem: Given a digraph with non-negative edge weights and a distinguished source , vertex, determine the distance and a shortest path from the source vertex to every vertex in the digraph. Question: How do you design an efficient algorithm for this problem? 4
  • 5. Single-Source Shortest-Paths Problem Important Observation: Any subpath of a shortest path must also be a shortest path. Why? Example: In the following digraph, The subpath est path. length( distance from is a shortis also a shortest path. to is Observation Extending this idea we observe the existence of a shortest path tree in which distance from to source is length of shortest path from vertex in original tree. source to vertex 5
  • 6. Intuition behind Dijkstra’s Algorithm Report the vertices in increasing order of their distance from the source vertex. Construct the shortest path tree edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex. 6
  • 7. The Rough Idea of Dijkstra’s Algorithm Maintain an estimate of the length the shortest path for each . vertex of Always and equals the length of a known path ( if we have no paths so far). Initially and all the other values are set to . The algorithm will then process the vertices one by one in some order. The processed vertex’s estimate will be validated as being real shortest distance, i.e. paths and updating Here “processing a vertex allmeans finding necfor ” if new essary. The process by which an estimate is updated is called relaxation. When all vertices have been processed, for all . 7
  • 8. The Rough Idea of Dijkstra’s Algorithm Question 1: How does the algorithm find new paths and do the relaxation? Question 2: In which order does the algorithm process the vertices one by one? 8
  • 9. Answer to Question 1 Finding new paths. When processing a vertex , the algorithm will examine all vertices . For each vertex , a new path from to is found (path to + new edge). from Relaxation. If the length of the new path from to is shorter than , then update to the length of this new path. Remark: Whenever we to a finite value, set there exists a path of that length. Therefore (Note: If . , then further relaxations cannot change its value.) 9
  • 10. Implementing the Idea of Relaxation Consider an edge from a vertex to whose weight is . Suppose that we have already processed so that we know and also computed a current estimate for . Then There is a (shortest) path from There is a path from to to with length with length . . Combining this path from to with the edge another path from to with length , we obtain . If , then we replace the old with the new shorter path . Hence we update path (originally, s ). w d[v] v u d[u] 10
  • 11. The Algorithm for Relaxing an Edge Relax(u,v) if ( ) ; ; Remark: The predecessor pointer mining the shortest paths. is for deter- 11
  • 12. Idea of Dijkstra’s Algorithm: Repeated Relaxation Dijkstra’s algorithm operates by maintaining a sub- vertices, set of , for which we know the true distance, that is . Initially , the empty set, and we set for all others vertices . One by and to add to . one select vertices from we The set can be implemented using an array of vertex colors. Initially all vertices are white, and we set black to indicate that . 12
  • 13. The Selection in Dijkstra’s Algorithm Recall Question 2: What is the best order in which to process vertices, so that the estimates are guaranteed to converge to the true distances. That is,the verticesthe algorithm select next? vertex among how does of to process which Answer: We use a greedy algorithm. For each ver, we have computed a distance estex in . The next vertex processed is always a timate for which is minimum, that is, vertex we take the unprocessed vertex that is closest (by our estimate) to . Question: How do we implement this selection of vertices efficiently? 13
  • 14. The Selection in Dijkstra’s Algorithm Question: How do we perform this selection efficiently? Answer: We store the vertices of queue, where the key value of each vertex in a priority is . [Note: if we implement the priority queue using a heap, we can perform the operations Insert(), time.] Extract and Decrease Key(), each in Min(), 14
  • 15. Review of Priority Queues A Priority Queue is a data structure (can be implemented as a heap) which supports the following operations: insert( ): Insert with the key value in . u = extractMin(): Extract the item with the minimum key value in . decreaseKey( . - ): Decrease ’s key value to Remark: Priority Queues can be implementad such that each operation takes . See CLRS! time 15
  • 16. Description of Dijkstra’sAlgorithm Dijkstra(G,w,s) % Initialize for (each ) ; white; ; NIL; (queue with all vertices); while (Non-Empty( )) Extract-Min for (each if ( % Process all vertices ; ) % Find new vertex ) % If estimate improves ; Decrease-Key ; relax ; black; 16
  • 17. Dijkstra’s Algorithm Example: 1 b inf inf c 7 0 s 3 8 2 4 5 2 a inf inf 5 d Step 0: Initialization. s 0 ni a c d nil W l b nil W nil W nil W W Priority Queue: s 0 a b c d 17
  • 18. Dijkstra’s Algorithm Example: 1 b 7 inf c 7 0 s 3 2 8 4 5 2 a 2 inf 5 Step 1: As update information. , work on s 0 n il B Priority Queue: d a and c d s s W W a b nil W and nil W b c d 18
  • 19. Dijkstra’s Algorithm Example: 1 b 5 10 c 7 0 s 3 2 8 4 5 2 a 2 7 5 Step 2: After Step 1, priority queue. As and update information. has the minimum key in the , work on , , s 0 n il B Priority Queue: d a c d s a B W b b a W a W c d 19
  • 20. Dijkstra’s Algorithm Example: 1 b 5 6 c 7 0 s 3 2 8 4 5 2 a 2 7 5 Step 3: After Step 2, priority queue. As update information. has the minimum key in the , work on , and s 0 n il B Priority Queue: d a b c d s a b a B B W W c d 20
  • 21. Dijkstra’s Algorithm Example: 1 b 5 6 c 7 0 s 3 2 8 4 5 2 a 2 Step 4: After Step 3, ority queue. As information. Priority Queue: 5 7 d has the minimum key in the pri-, work on and update s a b c d 0 n s a b a il B B B W B d 21
  • 22. Dijkstra’s Algorithm Example: 1 b 5 6 c 7 0 s 3 2 8 4 5 2 a 2 Step 5: After Step 4, ority queue. As information. Priority Queue: 7 5 d has the minimum key in the pri-, work on and update s 0 n il B . a b c d s a b a B B B B We are done. 22
  • 23. Dijkstra’s Algorithm Shortest Path Tree: The array is used to build the tree. b 5 0 s , where 1 6 c 3 2 a 2 7 5 d Example: s 0 nil a b c d s a b a 23
  • 24. Correctness of Dijkstra’s Algorithm Lemma: When a vertex queue), . is added to (i.e., dequeued from the Proof: Suppose to the contrary that at some point Dijkstra’s algorithm first attempts to add a vertex to for which . By our observations about relaxation, . Consider the situation just prior to the insertion of . Consider the true shortest path from to . Because and , at some point this path must fi rst take a jump out of . Let be the edge taken by the path, where and ( it may be and/ ). that or u S s x y 24
  • 25. Correctness of Dijkstra’s Algorithm – Continued We now prove that when processing , so . We have done relaxation (1) Since is added to earlier, by hypothesis, (2) Since is subpath of a shortest path, by (2) (3) By (1) and (3), Hence So (because we suppose ). Now observe that since appears midway on the path from , and all subsequent edges are non-negative, we have to , and thus Thus , which means would have been added to , befto be in contradiction to our assumption that is the next vertex added to . ore 25
  • 26. Proof of the Correctness of Dijkstra’s Algorithm By the lemma, into , that is when we set when is added black. At the end of the algorithm, all vertices are in then all distance estimates are correct. 26 ,
  • 27. Analysis of Dijkstra’s Algorithm: The initialization uses only time. Each vertex is processed exactly once so Non-Empty() and Extract-Min() are called exactly once, e.g., inner loop for (each The times in total. ) is called once for each edge in the graph. Each call of the inner loop does work plus, possibly, one operation. Decrease-Key Recalling that all of the priority queue operations retime we have that the quire algorithm uses time. 27