Intuition The ONE core idea
Dijkstra finds the shortest travel-time from one starting place to every other place, by always expanding outward to the nearest place you haven't finished yet . Because roads never give back time (weights are non-negative), the first time you reach a place the cheap way is guaranteed to be the cheapest way — so you can stamp it "done" and never look again.
This page builds the vocabulary the parent note Dijkstra's algorithm leans on, one symbol at a time. Nothing below assumes you have seen graph notation before. Read top to bottom; each idea is the ground the next one stands on.
A graph is just dots connected by lines . We write G = ( V , E ) , read as "the graph G is made of a set of dots V and a set of lines E ."
V = the set of vertices (the dots — think cities , houses , web pages ).
E = the set of edges (the lines — think roads connecting two cities).
Look at the figure below. Each labelled circle is a vertex . Each arrow between two circles is an edge .
Intuition Why the topic needs this
Dijkstra is a question about a graph: "starting from one dot, what is the cheapest way to reach every other dot?" You literally cannot state the problem without dots (V ) and lines (E ).
The same letters V and E get reused in two different jobs, and mixing them up trips beginners. Let's separate them cleanly.
∣ V ∣ and ∣ E ∣ — the sizes of the sets
The vertical bars ∣ ⋅ ∣ mean "how many things are inside ." So:
V = the set of vertices; ∣ V ∣ = the number of vertices (count the dots).
E = the set of edges; ∣ E ∣ = the number of edges (count the lines).
In the figure above, ∣ V ∣ = 4 (dots A, B, C, D) and ∣ E ∣ = 5 (five arrows).
Intuition Why we still write "
O (( V + E ) log V ) " without the bars
Textbooks (and the parent note) are lazy and drop the bars, writing V where they mean ∣ V ∣ . Whenever V or E appears inside a Big-O , silently read it as the count ∣ V ∣ or ∣ E ∣ . Everywhere else it means the set . That is the only ambiguity — and now you know both readings.
Before we can talk about one specific dot or the road between two dots, we need short names for "some dot." Just as algebra uses x for "some number," graph work uses lowercase letters for "some vertex."
u and v — placeholders for arbitrary vertices
u and v are stand-in names for any two vertices (any two dots from the set V ). When we write a rule "for vertices u and v ," we mean it holds no matter which actual dots you plug in — A, B, C, or D. Think of u and v as blank labels you can peel off and stick onto whichever dots you're currently discussing.
Intuition Why we need generic names
A graph can have millions of dots. We cannot write a separate rule for A, for B, for C... So we state each rule once using placeholders u and v , and it automatically applies to every pair of real dots. Every rule below — direction, weight, relaxation — is written with these placeholders.
An edge can point one way (a one-way street) or both ways (a two-way street). We use the placeholders u and v from §2 to describe the two endpoints.
Definition Directed vs. undirected
Directed edge u → v : you may travel from u to v , but not necessarily back. Drawn as an arrow.
Undirected edge : travel either way. Drawn as a plain line (equivalent to two arrows).
The parent's worked example (A→B, A→C, ...) uses arrows, so it is a directed graph. In the figure of §1, notice A→B exists but there is no B→A.
A weight is a number written on an edge telling you its cost — minutes to drive, kilometres, price. We write w ( u , v ) meaning "the weight of the edge going from vertex u to vertex v " (using the placeholders from §2).
Read w ( u , v ) out loud as "the cost of the road from u to v ." The figure below puts a yellow number on every road of the parent's example graph.
Follow the picture: the edge A→C carries the yellow label 1 , so w ( A , C ) = 1 ; the edge A→B carries 4 , so w ( A , B ) = 4 ; and so on for all five arrows. The green note at the top of the figure highlights the punchline we prove next — the two-hop route A→C→B (cost 1 + 2 = 3 ) is cheaper than the single road A→B (cost 4 ).
Intuition Why non-negative? (the heart of the whole topic)
Dijkstra demands w ( u , v ) ≥ 0 — every road costs zero or more , never negative. Picture time: no road can rewind your clock . If a road could give back minutes, then reaching a place slowly now might turn out cheaper later, and "stamp it done forever" would be a lie. That single assumption is what makes the greedy trick valid.
Common mistake "Weight" is not "number of edges"
A path with more edges can still be cheaper if those edges have small weights. In the parent example (and the green note in the figure above), A→C→B (cost 1 + 2 = 3 ) beats A→B (cost 4 ) even though it uses more roads. Always add up weights , never count hops — unless every weight is 1 (that's the BFS special case).
Before we talk about a path , we need to name the one place all journeys begin from.
s
The source s is the single starting vertex — the "home" you measure all distances from . It is another placeholder (like u and v ), but reserved specially for the fixed starting dot. "SSSP" = S ingle-S ource S hortest P ath: one home (s ), shortest routes to everywhere.
In the parent's worked example the source is s = A : every distance is measured starting from A.
A path from the source s (§5) to some vertex v (§2) is a chain of edges that walks you from s to v , e.g. s → a → b → v .
Now — what does it cost to walk a path? We already have w ( u , v ) for one edge. A path is several edges in a row, so we simply add the edge-weights together , one per step.
Worked example Reading a path weight
Path A → C → B → D steps over three edges. Its weight is
w ( A , C ) + w ( C , B ) + w ( B , D ) = 1 + 2 + 1 = 4.
This is exactly the "add up the weights" rule from §4, now written formally. This same addition is what the relaxation rule in §8 uses — d i s t [ u ] + w ( u , v ) is "cost to reach u , plus one more edge."
s ⇝ u — "some path from s to u "
The wiggly arrow ⇝ is NOT a single edge. It means "there exists a path (any chain of edges) leading from s to u — we don't yet say which one." Contrast:
u → v (straight arrow) = one edge, a single road.
s ⇝ u (wiggly arrow) = a whole route , possibly many edges.
Worked example Straight vs. wiggly, side by side
In the figure, A → C is one straight blue arrow (a single edge, cost 1 ). The green thick route is one example of A ⇝ D : the wiggle stands for any of the routes A → C → D (cost 6 ), A → C → B → D (cost 4 ), or A → B → D (cost 5 ). The proof in the parent note writes s ⇝ u precisely because it wants to argue about whichever route is shortest without naming it up front.
Definition True shortest distance
δ ( v )
δ ( v ) (Greek letter delta ) means the honest, final, cannot-be-beaten shortest total weight of any path from s to v . It is the answer — the number we are hunting for.
δ ( v ) = min all paths s ⇝ v ( weight ( P ) )
The min symbol means "take the smallest." So δ ( v ) is the smallest possible path-weight (using the sum from §6) — by definition, no route beats it.
Definition Unreachable vertices:
δ ( v ) = ∞
What if there is no path s ⇝ v at all — the vertex is on a different island of the graph? Then the set of "all paths" is empty , there is nothing to take the minimum of, and we define
δ ( v ) = ∞ ( "you simply cannot get there" ) .
The symbol ∞ (infinity) means "bigger than any real cost." For such a vertex, our running guess never drops below ∞ — Dijkstra correctly leaves it as ∞ , telling you "unreachable."
Here is the subtle one. δ ( v ) is the true answer we don't know yet. d i s t [ v ] is our current best guess , which we improve over time until it equals δ ( v ) .
d i s t [ v ] — best-known-so-far
d i s t [ v ] is a number stored in an array (a row of labelled boxes, one per vertex). It holds the cheapest route to v we have discovered up to now . It starts too big and only ever shrinks. We set d i s t [ v ] = ∞ for every vertex at the start, except the home: d i s t [ s ] = 0 (it costs nothing to already be at the source).
The figure below shows all four boxes of the parent example. The red row is the starting guess (everything ∞ except A = 0 ); the green row is the final answer, where each d i s t [ v ] has shrunk down until it equals δ ( v ) .
Trace the vertical arrows in the picture: box B falls from ∞ to 3 , box C from ∞ to 1 , box D from ∞ to 4 , while box A stays pinned at 0 . Each downward drop is one relaxation , defined next.
Intuition Two symbols, one shrinking toward the other
Think of d i s t [ v ] as a sticky note on each house showing your best time so far, and δ ( v ) as the true fastest time written in a sealed envelope. The whole algorithm is: keep lowering the sticky notes until each one matches its envelope. When Dijkstra "finalizes" a vertex, it has proven d i s t [ v ] = δ ( v ) .
Symbol
Plain words
Changes?
δ ( v )
the true, final shortest distance
never — it's the fixed answer
d i s t [ v ]
our best guess right now
shrinks as we find shorter routes
∞
"no route found yet" (or unreachable forever)
the starting guess
The + here is exactly the path-sum of §6: "cost already paid to reach u " plus "one more edge w ( u , v ) ." The arrow ← means "becomes " — the left box is overwritten with the right value (it is assignment , not equality).
Intuition Why called "relax"?
A stretched-tight guess (d i s t [ v ] too big) gets relaxed down toward the truth δ ( v ) each time we find a cheaper way in. You can only ever loosen it downward, never tighten it up. Each downward drop you traced in the §8 figure is one application of this rule.
Definition Priority queue / min-heap
A priority queue is a magic bucket of items each tagged with a number. Its one talent: instantly hand you the item with the smallest number. A min-heap is the standard fast implementation.
Dijkstra fills this bucket with pairs (dist[v], v) — "guessed cost , vertex " — and always pulls out the vertex with the smallest guessed cost next. That "always grab the nearest unfinished dot" is the greedy heart. See Priority Queue (Binary Heap) for how the bucket works internally.
Intuition Why a heap and not just "scan the array for the min"?
Scanning every vertex to find the minimum costs O ( ∣ V ∣ ) each time — O ( ∣ V ∣ 2 ) total. A heap finds and removes the min in O ( log ∣ V ∣ ) , giving the parent's O (( ∣ V ∣ + ∣ E ∣ ) log ∣ V ∣ ) . The heap is purely a speed engine ; the algorithm would be correct (just slower) with a plain scan.
O ( f ) — Big-O
O ( f ) describes how the running time grows as the input grows, ignoring constants and small terms. O (( ∣ V ∣ + ∣ E ∣ ) log ∣ V ∣ ) means: "the work grows roughly like ( ∣ V ∣ + ∣ E ∣ ) times log ∣ V ∣ ."
log ∣ V ∣ (logarithm) answers "how many times can I halve ∣ V ∣ before reaching 1 ?" — it's the depth of the heap, hence the cost of one pop or push. Tiny even for huge ∣ V ∣ .
( ∣ V ∣ + ∣ E ∣ ) log ∣ V ∣ step by step
Count the heap operations Dijkstra performs.
Pushes: each successful relaxation of an edge pushes one new pair. Every edge u → v is examined when u is popped, and can push at most once per examination, so there are at most ∣ E ∣ pushes.
Pops: every pair that was pushed gets popped once. Starting push plus ≤ ∣ E ∣ relaxation-pushes gives O ( ∣ V ∣ + ∣ E ∣ ) pops. (Since a connected graph has ∣ E ∣ ≥ ∣ V ∣ − 1 , this is O ( ∣ E ∣ ) in practice, but ∣ V ∣ keeps the bound honest for sparse or disconnected graphs.)
Cost per operation: the heap holds at most O ( ∣ E ∣ ) items, and one push or pop on a binary heap costs O ( log ( heap size )) = O ( log ∣ E ∣ ) . Because ∣ E ∣ ≤ ∣ V ∣ 2 , we have log ∣ E ∣ ≤ log ∣ V ∣ 2 = 2 log ∣ V ∣ = O ( log ∣ V ∣ ) — so we write log ∣ V ∣ .
Multiply (number of operations) × (cost each) :
pops + pushes O ( ∣ V ∣ + ∣ E ∣ ) × per operation O ( log ∣ V ∣ ) = O ( ( ∣ V ∣ + ∣ E ∣ ) log ∣ V ∣ ) .
Add the one-time O ( ∣ V ∣ ) to initialise every d i s t [ v ] = ∞ , which is absorbed into the total.
Each foundation feeds the next: dots and lines carry weights; weights summed along a path give the true distance; guesses chase that truth via relaxation; the heap orders the chase; non-negativity makes the greedy order safe. Read the diagram below top to bottom — an arrow "X → Y" means "you need X before Y makes sense."
Path weight = sum of edges
Guess dist v and infinity
Intuition How to read that map in words
If mermaid ever fails to render for you, here is the same chain in prose: a graph gives you both counts (∣ V ∣ , ∣ E ∣ ) and named dots (u , v , s ); weights sum along a path to define the true distance δ ; δ is what our shrinking guess d i s t [ ] (with ∞ ) chases via the relaxation rule ; non-negative weights make the greedy order correct ; the min-heap supplies the ordering; and Big-O counts the cost. All arrows finally point into Dijkstra .
G = ( V , E ) means...A graph made of a set of vertices (dots) V and a set of edges (lines) E .
What is the difference between V and ∣ V ∣ ? V is the set of vertices; ∣ V ∣ is the number of vertices (count the dots). Inside a Big-O, V silently means ∣ V ∣ .
What do the placeholders u and v stand for? Any two arbitrary vertices — blank labels you stick onto whichever dots you're discussing, so a rule can be written once and apply to all.
What special role does s play? The source — the single fixed starting vertex all distances are measured from.
What does w ( u , v ) read as? The weight (cost) of the edge going from vertex u to vertex v .
How do you compute the weight of a whole path? Add up the weights of every edge stepped over: ∑ i w ( v i , v i + 1 ) .
How do you read s ⇝ u versus u → v ? u → v is one single edge; s ⇝ u is "some path" (any chain of edges) from s to u .
Why must w ( u , v ) ≥ 0 ? No road can give back time; so the first cheap arrival is truly final and greedy "stamp done forever" is valid.
Difference between δ ( v ) and d i s t [ v ] ? δ ( v ) is the true, final shortest distance (fixed answer); d i s t [ v ] is our best-known guess that shrinks toward it.
What is δ ( v ) if v is unreachable from s ? ∞ — there is no path, so the minimum is over an empty set and d i s t [ v ] stays ∞ .
What does d i s t [ v ] = ∞ mean at the start? No route to v has been found yet; only d i s t [ s ] = 0 .
What does ← mean in the relaxation rule? "Becomes" — assign the right-hand value into the left-hand box.
What is relaxation in one sentence? Lowering d i s t [ v ] whenever going through u gives a cheaper route than the current guess.
What is a min-heap's one talent? Instantly return (and remove) the item with the smallest tag number.
Why is Dijkstra O (( ∣ V ∣ + ∣ E ∣ ) log ∣ V ∣ ) ? There are O ( ∣ V ∣ + ∣ E ∣ ) heap pops+pushes (one push per relaxed edge), each costing O ( log ∣ V ∣ ) .
The parent topic (Hinglish)
BFS — the all-weights-equal-1 special case of everything above
Priority Queue (Binary Heap) — the min-heap engine defined in §10
Greedy algorithms — why "grab the nearest unfinished dot" is justified
Bellman-Ford algorithm — what to use when §4's non-negative rule is broken