Find the Number of Islands using DFSIn 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. ExampleMatrix: 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 MatrixThe 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):
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:
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) ConclusionIn 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. Next TopicKnapsack-problem-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India