Skip to content
Back to exploration
Mathematical modeling / English

Dijkstra's algorithm in 3 minutes

Michael Sambol · YouTube · 2:45

Open original
READ & KEEP

The explanation, unpacked.

Reviewed learning material · Video analysis · English
Read the full overview

This video explains Dijkstra's algorithm for finding the shortest path from a single source node to all other nodes in a weighted directed graph. It contrasts the algorithm with Prim's and Kruskal's minimum spanning tree algorithms. A step-by-step example demonstrates how to initialize distances, select the unvisited node with the smallest distance, update neighbors, and mark nodes as visited until all are processed. Finally, it states the time complexity is O(|E| + |V|log|V|) when using a Fibonacci heap. This 15-second video clip displays the complete pseudocode for Dijkstra's algorithm, which computes shortest paths from a source vertex in a graph with positive edge weights. The visual includes a brief handwritten annotation of the algorithm's time complexity, O(|E| + |V|log|V|), which fades out within the first second. The audio consists solely of a narrator introducing the pseudocode and concluding the presentation, without explaining the individual steps or logic of the algorithm.

Use the learning inspector for key ideas and moments, or open the reading tabs for the complete notes.

Chapters

0:00Introduction to Dijkstra's Algorithm0:17Step-by-Step Example2:12Time Complexity2:30Dijkstra's Algorithm Pseudocode

Learning script

Generated from the video's visuals and explanation; not verbatim speech.

The video introduces Dijkstra's algorithm, explaining its purpose is to find the shortest path from a single starting node to all other nodes in a weighted directed graph.

It clarifies that this is different from Prim's and Kruskal's algorithms, which are used to find minimum spanning trees.

A sample weighted directed graph with nodes A, B, C, D, and E is presented to demonstrate the algorithm.

The process begins by initializing a distance table: the starting node A is set to 0, and all other nodes are set to infinity.

The algorithm examines edges leaving A, updating the distances to B(4)B (4) and C(2)C (2).

Node C is selected as the next node to visit because it has the smallest distance among unvisited nodes.

Edges leaving C are examined, updating the distance to B to 3 (via C), and setting distances to D(6)D (6) and E(7)E (7).

Node B is selected next. Edges leaving B update the distance to D to 5 and E to 6.

Node D is selected, but it has no outgoing edges, so no updates occur.

Node E is selected. Its outgoing edge to D does not provide a shorter path, so the table remains unchanged.

The final shortest paths from A are displayed: A=0A=0, B=3B=3, C=2C=2, D=5D=5, E=6E=6.

The video concludes by stating the time complexity of Dijkstra's algorithm is O(|E| + |V|log|V|) when using a Fibonacci heap.

The screen presents the formal pseudocode for Dijkstra's algorithm, a fundamental method in graph theory for solving the single-source shortest path problem. A handwritten annotation briefly overlays the text, stating the time complexity as O(∣E∣+∣V∣log⁡∣V∣)O(|E| + |V|\log|V|), before fading away.

The algorithm takes as input a graph G=(V,E)G = (V, E), which can be either directed or undirected, along with a set of strictly positive edge lengths {le:e∈E}\{l_e : e \in E\} and a designated source vertex s∈Vs \in V. Its output is an array distdist, where dist(u)dist(u) represents the shortest distance from ss to any reachable vertex uu.

The procedure begins by initializing the distance dist(u)dist(u) to infinity and the predecessor prev(u)prev(u) to nil for every vertex uu in VV. The distance to the source itself is set to zero, dist(s)=0dist(s) = 0. A priority queue HH is then constructed containing all vertices, using their current distdist values as keys.

The core loop continues while the priority queue HH is not empty. In each iteration, the vertex uu with the minimum distance is extracted via deletemin(H)deletemin(H). The algorithm then iterates over all outgoing edges (u,v)∈E(u,v) \in E. If a shorter path to vv is discovered through uu, specifically if dist(v)>dist(u)+l(u,v)dist(v) > dist(u) + l(u,v), the distance dist(v)dist(v) is updated to dist(u)+l(u,v)dist(u) + l(u,v), the predecessor prev(v)prev(v) is set to uu, and the key for vv is decreased in the priority queue HH.

Knowledge cards

01

Dijkstra's Algorithm Definition

An algorithm to find the shortest path from a single source node to all other nodes in a weighted graph.

02

Contrast with MST Algorithms

Unlike Prim's and Kruskal's which find minimum spanning trees, Dijkstra's finds shortest paths from a specific source.

03

Algorithm Steps

1. Init distances (source=0, others=inf). 2. Pick unvisited node with min distance. 3. Update neighbors. 4. Mark visited. 5. Repeat.

04

Time Complexity

The efficiency of the algorithm depends on the data structure used for the priority queue.

