Dijisktra Algorithm Explained with Diagram

Dijkstra Algorithm

Steps of the Algorithm:
Initialization:

Create an array (or dictionary) called distances, where each element represents the shortest distance from the source to a particular node.
Initialize all distances to infinity (float('inf')) except for the source node, which is set to 0 because the distance from the source to itself is zero.
Use a Min-Priority Queue:

A priority queue (or a set) is used to store nodes along with their tentative shortest distances. The queue helps in retrieving the next node with the minimum distance efficiently.
Initially, insert the source node into the queue with a distance of 0.
Processing Nodes (Similar to BFS):

Begin by visiting the source node, and for each of its neighbors, calculate the total distance from the source.
If the newly calculated distance to a neighbor is shorter than the current known distance, update the distance and insert the neighbor into the queue with this updated distance.
Greedy Selection:

Always process the node with the smallest distance from the queue.
Once a node has been processed, it is considered visited, and its shortest path is finalized.
Continue Until All Nodes Are Processed:

Repeat the process for all nodes, updating distances and expanding the node with the smallest known distance until all nodes are visited or the queue is empty.

image

Comments (0)