Find the Number of Islands using DFS

In this tutorial, we will write a Python program to find the number of islands. We will solve this problem using the various approaches. This problem can be asked in the technical interview. First, let's understand the following problem statement.

In a binary 2D matrix, our task is to determine the count of islands. An island is defined as a collection of adjacent '1's that are connected either horizontally or vertically. For instance, in the matrix below, there are four distinct islands. Our goal is to write the Python program to count the number of such islands within the provided matrix.

Example

Matrix:

Before diving into the solution, it's important to grasp the concept of connected components. This problem is a classic example of "Counting the number of connected components in an undirected graph."

Suppose a graph with several nodes and edges connecting them. Each connected component would be a group of nodes that are all connected to each other, but not connected to nodes outside of that group. So, you might have one group of nodes that forms a connected component, and another group that is separate and forms a different connected component. These components are like isolated clusters within the larger graph.

We can solve this problem using the DFS() on each component. In each DFS() call, a component or a sub-graph is visited. To find the number of connected components in an undirected graph, we can use the Depth-First Search (DFS) and Breadth-First Search (BFS). Each call to the DFS or BFS algorithm corresponds to exploring and marking one connected component. By counting the number of DFS or BFS calls, we determine the total count of connected components within the graph. Both algorithms can be effectively utilized for this purpose.

Note - A group of connected 1s forms islands.

Finding the number of islands using an Additional Matrix

The additional matrix will help to keep track of the visited nodes in the given matrix and perform DFS to find the total number of island.

Following are the steps to find the number of connected components in a binary 2D matrix using Depth-First Search (DFS):

  1. First, we create a boolean matrix visited with the same dimensions as the given matrix and initialize all its elements to "false."
  2. Initialize a variable count to 0, which will be used to store the answer.
  3. Iterate through rows from 0 to ROW and within each row, iterate through columns from 0 to COL.
  4. If the current cell in the given matrix has a value of 1 and has not been visited. Then call the DFS function to explore the connected component.
  5. Define two arrays, "rowNbr" = { -1, -1, -1, 0, 0, 1, 1, 1 } and "colNbr" = { -1, 0, 1, -1, 1, -1, 0, 1 } to represent neighboring cells. After that, we mark the current cell as visited. Then we iterate through all eight neighbors.
  6. If a neighbor is valid (within matrix boundaries) and has not been visited. Recursively call the DFS function on the neighbor.
  7. Increment the count by 1 for each connected component encountered. Finally, return count as the total count of connected components.

Let's understand the following code snippet.

Example -

Output

Number of islands: 3

The time complexity is O(rows * cols), and the space complexity is O(rows * cols), where "rows" and "cols" are the dimensions of the input matrix.

Determining the Count of Islands with Depth-First Search (DFS)

Below are the steps of DFS:

  1. First, we start with a count set to 0, which will store the final answer.
  2. Iterate through rows (from 0 to ROW) and within each row, iterate through columns (from 0 to COL).
  3. If the value of the current cell in the given matrix is 1, increment the count by 1.
  4. Invoke the DFS function for further exploration.
  5. If the cell goes beyond the matrix boundaries or the value in the current cell is 0, return.
  6. Update the value at the current cell to 0 to mark it as visited.
  7. Recursively applies DFS to neighboring cells.
  8. Return the count as a result.

Let's implement the above steps in the code -

Example -

Output

Number of islands: 3

Time complexity: O(n*m)

Auxiliary Space: O(n*m)

Conclusion

In this tutorial, we solved the problem of counting islands in a binary 2D matrix using Depth-First Search (DFS). By applying DFS to explore connected components, we efficiently determined the number of islands. We discussed the concept of connected components and how it relates to the problem.

The code example demonstrated two implementations, one using an additional matrix to track visited nodes and another directly marking visited cells in the original matrix. Both methods yielded accurate results. This approach is useful for various applications, offering a clear and effective solution to island counting in binary matrices.