Check if a Matrix is Bitonic or not | Python3 program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row wise ; Check column wise ; Generate an array consisting of elements of current column ; Driver Code
N = 3 NEW_LINE M = 3 NEW_LINE def checkBitonic ( arr , n ) : NEW_LINE INDENT i , j , f = 0 , 0 , 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( i == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < arr [ j - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT if ( f == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def check ( arr ) : NEW_LINE INDENT f = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not checkBitonic ( arr [ i ] , M ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT i = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = [ 0 ] * N NEW_LINE for j in range ( N ) : NEW_LINE INDENT temp [ j ] = arr [ j ] [ i ] NEW_LINE DEDENT if ( not checkBitonic ( temp , N ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 2 , 6 , 4 ] ] NEW_LINE check ( m ) NEW_LINE DEDENT