📅  最后修改于: 2023-12-03 15:12:38.253000             🧑  作者: Mango
This is a programming question from the 2018 GATE CS Mock exam. The question requires the implementation of a function that calculates the size of the largest connected component of a specific shape in a binary matrix. The solution to this problem requires the usage of graph traversal algorithms such as Breadth First Search or Depth First Search.
The problem requires the implementation of the function LargestComponent(matrix: List[List[int]]) -> int
which takes in a binary matrix as an input and returns an integer value which represents the size of the largest connected component of the shape shown below.
The shape that needs to be computed in the binary matrix is as follows:
1 1 0
1 0 0
0 0 1
Here is a brief description of the shape:
The problem can be solved using graph traversal algorithms such as BFS or DFS. The approach is as follows:
(i, j)
in the binary matrix that is equal to 1, perform a BFS or DFS traversal and count the number of connected 1's. Store this count in a list.Here is the Python implementation of the solution:
from typing import List
def LargestComponent(matrix: List[List[int]]) -> int:
ROW = len(matrix)
COL = len(matrix[0])
directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
visited = [[False for j in range(COL)] for i in range(ROW)]
max_count = 0
def isValid(row: int, col: int) -> bool:
return (row >= 0 and col >= 0 and row < ROW and col < COL)
def BFS(row: int, col: int) -> int:
queue = [(row, col)]
visited[row][col] = True
count = 1
while queue:
(p, q) = queue.pop(0)
for (dx, dy) in directions:
x = p + dx
y = q + dy
if isValid(x, y) and not visited[x][y] and matrix[x][y] == 1:
visited[x][y] = True
count += 1
queue.append((x, y))
return count
for i in range(ROW):
for j in range(COL):
if not visited[i][j] and matrix[i][j] == 1:
count = BFS(i, j)
max_count = max(max_count, count)
return max_count
The function LargestComponent()
takes the binary matrix as an argument and returns the size of the largest connected component of the shape we are looking for.
The function begins by initializing variables such as the number of rows and columns in the matrix, the directions for the BFS algorithm, a 2D array to keep track of visited cells, and a variable to store the maximum count of 1's in a connected component.
The isValid()
function checks whether a cell is a valid cell to be included in the BFS traversal. It returns True
if the row and column values lie within the bounds of the matrix and False
otherwise.
The BFS()
function performs a Breadth First Search traversal on the binary matrix starting at cell (row, col)
and returns the count of connected 1's.
The main loop of the function iterates over each cell in the binary matrix. If the cell is equal to 1 and has not already been visited, the BFS()
function is called on that cell. The maximum count of 1's in all connected components is stored in max_count
and returned by the function.
This problem required the implementation of a function that finds the size of the largest connected component of a specific shape in a binary matrix. The solution requires the usage of BFS or DFS traversal algorithms to find the number of connected 1's. The Python implementation uses a BFS algorithm to find the count of 1's in all connected components and returns the maximum count as the solution.