logo
SEE ALGORITHMS
    Bubble Sort
    Insertion Sort
    Selection Sort
    Heap Sort
    Radix Sort
    Merge Sort
    Quick Sort

Dijkstra's Algorithm

Dijkstra's Algorithm finds the shortest path from a source node to all other nodes in a graph with non-negative weights. It uses a priority queue to explore nodes in order of increasing distance. This algorithm works by maintaining a set of visited nodes and, at each step, selecting the unvisited node with the smallest known distance to visit next. This process continues until all nodes have been visited, making it essential for network routing problems.

Pseudocode

dist = map vertex -> Infinity
dist[src] = 0
heap = new MinHeap()
heap.insert(src, 0)
while heap is not empty:
    (u, d) = heap.extract()
    if u is not visited:
        relax(u, d)
function relax(u, d):
    mark u as visited
    for each neighbor v of u:
        alt = d + weight(u, v)
        if alt < dist[v]:
            dist[v] = alt
            heap.insert(v, alt)
Draw Graph

Curious to Learn More?

Hand-picked resources to deepen your understanding

Beginner Friendly
Coding Interview Bootcamp: Algorithms + Data Structures

Learn essential data structures and algorithms step-by-step with practical JavaScript examples.

Practical Guide
JavaScript Algorithms & Data Structures Masterclass

Master DSA fundamentals, problem-solving techniques, and advanced structures using JavaScript.

Deep Dive
Master the Coding Interview: Data Structures + Algorithms

Prepare for top tech interviews with advanced DSA concepts and real-world coding challenges.

As an Udemy Associate, I earn from qualifying purchases. This helps support the site at no extra cost to you.

© 2025 SEE Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.