Skip to main content
CSAlgorithms & ComplexityDiagramIntermediate

Dijkstra's Algorithm

KO: 다익스트라 알고리즘·EN: Dijkstra's Algorithm·JA: ダイクストラ法

A greedy shortest-path algorithm for non-negative weighted graphs using a priority queue to find optimal distances in O((V+E) log V) time

Table of Contents

Structure Diagram

Dijkstra Shortest Path Edge RelaxationTap a node
w=2w=5w=1 (Relaxw=3w=7Start A (…B (dist: …C (dist: …Goal D (d…
ROOTStart A (0)
2 links
Quick Answer

What is Dijkstra's Algorithm?

A greedy shortest-path algorithm for non-negative weighted graphs using a priority queue to find optimal distances in O((V+E) log V) time. It is a core concept in Algorithms & Complexity within CS.

Core Principles

Dijkstra's Algorithm computes the shortest path from a single source to all other vertices in a weighted graph with non-negative edge weights.

  • Greedy Extraction & Relaxation: Pop the closest unvisited vertex u from a min-heap, then relax each outgoing edge (u, v, w): if dist[u] + w < dist[v], update dist[v] and push to the heap.
  • Complexity: O((V + E) log V) with an adjacency list and binary heap.
  • Non-Negative Requirement: Negative edges break the greedy assumption that popped vertices are final (use Bellman-Ford instead).

Practical Takeaways

  • Skip stale heap entries: Rather than implementing decrease-key, push updated (dist, node) pairs and ignore popped entries where d > dist[u].
  • Early exit & A extension*: Stop immediately once the target node is popped; add an admissible heuristic (like Euclidean distance) to upgrade to A* for spatial routing.
  • Use BFS for unit weights: If every edge costs 1, plain BFS runs faster in O(V + E) without heap overhead.

Notable Examples

  • GPS Navigation: Computing fastest routes over weighted road segments.
  • OSPF Network Routing: Calculating lowest-cost packet forwarding tables across routers.
  • Flight & Logistics Routing: Minimizing travel cost or transfer latency.