3.5.10 · D1Graphs

Foundations — Dijkstra's algorithm — greedy, priority queue, O((V+E) log V) — no negative edges

3,291 words15 min readBack to topic

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.


1. What is a graph? (, , )

Look at the figure below. Each labelled circle is a vertex. Each arrow between two circles is an edge.

Figure — Dijkstra's algorithm — greedy, priority queue, O((V+E) log V) — no negative edges

1b. Counting the dots and lines — and

The same letters and get reused in two different jobs, and mixing them up trips beginners. Let's separate them cleanly.


2. Naming individual dots — the placeholders and

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 for "some number," graph work uses lowercase letters for "some vertex."


3. Direction — arrows vs. plain lines

An edge can point one way (a one-way street) or both ways (a two-way street). We use the placeholders and from §2 to describe the two endpoints.

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.


4. Weights — putting a cost on each edge ()

Read out loud as "the cost of the road from to ." The figure below puts a yellow number on every road of the parent's example graph.

Figure — Dijkstra's algorithm — greedy, priority queue, O((V+E) log V) — no negative edges

Follow the picture: the edge A→C carries the yellow label , so ; the edge A→B carries , so ; 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 ) is cheaper than the single road A→B (cost ).


5. The source — where every journey starts

Before we talk about a path, we need to name the one place all journeys begin from.

In the parent's worked example the source is : every distance is measured starting from A.


6. A path, and how its weights add up

Now — what does it cost to walk a path? We already have for one edge. A path is several edges in a row, so we simply add the edge-weights together, one per step.

Reading the squiggle

Figure — Dijkstra's algorithm — greedy, priority queue, O((V+E) log V) — no negative edges

7. The true distance , and infinity

The symbol means "take the smallest." So is the smallest possible path-weight (using the sum from §6) — by definition, no route beats it.


8. The tentative distance — our shrinking guess

Here is the subtle one. is the true answer we don't know yet. is our current best guess, which we improve over time until it equals .

The figure below shows all four boxes of the parent example. The red row is the starting guess (everything except ); the green row is the final answer, where each has shrunk down until it equals .

Figure — Dijkstra's algorithm — greedy, priority queue, O((V+E) log V) — no negative edges

Trace the vertical arrows in the picture: box falls from to , box from to , box from to , while box stays pinned at . Each downward drop is one relaxation, defined next.

Symbol Plain words Changes?
the true, final shortest distance never — it's the fixed answer
our best guess right now shrinks as we find shorter routes
"no route found yet" (or unreachable forever) the starting guess

9. Relaxation — the one update rule

The here is exactly the path-sum of §6: "cost already paid to reach " plus "one more edge ." The arrow means "becomes" — the left box is overwritten with the right value (it is assignment, not equality).


10. The priority queue (min-heap)

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.


11. Big-O notation — — and where comes from

(logarithm) answers "how many times can I halve before reaching ?" — it's the depth of the heap, hence the cost of one pop or push. Tiny even for huge .


Prerequisite map

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."

Graph G = V and E

Counts abs V and abs E

Placeholders u and v

Edge weights w u v

Path weight = sum of edges

True distance delta v

Non-negative weights

Source s

Paths s to v

Guess dist v and infinity

Relaxation rule

Greedy correctness

Min-heap priority queue

Dijkstra algorithm

Big-O and log V


Equipment checklist

means...
A graph made of a set of vertices (dots) and a set of edges (lines) .
What is the difference between and ?
is the set of vertices; is the number of vertices (count the dots). Inside a Big-O, silently means .
What do the placeholders and 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 play?
The source — the single fixed starting vertex all distances are measured from.
What does read as?
The weight (cost) of the edge going from vertex to vertex .
How do you compute the weight of a whole path?
Add up the weights of every edge stepped over: .
How do you read versus ?
is one single edge; is "some path" (any chain of edges) from to .
Why must ?
No road can give back time; so the first cheap arrival is truly final and greedy "stamp done forever" is valid.
Difference between and ?
is the true, final shortest distance (fixed answer); is our best-known guess that shrinks toward it.
What is if is unreachable from ?
— there is no path, so the minimum is over an empty set and stays .
What does mean at the start?
No route to has been found yet; only .
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 whenever going through 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 ?
There are heap pops+pushes (one push per relaxed edge), each costing .

Connections

  • 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