Graphs model relationships. Cities connected by roads, friends in a social network, dependencies between tasks—all graphs. Mastering graphs means learning to see problems as nodes and edges, then applying the right traversal or algorithm.Analogy: A graph is a map. Nodes are locations; edges are roads connecting them. BFS is like a flood spreading outward from a point—it reaches all nearby locations first, then farther ones. DFS is like exploring a maze by always going deeper into one corridor until you hit a dead end, then backtracking. Both visit every reachable location, but the order is different, and that order matters for the problem you are solving.
Pattern Recognition Signals:
“Connected components” → DFS/BFS + Union-Find
“Shortest path” → BFS (unweighted) or Dijkstra (weighted)
// Unweighted graphint n, m; // n nodes, m edgesvector<vector<int>> adj(n + 1);for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); // Remove for directed graph}// Weighted graphvector<vector<pair<int, int>>> adj(n + 1); // {neighbor, weight}for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w});}
vector<vector<int>> adj(n + 1, vector<int>(n + 1, INF));for (int i = 1; i <= n; i++) adj[i][i] = 0;for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; adj[u][v] = min(adj[u][v], w);}
DFS explores as deep as possible before backtracking. It is the foundation for many graph algorithms: cycle detection, topological sort, SCC, bridges, and more.Time Complexity: O(V + E) — every node is visited once, and every edge is examined once.Space Complexity: O(V) for the visited array and recursion stack (stack depth = longest path from root).
vector<bool> visited(n + 1, false);void dfs(int u) { visited[u] = true; // Process node u here (pre-order) for (int v : adj[u]) { if (!visited[v]) { dfs(v); } } // Process node u here (post-order, useful for topo sort)}// Usagefor (int i = 1; i <= n; i++) { if (!visited[i]) { dfs(i); // Found a new connected component }}
void dfs(int start) { stack<int> st; st.push(start); while (!st.empty()) { int u = st.top(); st.pop(); if (visited[u]) continue; visited[u] = true; for (int v : adj[u]) { if (!visited[v]) { st.push(v); } } }}
Problem: Count connected components or check if two nodes are connected.
int countComponents(int n, vector<vector<int>>& adj) { vector<bool> visited(n + 1, false); int components = 0; for (int i = 1; i <= n; i++) { if (!visited[i]) { dfs(i); // or bfs(i) components++; } } return components;}
bool hasCycle(int u, int parent) { visited[u] = true; for (int v : adj[u]) { if (!visited[v]) { if (hasCycle(v, u)) return true; } else if (v != parent) { return true; // Back edge found } } return false;}
Analogy: Think of DFS as exploring a cave system. White nodes are unexplored rooms. Gray nodes are rooms you are currently inside (on your current path from the entrance). Black nodes are rooms you fully explored and left. If you find a passage to a gray room, you have found a cycle — you can walk in a circle back to where you are.Why three colors instead of two? In directed graphs, visiting an already-visited node does not always mean a cycle. If the node is black (fully processed), the edge goes to a separate branch — no cycle. Only a back edge to a gray (in-progress) node forms a cycle.
// 0 = white (unvisited), 1 = gray (in current DFS path), 2 = black (fully processed)vector<int> color(n + 1, 0);bool hasCycle(int u) { color[u] = 1; // Mark as in-progress for (int v : adj[u]) { if (color[v] == 1) return true; // Back edge to ancestor = CYCLE if (color[v] == 0 && hasCycle(v)) return true; // Unvisited: explore // color[v] == 2: cross/forward edge, no cycle } color[u] = 2; // Fully processed return false;}// IMPORTANT: call for all nodes to handle disconnected components// for (int i = 1; i <= n; i++)// if (color[i] == 0 && hasCycle(i)) { /* cycle found */ }
Analogy: Think of university course prerequisites. You can only take a course when all its prerequisites are done. Kahn’s algorithm starts with courses that have no prerequisites (in-degree 0), “takes” them, and reduces the prerequisite count for dependent courses. When a course’s count drops to zero, it becomes available. If you process all courses this way and some remain, there is a circular dependency — a cycle.
vector<int> topologicalSort(int n, vector<vector<int>>& adj) { // Step 1: Count incoming edges for each node vector<int> inDegree(n + 1, 0); for (int u = 1; u <= n; u++) { for (int v : adj[u]) { inDegree[v]++; } } // Step 2: Start with nodes that have no prerequisites queue<int> q; for (int i = 1; i <= n; i++) { if (inDegree[i] == 0) q.push(i); } // Step 3: Process nodes, "removing" their outgoing edges vector<int> order; while (!q.empty()) { int u = q.front(); q.pop(); order.push_back(u); for (int v : adj[u]) { if (--inDegree[v] == 0) { // All prerequisites of v are done q.push(v); } } } // If not all nodes processed, a cycle prevented it if (order.size() != n) return {}; // Cycle exists return order;}// Time: O(V + E) -- each node and edge processed exactly once
Problem: Can we 2-color the graph such that no edge connects same colors?
bool isBipartite(int n, vector<vector<int>>& adj) { vector<int> color(n + 1, -1); for (int start = 1; start <= n; start++) { if (color[start] != -1) continue; queue<int> q; q.push(start); color[start] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (color[v] == -1) { color[v] = 1 - color[u]; q.push(v); } else if (color[v] == color[u]) { return false; } } } } return true;}
Key Insight: A graph is bipartite if and only if it has no odd-length cycles. This is because in a 2-coloring, every edge alternates colors, and a cycle of odd length would require a color to be adjacent to itself.
Contest tip: Many problems do not explicitly say “bipartite.” Look for signals like “divide into two groups such that all edges go between groups” or “2-colorable.” Also, any tree is bipartite (trees have no cycles at all).
Problem: Find connected regions in a grid (like paint bucket tool).
void floodFill(vector<vector<int>>& grid, int r, int c, int newColor) { int oldColor = grid[r][c]; if (oldColor == newColor) return; int n = grid.size(), m = grid[0].size(); queue<pair<int, int>> q; q.push({r, c}); grid[r][c] = newColor; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int d = 0; d < 4; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == oldColor) { grid[nx][ny] = newColor; q.push({nx, ny}); } } }}
Mistake 1: 0 vs 1 Indexing
Be consistent. In CP, 1-indexing is common for graphs.
vector<vector<int>> adj(n + 1); // 1 to n
Mistake 2: Forgetting to Check All Components
Not all nodes may be reachable from node 1:
for (int i = 1; i <= n; i++) { if (!visited[i]) { dfs(i); }}
Mistake 3: Stack Overflow in DFS
For graphs with 10^5+ nodes, use iterative DFS or increase stack size.
Mistake 4: Revisiting in BFS
Mark visited when pushing to queue, not when popping. If you mark on pop, a node can be pushed multiple times by different neighbors, wasting time and potentially causing O(V^2) behavior on dense graphs.
// WRONG -- node u may be pushed many times before it is poppedint u = q.front(); q.pop();if (visited[u]) continue;visited[u] = true;// CORRECT -- each node is pushed exactly oncefor (int v : adj[u]) { if (!visited[v]) { visited[v] = true; // Mark BEFORE pushing q.push(v); }}