Dijkstra's
Algorithm
Presenter Ghazala Anjum
DIJKSTRA'S ALGORITHM
Dijkstra's algorithm is a graph search algorithm that solves
the single-source shortest path problem for a graph with
non-negative edge weights.
 It was developed by EdsgerW. Dijkstra in 1956 and
published in 1959.
 The algorithm is widely applied in network routing, GPS
navigation, and AI pathfinding.
B
A
C
D
1
3
2
4
Source
Destination
Weight
Key Terms
∞
0 ∞
∞
push
{0,0}
pq
dk[]= 0 ∞
0 1 2 3
∞ ∞
KEY TERMS
 Priority Queue: A data structure used in Dijkstra's algorithm to efficiently retrieve
the next node to process, based on the current minimum distance from the source
node. Common implementations include binary heaps.
 Distance Table: A data structure that stores the current shortest distance from the
source node to each node in the graph. Initially, the distance to the source node is
zero, and to all others, it is infinity.
 Relaxation:The algorithm's core concept is relaxation, which involves updating
the shortest path estimates as the algorithm progresses
DETAILED STEPS OF DIJKSTRA'S
ALGORITHM
Assign an initial distance of 0 to the source node and
infinity to all others.
Use a priority queue to select the node with the smallest
known distance.
Update the distances of neighboring nodes if a shorter
path is found.
Repeat until all nodes have been visited.
push
{0,0}
pq
dk[]
=
0 ∞
0 1 2 3
∞ ∞
0
1
3
2
4
8
6
2
Source
4
∞ ∞
∞
∞
0
∞
4
10
push
{8,2}
pq
0
1
3
2
4
8
6
2
4
8 ∞
∞
4
0
10
dk[]
=
0 4
0 1 2 3
8 ∞ ∞
4
push
{8,2}
pq
0
1
3
2
4
8
6
2
4
8 ∞
0
10
dk[]
=
0 4
0 1 2 3
8 ∞ ∞
4
pop
{4,1}
{0,0}
∞
4
push
{10,4}
pq
0
1
3
2
4
8
6
2
4
8 ∞
0
10
dk[]
=
0 4
0 1 2 3
8 ∞ 10
4
pop
{ 8,2 }
{4,1}
10
4
push
{10, 4}
pq
0
1
3
2
4
8
6
2
4
8 10
0
10
dk[]
=
0 4
0 1 2 3
8
4
pop
{ 10,3 }
{8,2}
10
4
10 10
{10, 4}
pq
0
1
3
2
4
8
6
2
4
8 10
0
10
dk[]
=
0 4
0 1 2 3
8
4
pop
10
4
10 10
{10,3}
pq
0
1
3
2
4
8
6
2
4
8 10
0
10
dk[]
=
0 4
0 1 2 3
8
4
pop
10
4
10 10
{10,4}
pq
0
1
3
2
4
8
6
2
4
8 10
0
10
dk[]
=
0 4
0 1 2 3
8
4
10
4
10 10
Given an undirected, weighted graph with V vertices numbered from 0 to V-1 with E edges
Shortest Paths:
For 0 to 1 ,minimum distance will be 4.By following path 0 1.For 0 to 2 minimum distance will
be 8, By following path 02 for 1 to 3 minimum distance will be 10.BY following path 0 2 3
for 0 to 4 minimum distance will be 10 .by following 01 4
Note:The Graph is connected and doesn't contain any negative weight edge.
IMPLEMENTATION OF DIJKSTRA'S ALGORITHM
PSEUDOCODE OVERVIEW:
The time complexity of Dijkstra’s algorithm when using a min-heap is O(E log V), where E is the number of
edges and V is the number of vertices. So,the complexity of Dijkstra’s algorithm is O(E log V)
Create vertex set Q
For each vertex v in graph
dist [v]= ∞
add vto Q
dist[source]=0
While Q is not empty
u=Extract-min[Q]
For each neighbor v of u
relax (u,v)
O(v)
O(log v) ,
STEP-BY-STEP IMPLEMENTATION
 Set dist[source]=0 and all other distances as infinity.
 Push the source node into the min heap as a pair <distance, node> i.e., <0, source>.
→
 Pop the top element (node with the smallest distance) from the min heap.
 For each adjacent neighbor of the current node:
 Calculate the distance using the formula:
 dist[v] = dist[u] + weight[u][v]
 If this new distance is shorter than the current dist[v], update it.
 Push the updated pair <dist[v], v> into the min heap
 Repeat step 3 until the min heap is empty.
 Return the distance array, which holds the shortest distance from the source to all nodes.
 Time Complexity: O(E*logV),Where E is the number of edges and V is the
number of vertices.
Auxiliary Space: O(V),Where V is the number of vertices,We do not count the
adjacency list in auxiliary space as it is necessary for representing the input graph.
LIMITATIONS AND CONSIDERATIONS
 Dijkstra's algorithm does not work with graphs that contain negative weight edges,
which can lead to incorrect results.
 For graphs with such characteristics, the Bellman-Ford algorithm is
recommended as it can correctly handle negative weights
APPLICATIONS
 Dijkstra's algorithm is widely used in various applications, including:
 Navigation Systems: Finding the shortest route in mapping services (e.g., Google
Maps).
 Network Routing: Determining the most efficient path for data transfer in
telecommunications.
 Game Development: Enabling character navigation and movement through a
virtual environment.
 Robotics: Planning routes for robotic navigation.
