BFS, DFS, shortest paths, MST, and topological sort — the graph interview playbook.
Preview — first 3 of 112 cards
Adjacency list
Store neighbors per vertex: list of lists or dict. Space O(V+E). Fast iteration over neighbors; good for sparse graphs. Most common in interviews.
from collections import defaultdict
adj = defaultdict(list)
for u,v in edges:
adj[u].append(v)
adj[v].append(u) # undirected
Adjacency matrix
VxV 2D array; entry [u][v] = weight or 1. O(V^2) space and O(1) edge lookup. Good for dense graphs or Floyd-Warshall. Costly for sparse.
n = len(nodes)
mat = [[0]*n for _ in range(n)]
for u,v,w in edges:
mat[u][v] = w
mat[v][u] = w # undirected
Edge list
List of (u, v, w) tuples. Space O(E). Used by Kruskal (sort edges) and Bellman-Ford (relax all). Not efficient for neighbor iteration.
edges = [(0,1,4),(1,2,3),(0,2,10)]
edges.sort(key=lambda e: e[2])
109 more cards in this sheet
Unlock the DSA & Data Structures bundle — ₹399
One-time payment. Unlocks this sheet and 8 other sheets in the bundle, plus a typeset PDF of each.
Unlock the bundle
See all bundles
Or get all 76 sheets for ₹999 — one payment, every future sheet included.