Skip to content
We areTOP 1%
DSA · Intermediate · Pattern 8 of 10

Graphs: BFS & DFS

17 problems.
Your progress0%
1st Visit
0/17
2nd Visit
0/17
Revise
0/17

About this pattern

A graph is a set of nodes joined by edges; grids, state spaces and number transformations are often graphs in disguise. Depth-first search explores as far as possible before backtracking and suits connectivity, counting components, flood fill and paths in trees. Breadth-first search explores in layers from one or several sources and gives shortest paths when every edge has the same cost.

Signals include connected regions in a grid, processes that spread step by step, and the fewest operations needed to reach a target. Store the graph as an adjacency list, mark a node visited when it is first discovered, and expect O(V + E) time.

// Multi-source BFS on a grid; dist = -1 means unvisited.
int dr[4] = {1, -1, 0, 0}, dc[4] = {0, 0, 1, -1};
vector<vector<int>> dist(R, vector<int>(C, -1));
queue<pair<int, int>> q;
for (auto [r, c] : sources) { dist[r][c] = 0; q.push({r, c}); }
while (!q.empty()) {
    auto [r, c] = q.front(); q.pop();
    for (int d = 0; d < 4; ++d) {
        int nr = r + dr[d], nc = c + dc[d];
        if (nr < 0 || nr >= R || nc < 0 || nc >= C) continue;
        if (dist[nr][nc] != -1 || blocked(nr, nc)) continue;
        dist[nr][nc] = dist[r][c] + 1;
        q.push({nr, nc});
    }
}

Problems

Graphs: BFS & DFS questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Flood FillLeetCodeEasy Solve Flood Fill on LeetCode (opens in a new tab)
2The LakesCodeforcesEasy 1100Solve The Lakes on Codeforces (opens in a new tab)
3Ice SkatingCodeforcesEasy 1200Solve Ice Skating on Codeforces (opens in a new tab)
4Number of IslandsLeetCodeMedium Solve Number of Islands on LeetCode (opens in a new tab)
5Rotting OrangesLeetCodeMedium Solve Rotting Oranges on LeetCode (opens in a new tab)
601 MatrixLeetCodeMedium Solve 01 Matrix on LeetCode (opens in a new tab)
7Surrounded RegionsLeetCodeMedium Solve Surrounded Regions on LeetCode (opens in a new tab)
8Clone GraphLeetCodeMedium Solve Clone Graph on LeetCode (opens in a new tab)
9Is Graph Bipartite?LeetCodeMedium Solve Is Graph Bipartite? on LeetCode (opens in a new tab)
10RumorCodeforcesMedium 1300Solve Rumor on Codeforces (opens in a new tab)
11Mahmoud and Ehab and the bipartitenessCodeforcesMedium 1300Solve Mahmoud and Ehab and the bipartiteness on Codeforces (opens in a new tab)
12Arrow PathCodeforcesMedium 1300Solve Arrow Path on Codeforces (opens in a new tab)
13Two ButtonsCodeforcesMedium 1400Solve Two Buttons on Codeforces (opens in a new tab)
14Pacific Atlantic Water FlowLeetCodeMedium Solve Pacific Atlantic Water Flow on LeetCode (opens in a new tab)
15Kefa and ParkCodeforcesMedium 1500Solve Kefa and Park on Codeforces (opens in a new tab)
16Fox And Two DotsCodeforcesMedium 1500Solve Fox And Two Dots on Codeforces (opens in a new tab)
17Word LadderLeetCodeHard Solve Word Ladder on LeetCode (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account