A Graph is a non-linear data structure consisting of a finite set of vertices (or nodes) $V$ and a collection of edges $E$ that connect pairs of vertices: $G = (V, E)$. Graphs generalize trees by eliminating the constraints of root nodes, parent-child hierarchies, and cycle-free requirements. Graphs model networks everywhere: social networks (friendships), city road systems, internet web pages (hyperlinks), and dependency packaging systems.
Before writing code, we categorize graphs according to direction, weighting, and cyclic nature:
| Operation / Metric | Adjacency Matrix | Adjacency List (Standard) | Winner |
|---|---|---|---|
| Space Complexity | $O(V^2)$ regardless of edge count | $O(V + E)$ (or $O(V + 2E)$ for undirected) | List for sparse graphs |
| Add Vertex | $O(V^2)$ (reallocate matrix) | $O(1)$ | List |
| Add Edge | $O(1)$ | $O(1)$ | Tie |
| Query Edge (u, v) | $O(1)$ (matrix[u][v] == 1) | $O(\text{deg}(u))$ (search list) | Matrix |
| Iterate Neighbors of u | $O(V)$ (scan entire row) | $O(\text{deg}(u))$ (scan only actual neighbors) | List |
In technical interviews and real-world software, graphs are overwhelmingly sparse ($E \ll V^2$), making the Adjacency List the universal industry choice.
Traversing a graph requires exploring all reachable vertices while maintaining a visited set to prevent infinite loops caused by cycles.
collections.deque).Given an undirected graph with nodes 0, 1, 2, 3, 4 and edges: (0,1), (0,2), (1,3), (2,4). Starting at node 0:
| Step | Current Node | Visited Set | Queue State (Front -> Back) | Action / Neighbors Added |
|---|---|---|---|---|
| Initial | - | {0} | [0] | Start at root 0 |
| 1 | 0 | {0, 1, 2} | [1, 2] | Dequeue 0, add neighbors 1, 2 |
| 2 | 1 | {0, 1, 2, 3} | [2, 3] | Dequeue 1, add neighbor 3 |
| 3 | 2 | {0, 1, 2, 3, 4} | [3, 4] | Dequeue 2, add neighbor 4 |
| 4 | 3 | {0, 1, 2, 3, 4} | [4] | Dequeue 3, no unvisited neighbors |
| 5 | 4 | {0, 1, 2, 3, 4} | [] | Dequeue 4, no unvisited neighbors. Queue empty! |
BFS Traversal Order: [0, 1, 2, 3, 4]
| Algorithm | Time Complexity | Space Complexity | Primary Best-Use Case |
|---|---|---|---|
| BFS (Adj List) | $O(V + E)$ | $O(V)$ | Finding unweighted shortest path; finding nodes within $k$ hops |
| DFS (Adj List) | $O(V + E)$ | $O(V)$ (recursion depth) | Topological sort; backtracking; finding cycles; flood fill |
| BFS (Adj Matrix) | $O(V^2)$ | $O(V)$ | Dense graphs where $E \approx V^2$ |
Why $O(V + E)$? Every vertex is placed into the queue/stack at most once ($O(V)$). For each vertex, we iterate over all its incident edges ($O(E)$ total edges processed across all adjacency lists).
for node in range(V): if node not in visited: bfs(node).visited Check before Queueing: Adding nodes to the BFS queue without marking them visited immediately allows duplicates into the queue, causing catastrophic $O(2^V)$ exponential memory spikes!RecursionError. In production or deep graphs, convert DFS to an explicit stack loop.visited?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.