As we are dealing with graph , the input might come as a forest , thus start coloring from a node and if true is returned we 'll know that   we successfully colored the subpart of our   forest and we start coloring again from a new   uncolored node. This way we cover the entire forest.
for i in range ( N ) : NEW_LINE INDENT if colorArr [ i ] == - 1 : NEW_LINE INDENT if twoColorUtil ( G , i , N , colorArr ) == False : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT