Given a square matrix mat every element of which is either 0 or 1. A value 1 means connected and 0 means not connected. The task is to find the largest length of a path in the matrix after changing atmost one 0 to 1. A path is a 4-directionally connected group of 1s.
Examples:
Input: mat[][] = {{1, 1}, {1, 0}}
Output: 4
Change the only 0 to 1 and the length of the largest path will be 4.Input: mat[][] = {{1, 1}, {1, 1}}
Output: 4
Naive Approach: The idea is to change each ‘0’ to ‘1’ one by one and do a Depth First Search to find the size of the largest path.
Efficient Approach: In the naive approach, we have checked every ‘0’. However, we can also make this efficient by storing the size of each group, so that we do not have to use depth-first search to repeatedly calculate the same size all over again.
Note: We need to take care when the 0 touches the same group. For example, consider grid = [[0, 1], [1, 1]]. The right and bottom neighbor of the 0 will belong to the same group after changing 0 to 1.
We can solve this problem by keeping track of group Id (or Index), that will be unique for each group.
- For each groups, fill it with value Index and remember its size as an element in the array area[Index] which can be found out with a depth first search..
- Then for each 0, look at the neighboring group IDs and add the area of those groups, and add 1 for the 0 we are toggling. This will give us the answer, and we take the maximum of it form previous answer.
Below is the implementation of the above approach:
Python3
# Python3 implementation of above approach # check if index is within range def neighbors(r, c, N): for nr, nc in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)): if 0 <= nr < N and 0 <= nc < N: yield nr, nc # dfs to calculate length of path def dfs(R, C, index, grid, N): ans = 1 grid[R][C] = index for nr, nc in neighbors(R, C, N): if grid[nr][nc] == 1: ans += dfs(nr, nc, index) return ans # function to return largest possible length of Path def largestPath(grid): N = len(grid) area = {} index = 2 for i in range(N): for j in range(N): if grid[i][j] == 1: area[index] = dfs(i, j, index, grid, N) index += 1 ans = max(area.values() or [0]) for i in range(N): for j in range(N): if grid[i][j] == 0: seen = {grid[nr][nc] for nr, nc in neighbors(i, j, N) if grid[nr][nc] > 1} ans = max(ans, 1 + sum(area[i] for i in seen)) # return maximum possible length return ans # Driver code I = [[1, 0], [0, 1]] # Function call to print answer print(largestPath(I)) # This code is written by # Sanjit_Prasad |
3
Recommended Posts:
- Maximum decimal value path in a binary matrix
- Find Maximum side length of square in a Matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Maximum path sum in matrix
- Maximum sum path in a matrix from top to bottom
- Find if there is a path of more than k length from a source
- Maximum weight path ending at any element of last row in a matrix
- Find length of the longest consecutive path from a given starting character
- Find row number of a binary matrix having maximum number of 1s
- Find whether there is path between two cells in matrix
- Find the longest path in a matrix with given constraints
- Find length of the largest region in Boolean Matrix
- Maximum size rectangle binary sub-matrix with all 1s
- Find maximum length Snake sequence
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



