Largest connected component on a grid | Python3 program to print the largest connected component in a grid ; stores information about which cell are already visited in a particular BFS ; result stores the final result grid ; stores the count of cells in the largest connected component ; Function checks if a cell is valid i . e it is inside the grid and equal to the key ; BFS to find all cells in connection with key = input [ i ] [ j ] ; terminating case for BFS ; x_move and y_move arrays are the possible movements in x or y direction ; checks all four points connected with input [ i ] [ j ] ; called every time before a BFS so that visited array is reset to zero ; If a larger connected component is found this function is called to store information about that component . ; function to print the result ; prints the largest component ; function to calculate the largest connected component ; checking cell to the right ; updating result ; checking cell downwards ; updating result ; Drivers Code ; function to compute the largest connected component in the grid
n = 6 ; NEW_LINE m = 8 ; NEW_LINE visited = [ [ 0 for j in range ( m ) ] for i in range ( n ) ] NEW_LINE result = [ [ 0 for j in range ( m ) ] for i in range ( n ) ] NEW_LINE COUNT = 0 NEW_LINE def is_valid ( x , y , key , input ) : NEW_LINE INDENT if ( x < n and y < m and x >= 0 and y >= 0 ) : NEW_LINE INDENT if ( visited [ x ] [ y ] == 0 and input [ x ] [ y ] == key ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def BFS ( x , y , i , j , input ) : NEW_LINE INDENT global COUNT NEW_LINE if ( x != y ) : NEW_LINE INDENT return ; NEW_LINE DEDENT visited [ i ] [ j ] = 1 ; NEW_LINE COUNT += 1 NEW_LINE x_move = [ 0 , 0 , 1 , - 1 ] NEW_LINE y_move = [ 1 , - 1 , 0 , 0 ] NEW_LINE for u in range ( 4 ) : NEW_LINE INDENT if ( is_valid ( i + y_move [ u ] , j + x_move [ u ] , x , input ) ) : NEW_LINE INDENT BFS ( x , y , i + y_move [ u ] , j + x_move [ u ] , input ) ; NEW_LINE DEDENT DEDENT DEDENT def reset_visited ( ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT visited [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT def reset_result ( key , input ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( visited [ i ] [ j ] != 0 and input [ i ] [ j ] == key ) : NEW_LINE INDENT result [ i ] [ j ] = visited [ i ] [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT result [ i ] [ j ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def print_result ( res ) : NEW_LINE INDENT print ( " The ▁ largest ▁ connected ▁ " + " component ▁ of ▁ the ▁ grid ▁ is ▁ : " + str ( res ) ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( result [ i ] [ j ] != 0 ) : NEW_LINE INDENT print ( result [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' . ▁ ' , end = ' ' ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT def computeLargestConnectedGrid ( input ) : NEW_LINE INDENT global COUNT NEW_LINE current_max = - 10000000000 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT reset_visited ( ) ; NEW_LINE COUNT = 0 ; NEW_LINE if ( j + 1 < m ) : NEW_LINE INDENT BFS ( input [ i ] [ j ] , input [ i ] [ j + 1 ] , i , j , input ) ; NEW_LINE DEDENT if ( COUNT >= current_max ) : NEW_LINE INDENT current_max = COUNT ; NEW_LINE reset_result ( input [ i ] [ j ] , input ) ; NEW_LINE DEDENT reset_visited ( ) ; NEW_LINE COUNT = 0 ; NEW_LINE if ( i + 1 < n ) : NEW_LINE INDENT BFS ( input [ i ] [ j ] , input [ i + 1 ] [ j ] , i , j , input ) ; NEW_LINE DEDENT if ( COUNT >= current_max ) : NEW_LINE INDENT current_max = COUNT ; NEW_LINE reset_result ( input [ i ] [ j ] , input ) ; NEW_LINE DEDENT DEDENT DEDENT print_result ( current_max ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT input = [ [ 1 , 4 , 4 , 4 , 4 , 3 , 3 , 1 ] , [ 2 , 1 , 1 , 4 , 3 , 3 , 1 , 1 ] , [ 3 , 2 , 1 , 1 , 2 , 3 , 2 , 1 ] , [ 3 , 3 , 2 , 1 , 2 , 2 , 2 , 2 ] , [ 3 , 1 , 3 , 1 , 1 , 4 , 4 , 4 ] , [ 1 , 1 , 3 , 1 , 1 , 4 , 4 , 4 ] ] ; NEW_LINE computeLargestConnectedGrid ( input ) ; NEW_LINE DEDENT