Graph Fundamentals and Traversals
Lesson 16 of 20Watch BFS flood a five-server network in hops — then send DFS diving down one cable at a time.
Graphs everywhere
A graph is the shape of every network — servers, friendships, road maps. Two traversals, BFS and DFS, walk it in two very different ways.
Nodes + edges
Dots and the lines joining them. Five servers and five cables already make a graph you can traverse.
Adjacency list
One vector of neighbors per node: adj[0] = {1, 2} means node 0 plugs into 1 and 2. Space cost: O(V + E).
BFS — the ripple
A queue drives it: visit the source, then everything 1 hop away, then 2. Nearest first, so hop counts come out shortest.
DFS — the dive
Recursion drives it: follow one edge to the end, backtrack, repeat. The call stack remembers the path for free.
Same price
Both touch every node and edge at most once: O(V + E). Same visit set, very different order.
BFS wavefront
A queue drives the wavefront: dequeued nodes turn amber, then green with their cables.
Five servers, five cables. BFS starts at hub A — the queue holds [A], and A is 0 hops from itself.
BFS control flow
One queue, one question — does this neighbor already have a distance? That is all BFS is.
Walkthrough
BFS with a queue and hop counts — then recursive DFS over the very same network.
1#include <iostream>2#include <vector>3#include <queue>4using namespace std;5int main() {6vector<vector<int>> adj = {7{1, 2}, {0, 3, 4}, {0, 4}, {1}, {1, 2}8};9int n = 5, src = 0;10vector<int> dist(n, -1);11queue<int> q;12dist[src] = 0;13q.push(src);14while (!q.empty()) {15int u = q.front(); q.pop();16cout << u << " ";17for (int v : adj[u])18if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); }19}20cout << "| dist to 4 = " << dist[4] << endl;21return 0;22}
adj is a vector of vectors — row i lists the direct neighbors of node i. Node 0 plugs into 1 and 2.
DFS in motion
Recursion dives to the deepest node, hits a dead end, and backtracks.
Same five-server network, new explorer: DFS dives down one path as far as it can, then backs up. It starts at A.
Predict the output
Adjacency-list arithmetic — degrees and the handshake the edges make.
1#include <iostream>2#include <vector>3using namespace std;45int main() {6vector<vector<int>> adj = {7{1, 2}, {0, 3}, {0, 3, 4}, {1, 2}, {2}8};9int total = 0;10for (int u = 0; u < 5; u++) total += adj[u].size();11cout << adj[2].size() << " neighbors of node 2" << endl;12cout << total / 2 << " cables" << endl;13return 0;14}
Hint: adj[u].size() is the degree of node u. Every cable lives in two lists — one per endpoint — so the degree sum is exactly twice the edge count.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A graph is nodes plus edges; an adjacency list stores neighbors in O(V + E) space.
BFS uses a queue and rings outward — its dist[] values are shortest hop counts.
DFS uses recursion and dives deep, backtracking when a path dead-ends.
Mark nodes seen when they are discovered, not when processed — duplicates die there.
Undirected edges live in both endpoint lists: the degree sum is twice the edge count.
Both traversals touch every node and edge at most once — O(V + E) is the floor for visiting a graph.