O(∣E∣+∣V∣log⁡∣V∣)O(|E| + |V|\log|V|)
05

Dijkstra's Algorithm Pseudocode

Dijkstra's algorithm finds the shortest paths from a source vertex ss to all other reachable vertices in a graph G=(V,E)G=(V,E) with positive edge weights ll. It initializes all distances to infinity except the source's distance, which is set to 0. It uses a priority queue HH keyed by these distances. In each step, it extracts the vertex uu with the smallest distance and relaxes all its outgoing edges (u,v)(u,v). If a shorter path to vv is found via uu (i.e., dist(v)>dist(u)+l(u,v)dist(v) > dist(u) + l(u,v)), it updates dist(v)dist(v), records uu as the predecessor of vv, and decreases vv's key in the priority queue. The video also briefly notes the time complexity as O(∣E∣+∣V∣log⁡∣V∣)O(|E| + |V|\log|V|), which corresponds to an implementation using a Fibonacci heap.

proceduredijkstra(G,l,s)Input:GraphG=(V,E),directedorundirected;positiveedgelengthsle:e∈E;vertexs∈VOutput:Forallverticesureachablefroms,dist(u)issettothedistancefromstou.forallu∈V:dist(u)=∞prev(u)=nildist(s)=0H=makequeue(V)(usingdist−valuesaskeys)whileHisnotempty:u=deletemin(H)foralledges(u,v)∈E:ifdist(v)>dist(u)+l(u,v):dist(v)=dist(u)+l(u,v)prev(v)=udecreasekey(H,v)procedure dijkstra(G,l,s) Input: Graph G = (V, E), directed or undirected; positive edge lengths {l_e : e \in E}; vertex s \in V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u \in V: dist(u) = \infty prev(u) = nil dist(s) = 0 H = makequeue(V) (using dist-values as keys) while H is not empty: u = deletemin(H) for all edges (u,v) \in E: if dist(v) > dist(u) + l(u,v): dist(v) = dist(u) + l(u,v) prev(v) = u decreasekey(H,v)

Detailed learning notes

Explore conditions, steps and evidence. Supplementary explanations are labeled separately from content shown in the video.

Symbols · 10

|V|

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Appears in the time complexity formula as |V|.

Symbol

|V|

Meaning

Number of vertices in the graph

Domain

Positive integer

|E|

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Appears in the time complexity formula as |E|.

Symbol

|E|

Meaning

Number of edges in the graph

Domain

Non-negative integer

G

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Input: Graph G=(V,E)G = (V, E)

Symbol

G

Meaning

The graph on which Dijkstra's algorithm operates.

Domain

A directed or undirected graph.

V

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Input: Graph G=(V,E)G = (V, E); for all u∈Vu \in V:

Symbol

V

Meaning

The set of vertices in the graph G.

E

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Input: Graph G=(V,E)G = (V, E); for all edges (u,v) ∈E\in E:

Symbol

E

Meaning

The set of edges in the graph G.

l

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    positive edge lengths {lel_e : e∈Ee \in E}; if dist(v) > dist(u) + l(u,v)l(u,v):

Symbol

l

Meaning

The edge length function assigning a positive weight to each edge.

s

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    vertex s∈Vs \in V; dist(s) = 0

Symbol

s

Meaning

The source vertex from which shortest paths are computed.

dist

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    dist(u) is set to the distance from s to u.; dist(u) = ∞\infty; dist(s) = 0

Symbol

dist

Meaning

An array storing the current shortest known distance from the source s to each vertex.

prev

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    prev(u) = nil; prev(v) = u

Symbol

prev

Meaning

An array storing the predecessor vertex on the shortest path from s to each vertex.

H

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    H = makequeue(V) (using dist-values as keys); while H is not empty:

Symbol

H

Meaning

A priority queue containing vertices, keyed by their current dist values.

Knowledge points · 5

Definition of Dijkstra's Algorithm

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Speaker states Dijkstra's algorithm finds the shortest distance from one node to every other node.

  2. Diagram
    Observation

    Text on screen reads 'shortest path from one node to every other node'.

Definition
Explanation

Dijkstra's algorithm is a method for finding the shortest path from a single starting node to all other nodes in a weighted directed graph.

Conditions
  1. Graph must be weighted

  2. Graph can be directed or undirected (video uses directed)

Contrast with Minimum Spanning Tree Algorithms

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Speaker contrasts Dijkstra's with Prim's and Kruskal's algorithms.

Definition
Explanation

Unlike Prim's and Kruskal's algorithms which produce minimum spanning trees, Dijkstra's algorithm produces shortest paths from a specific source node.

Prerequisites
  1. Definition of Dijkstra's Algorithm

