Skip to content
Back to libraryPart 3 · Graphs and dynamic programming

3.1Graph search: BFS and DFS

Before you start

It helps to have read 1.3 Arrays and strings and 2.4 Recursion and backtracking. Overall level: Intermediate.

Practice: Graph search: BFS and DFS

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Reachable stationsAn undirected graph has n stations, numbered 1 to n, joined by m edges. Count the stations reachable from station 1, including station 1 itself.ApplyNot started
  2. 2Minimum hopsAn undirected graph has n stations, numbered 1 to n, joined by m edges. Find the minimum number of edges needed to travel from station 1 to each station. Print -1 for a station that cannot be reached.ConnectNot started
  3. 3Open regionsA grid has n rows and m columns, and each cell is either free or blocked. Count the connected regions of free cells. Two free cells are connected only through a shared side, not diagonally.ReasonNot started
  4. 4Two compatible groupsAn undirected graph has n vertices, numbered 1 to n, and m edges. Decide whether every vertex can be labeled A or B so that every edge joins two vertices with different labels.StretchNot started

Read the lesson