20
References
• https://www.youtube.com/watch?v=Gd92jSu_cZk
• https://www.youtube.com/watch?v=_0s2e5SqhSk&t=293s
• https://www.freecodecamp.org/news/dijkstras-shortest-path-algorith
m-visual-introduction
/
• https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-gre
edy-algo-7
/
• https://www.w3schools.com/dsa/dsa_algo_graphs_dijkstra.php

Dijkstra Algorithm Presentation -the shortest path finding algorithm.pptx

  • 1.
  • 2.
    DIJKSTRA'S ALGORITHM Dijkstra's algorithmis a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge weights.  It was developed by EdsgerW. Dijkstra in 1956 and published in 1959.  The algorithm is widely applied in network routing, GPS navigation, and AI pathfinding.
  • 3.
  • 4.
    KEY TERMS  PriorityQueue: A data structure used in Dijkstra's algorithm to efficiently retrieve the next node to process, based on the current minimum distance from the source node. Common implementations include binary heaps.  Distance Table: A data structure that stores the current shortest distance from the source node to each node in the graph. Initially, the distance to the source node is zero, and to all others, it is infinity.  Relaxation:The algorithm's core concept is relaxation, which involves updating the shortest path estimates as the algorithm progresses
  • 5.
    DETAILED STEPS OFDIJKSTRA'S ALGORITHM Assign an initial distance of 0 to the source node and infinity to all others. Use a priority queue to select the node with the smallest known distance. Update the distances of neighboring nodes if a shorter path is found. Repeat until all nodes have been visited.
  • 6.
    push {0,0} pq dk[] = 0 ∞ 0 12 3 ∞ ∞ 0 1 3 2 4 8 6 2 Source 4 ∞ ∞ ∞ ∞ 0 ∞ 4 10
  • 7.
  • 8.
    push {8,2} pq 0 1 3 2 4 8 6 2 4 8 ∞ 0 10 dk[] = 0 4 01 2 3 8 ∞ ∞ 4 pop {4,1} {0,0} ∞ 4
  • 9.
    push {10,4} pq 0 1 3 2 4 8 6 2 4 8 ∞ 0 10 dk[] = 0 4 01 2 3 8 ∞ 10 4 pop { 8,2 } {4,1} 10 4
  • 10.
    push {10, 4} pq 0 1 3 2 4 8 6 2 4 8 10 0 10 dk[] = 04 0 1 2 3 8 4 pop { 10,3 } {8,2} 10 4 10 10
  • 11.
    {10, 4} pq 0 1 3 2 4 8 6 2 4 8 10 0 10 dk[] = 04 0 1 2 3 8 4 pop 10 4 10 10 {10,3}
  • 12.
    pq 0 1 3 2 4 8 6 2 4 8 10 0 10 dk[] = 0 4 01 2 3 8 4 pop 10 4 10 10 {10,4}
  • 13.
  • 14.
    Given an undirected,weighted graph with V vertices numbered from 0 to V-1 with E edges Shortest Paths: For 0 to 1 ,minimum distance will be 4.By following path 0 1.For 0 to 2 minimum distance will be 8, By following path 02 for 1 to 3 minimum distance will be 10.BY following path 0 2 3 for 0 to 4 minimum distance will be 10 .by following 01 4 Note:The Graph is connected and doesn't contain any negative weight edge.
  • 15.
    IMPLEMENTATION OF DIJKSTRA'SALGORITHM PSEUDOCODE OVERVIEW: The time complexity of Dijkstra’s algorithm when using a min-heap is O(E log V), where E is the number of edges and V is the number of vertices. So,the complexity of Dijkstra’s algorithm is O(E log V) Create vertex set Q For each vertex v in graph dist [v]= ∞ add vto Q dist[source]=0 While Q is not empty u=Extract-min[Q] For each neighbor v of u relax (u,v) O(v) O(log v) ,
  • 16.
    STEP-BY-STEP IMPLEMENTATION  Setdist[source]=0 and all other distances as infinity.  Push the source node into the min heap as a pair <distance, node> i.e., <0, source>. →  Pop the top element (node with the smallest distance) from the min heap.  For each adjacent neighbor of the current node:  Calculate the distance using the formula:  dist[v] = dist[u] + weight[u][v]  If this new distance is shorter than the current dist[v], update it.  Push the updated pair <dist[v], v> into the min heap  Repeat step 3 until the min heap is empty.  Return the distance array, which holds the shortest distance from the source to all nodes.
  • 17.
     Time Complexity:O(E*logV),Where E is the number of edges and V is the number of vertices. Auxiliary Space: O(V),Where V is the number of vertices,We do not count the adjacency list in auxiliary space as it is necessary for representing the input graph.
  • 18.
    LIMITATIONS AND CONSIDERATIONS Dijkstra's algorithm does not work with graphs that contain negative weight edges, which can lead to incorrect results.  For graphs with such characteristics, the Bellman-Ford algorithm is recommended as it can correctly handle negative weights
  • 19.
    APPLICATIONS  Dijkstra's algorithmis widely used in various applications, including:  Navigation Systems: Finding the shortest route in mapping services (e.g., Google Maps).  Network Routing: Determining the most efficient path for data transfer in telecommunications.  Game Development: Enabling character navigation and movement through a virtual environment.  Robotics: Planning routes for robotic navigation.
  • 20.
    20 References • https://www.youtube.com/watch?v=Gd92jSu_cZk • https://www.youtube.com/watch?v=_0s2e5SqhSk&t=293s •https://www.freecodecamp.org/news/dijkstras-shortest-path-algorith m-visual-introduction / • https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-gre edy-algo-7 / • https://www.w3schools.com/dsa/dsa_algo_graphs_dijkstra.php

Editor's Notes

  • #3 Here 1,2,3,4 can say distance,time,cost,Weight must be non negative