Dijkstra’s Algorithm
• It is a single-source shortest path problem or
algorithm in the graph theory.
• It is used for finding shortest paths from a
source vertex u to all other vertices in the
graph.
• This works on both directed and undirected
graphs.
• All the edges must have non-negative weights.
Procedure
Pseudocode
Example
u A B C D E
π N N N N N
Cont…
Cont…
u A B C D E
π N A A N N
Cont…
u A B C D E
π N A A N N
Cont…
u A B C D E
π N C A C C
Cont…
u A B C D E
π N C A C C
Cont…
u A B C D E
π N C A C C
Cont…
u A B C D E
π N C A B C
Cont…
u A B C D E
π N C A B C
Cont…
u A B C D E
π N C A B C
Cont…
Shortest Path
u A B C D E
π N C A B C
Running Times
• The simplest implementation is to store
vertices in an array or linked list. This will
produce a running time of
• For any graphs with very few edges and many
nodes or vertices, it can be implemented more
efficiently storing the graph in an adjacency
list using a binary heap or priority queue. This
will produce a running time of
Time Complexity: Using Linked list