Construct a graph which does not contain any pair of adjacent nodes with same value | Function that prints the edges of the generated graph ; First print connections stored in store [ ] ; Check if there is more than one occurrence of 1 st unique element ; Print all other occurrence of 1 st unique element with second unique element ; Function to construct the graph such that the every adjacent nodes have different value ; Stores pair of edges formed ; Stores first unique occurrence ; Check for the second unique occurrence ; Store indices of 2 nd unique occurrence ; To check if arr has only 1 unique element or not ; Store the connections of all unique elements with Node 1 ; If value at node ( i + 1 ) is same as value at Node 1 then store its indices ; If count is zero then it 's not  possible to construct the graph ; If more than 1 unique element is present ; Print the edges ; Driver Code ; Given array having node values ; Function Call
def printConnections ( store , ind , ind1 ) : NEW_LINE INDENT for pr in store : NEW_LINE INDENT print ( pr [ 0 ] , pr [ 1 ] ) NEW_LINE DEDENT if ( len ( ind ) != 0 ) : NEW_LINE INDENT for x in ind : NEW_LINE INDENT print ( ind1 , x + 1 ) NEW_LINE DEDENT DEDENT DEDENT def constructGraph ( arr , N ) : NEW_LINE INDENT ind = [ ] NEW_LINE store = [ ] NEW_LINE x = arr [ 0 ] NEW_LINE count , ind1 = 0 , 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] != x ) : NEW_LINE INDENT ind1 = i + 1 NEW_LINE count += 1 NEW_LINE store . append ( [ 1 , i + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ind . append ( i ) NEW_LINE DEDENT DEDENT if count == 0 : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Possible " ) NEW_LINE printConnections ( store , ind , ind1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE arr = [ ' a ' , ' b ' , ' a ' , ' b ' , ' c ' ] NEW_LINE constructGraph ( arr , N ) NEW_LINE DEDENT