3.5.4 · D1Graphs

Foundations — BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs

1,989 words9 min readBack to topic

Before you can believe that idea, you must be fluent in the tiny alphabet it is written in. This page builds every symbol, word, and picture the parent note silently assumes — in the exact order they depend on each other. Nothing is used before it is drawn.

Parent note: BFS (main note).


1. What is a graph? (the dots-and-strings picture)

The picture: think of a metro map. Each station is a vertex (dot); each rail between two stations is an edge (string).

Figure — BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs

Why the topic needs it: BFS is a graph traversal — the whole algorithm is about walking dots along strings. Without the dots-and-strings picture, every later symbol is meaningless.


2. Symbols and — counting the dots and strings

The picture: in the graph above, count the dots → that is ; count the lines → that is .

Why the topic needs it: the parent note's headline speed is . That phrase is nonsense until you know counts dots and counts strings. We build itself in Section 8.


3. Neighbor and degree — who is one string away

The picture: stand on dot . Everyone you can reach in one hop is a neighbor. The count of those people is your degree.

Figure — BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs

Why the topic needs it: BFS, when it "visits" a dot, looks at every neighbor. And the total edge-scanning work is the sum of all degrees — the reason appears in the running time (Section 8).


4. The source and distance

The picture: write a number on each station equal to how many rails you crossed to get there from home. Those numbers form the rings in the core idea.

Figure — BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs

Why the topic needs it: the entire promise of BFS is "I will compute for every ." Every ring in Section 1's intuition is the set of all dots with the same value. The word level = the set of all dots at one distance value.


5. The queue — a line where the first to arrive is the first to leave

The picture: a queue at a ticket counter. Person A arrives first, stands in front; B stands behind A. A is served first.

Figure — BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs

The two operations we use:

  • append(x) — put at the back of the line.
  • popleft() — remove and return the person at the front.

Why the topic needs it — and why a queue, not a stack: we need dots to come out sorted by distance. If we always add the source first, then its ring-1 dots, then ring-2, the FIFO rule serves all of ring 1 before any of ring 2 — because they lined up earlier. That is precisely the "rings" behaviour. A stack (Last-In-First-Out) would serve the newest dot first, diving deep — that gives DFS — depth-first traversal, which does not produce shortest paths.


6. Visited set and parent array — memory tags

The picture: as ripples spread, draw a little arrow on each newly-wet dot pointing back to the ripple that reached it. Follow those arrows all the way back and you retrace the shortest route home. Those arrows form the Shortest Path Tree.

Why the topic needs it: visited guarantees the "each vertex once" rule that makes BFS fast; parent lets us reconstruct the actual path, not just its length (parent note's Example 2).


7. Storing the strings: the adjacency list

The picture: a phone book. Each dot is a name; next to it is the list of its direct contacts.

For the parent's graph 0-1, 0-2, 1-3, 2-3, 3-4:

adj[u]
0 1, 2
1 0, 3
2 0, 3
3 1, 2, 4
4 3

Why the topic needs it: the inner loop for w in adj[u] walks exactly this list. Choosing a list (vs a full grid/matrix) is why edge work totals , not — see Graph Representations — adjacency list vs matrix.


8. Big-O: the symbol

Why it is a sum (not a product): BFS does two separate jobs:

  1. Handle each dot once → about units of work.
  2. Look at each string once → about units of work.

The edge work is (from Section 3), and constants drop inside , leaving . Because the loops run one after another, not nested, we add. A nested "check every string for every dot" would multiply, giving the slower — BFS avoids that by using the adjacency list.


Prerequisite map

Graph = dots and strings

Vertex V and Edge E counts

Neighbor and degree

Adjacency list adj

Big-O of V plus E

Source s and distance d

Queue FIFO

BFS explores in rings

Visited and parent tags

Read it bottom-up: dots-and-strings feed everything; the queue plus distances plus tags plus the adjacency list all pour into the final claim — BFS explores in rings.


Equipment checklist

Cover the right side and answer aloud. If any tag catches you, reread its section.

What is a vertex and what is an edge?
A vertex is a dot (a node); an edge is a string connecting two dots.
What do and stand for?
= number of vertices (dots); = number of edges (strings).
What is a neighbor, and what is ?
A neighbor is a dot one string away; is how many neighbors (strings) has.
Why does in an undirected graph?
Each string touches two dots, so summing degrees counts every string twice.
What does mean, and what is ?
is the fewest edges from source to ; .
Is BFS distance measured in weight or in hops?
In hops — every edge counts as 1; weights are ignored (that is why weighted graphs need Dijkstra).
What rule does a queue follow, and why not a stack?
FIFO (first in, first out), which serves nodes distance-sorted; a stack (LIFO) dives deep and breaks shortest paths.
When and why do we set visited[v]?
At enqueue time, so a dot is never queued twice — preserving the "each vertex once" guarantee.
What does parent[v] store, and what is it for?
The dot that first discovered ; following it back reconstructs the shortest path.
What is adj[u]?
The list of 's neighbors — the phone-book storage the inner loop walks.
Why is BFS an addition, not a multiplication?
Handling dots and scanning strings are two separate loops (each element once), so we add rather than nest and multiply.

Connections

  • Parent: BFS main note
  • DFS — depth-first traversal (the stack/LIFO cousin)
  • Dijkstra's Algorithm (BFS generalized to weighted graphs)
  • 0-1 BFS (deque trick for 0/1 weights)
  • Graph Representations — adjacency list vs matrix (why edge work is )
  • Connected Components · Bipartite Check · Shortest Path Tree