Execution Steps of Dijkstra's Algorithm

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Speaker narrates the steps: pick start node, update distances, pick smallest unvisited, repeat.

  2. Animation
    Observation

    Visual demonstration of updating the distance table and crossing off nodes.

Method
Explanation

1. Initialize distances to infinity, except start node (0). 2. Select the unvisited node with the smallest distance. 3. Update distances to its neighbors if a shorter path is found. 4. Mark the node as visited. 5. Repeat until all nodes are visited.

Conditions
  1. Requires a priority queue or list to track unvisited nodes and their current shortest distances.

Prerequisites
  1. Definition of Dijkstra's Algorithm

Time Complexity with Fibonacci Heap

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Formula O(|E| + |V|log|V|) is written on screen.

  2. Audio
    Observation

    Speaker states the time complexity is Big O of E plus V log V if a Fibonacci heap is used.

Formula
Explanation

The time complexity of Dijkstra's algorithm is O(|E| + |V|log|V|) when implemented using a Fibonacci heap for the priority queue.

Conditions
  1. Uses a Fibonacci heap data structure.

Prerequisites
  1. Execution Steps of Dijkstra's Algorithm

Dijkstra's Algorithm Pseudocode

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    procedure dijkstra(G,l,s) ... decreasekey(H,v)

Method
Explanation

The pseudocode defines Dijkstra's algorithm for finding shortest paths from a source vertex s in a graph with positive edge lengths. It initializes distances to infinity and predecessors to nil, sets the source distance to 0, and uses a priority queue H to repeatedly extract the unvisited vertex with the smallest distance. For each extracted vertex u, it relaxes all outgoing edges (u,v), updating dist(v) and prev(v) if a shorter path is found via u, and decreases the key of v in the priority queue.

Formula
proceduredijkstra(G,l,s)Input:GraphG=(V,E),directedorundirected;positiveedgelengthsle:e∈E;vertexs∈VOutput:Forallverticesureachablefroms,dist(u)issettothedistancefromstou.forallu∈V:dist(u)=∞prev(u)=nildist(s)=0H=makequeue(V)(usingdist−valuesaskeys)whileHisnotempty:u=deletemin(H)foralledges(u,v)∈E:ifdist(v)>dist(u)+l(u,v):dist(v)=dist(u)+l(u,v)prev(v)=udecreasekey(H,v)procedure dijkstra(G,l,s) Input: Graph G = (V, E), directed or undirected; positive edge lengths {l_e : e \in E}; vertex s \in V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u \in V: dist(u) = \infty prev(u) = nil dist(s) = 0 H = makequeue(V) (using dist-values as keys) while H is not empty: u = deletemin(H) for all edges (u,v) \in E: if dist(v) > dist(u) + l(u,v): dist(v) = dist(u) + l(u,v) prev(v) = u decreasekey(H,v)
Conditions
  1. The graph must have positive edge lengths.

  2. The graph can be directed or undirected.

Worked examples · 1

Step-by-step execution on a sample graph

Clear evidence
Shown in the video
Evidence
  1. Diagram
    Observation

    A weighted directed graph with nodes A, B, C, D, E is drawn and processed step-by-step.

  2. Audio
    Observation

    Speaker walks through the selection of nodes A -> C -> B -> D -> E and updates the distance table.

Problem

Find the shortest paths from node A to all other nodes in the given weighted directed graph.

Given
  1. Nodes: A, B, C, D, E

  2. Edges and weights: A->B(4)B(4), A->C(2)C(2), C->B(1)B(1), B->D(2)D(2), B->E(3)E(3), C->D(4)D(4), C->E(5)E(5), E->D(1)D(1)

Goal

Determine the shortest distance from A to B, C, D, and E.

Steps
  1. Explanation

    Initialize distances: A=0A=0, others=infinity. Unvisited set: {A, B, C, D, E}.

    Justification

    Starting condition for Dijkstra's algorithm.

    Shown in the video
  2. Explanation

    Select A (dist 0). Update neighbors: B=min(inf, 0+40+4)=4, C=min(inf, 0+20+2)=2. Mark A visited.

    Justification

    Process edges leaving the current minimum distance unvisited node.

    Shown in the video
  3. Explanation

    Select C (dist 2). Update neighbors: B=min(4, 2+12+1)=3, D=min(inf, 2+42+4)=6, E=min(inf, 2+52+5)=7. Mark C visited.

    Justification

    Process edges leaving C.

    Shown in the video
  4. Explanation

    Select B (dist 3). Update neighbors: D=min(6, 3+23+2)=5, E=min(7, 3+33+3)=6. Mark B visited.

    Justification

    Process edges leaving B.

    Shown in the video
  5. Explanation

    Select D (dist 5). No outgoing edges. Mark D visited.

    Justification

    Process edges leaving D.

    Shown in the video
  6. Explanation

    Select E (dist 6). Update neighbor D: min(5, 6+16+1)=5 (no change). Mark E visited.

    Justification

    Process edges leaving E.

    Shown in the video
Answer

Shortest distances from A: A=0A=0, B=3B=3, C=2C=2, D=5D=5, E=6E=6.

Verification

The final table matches the calculated shortest paths.

Visual events · 3

Initial Graph Setup

Clear evidence
Shown in the video
Evidence
  1. Diagram
    Observation

    Hand-drawn graph appears with nodes A, B, C, D, E and weighted directed edges.

Objects
  1. Nodes A, B, C, D, E

  2. Directed edges with weights

Changes
  1. Graph is drawn on the whiteboard.

Invariants
  1. Edge weights remain constant throughout the example.

Interpretation

Defines the problem instance for the algorithm demonstration.

Algorithm Execution Visualization

Clear evidence
Shown in the video
Evidence
  1. Animation
    Observation

    Distance table on the right updates values as nodes are processed; unvisited list at bottom shrinks.

Objects
  1. Distance table

  2. Unvisited nodes list

Changes
  1. Distances decrease or become finite.

  2. Nodes are removed from the unvisited list.

Invariants
  1. Processed nodes are never revisited or updated again.

Interpretation

Shows the greedy selection and relaxation steps of Dijkstra's algorithm.

Time Complexity Annotation

Clear evidence
Shown in the video
Evidence
  1. Animation
    Observation

    Handwritten text 'Time complexity O(|E| + |V|log|V|)' appears over the pseudocode and then fades out.

Objects
  1. Handwritten text overlay

Changes
  1. The handwritten text appears at the start of the clip and fades out completely by 1 second.

Invariants
  1. The underlying typed pseudocode remains visible throughout.

Interpretation

The annotation indicates the time complexity of the algorithm when implemented with a Fibonacci heap priority queue, though this specific implementation detail is not stated in the typed pseudocode.

Misconceptions · 1

Confusing Shortest Path with Minimum Spanning Tree

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Speaker explicitly distinguishes Dijkstra's from Prim's and Kruskal's.

Misconception

Thinking Dijkstra's algorithm finds a minimum spanning tree like Prim's or Kruskal's.

Clarification

Dijkstra's finds the shortest path from a single source to all other nodes, not a tree connecting all nodes with minimum total edge weight.

Concept relations · 2

Definition of Dijkstra's Algorithm → Contrast with Minimum Spanning Tree Algorithms

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Comparison made between Dijkstra's and MST algorithms.

Contrast
Explanation

Dijkstra's algorithm solves the single-source shortest path problem, whereas Prim's and Kruskal's solve the minimum spanning tree problem.

Execution Steps of Dijkstra's Algorithm → Time Complexity with Fibonacci Heap

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Complexity is discussed after the algorithm steps.

Application
Explanation

The stated time complexity applies specifically to the execution steps of Dijkstra's algorithm when using a Fibonacci heap.

Find an answer · 3

How does Dijkstra's algorithm update distances step-by-step?

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Detailed walkthrough of the algorithm.

Knowledge points
  1. Execution Steps of Dijkstra's Algorithm
  2. Step-by-step execution on a sample graph

What is the time complexity of Dijkstra's algorithm with a Fibonacci heap?

Clear evidence
Shown in the video
Evidence
  1. Formula
    Observation

    Time complexity formula shown.

Knowledge points
  1. Time Complexity with Fibonacci Heap

What is the pseudocode for Dijkstra's algorithm?

Clear evidence
Shown in the video
Evidence
  1. Audio
    Observation

    Here is the pseudocode for Dijkstra's algorithm.

Knowledge points
  1. Dijkstra's Algorithm Pseudocode
Coverage and review notes

Covered · Introduction and definition.

Covered · Algorithm execution example.

Covered · Final result display.

Covered · Time complexity discussion.

Covered · Static display of Dijkstra's algorithm pseudocode with brief audio introduction and conclusion.

Explore the knowledge in this video

Open video knowledge graph →

  • Shortest path models ExplanationAt 0:00
    Why this connection?

    Reviewed English material v1: card/concept_card "Dijkstra's Algorithm Definition" starting at 0:00 — An algorithm to find the shortest path from a single source node to all other nodes in a weighted graph.

  • Minimum spanning tree ExplanationAt 0:12
    Why this connection?

    Reviewed English material v1: knowledge_item/definition "Contrast with Minimum Spanning Tree Algorithms" from 0:12 to 0:16 — Unlike Prim's and Kruskal's algorithms which produce minimum spanning trees, Dijkstra's algorithm produces shortest paths from a specific source node.

Questions this video answers

Meet the concept

↗
Meet the concept

↗
Meet the concept

↗
Find a method

↗