Check if there is a cycle with odd weight sum in an undirected graph | This function returns true if the current subpart of the forest is two colorable , else false . ; Assign first color to source ; Create a queue ( FIFO ) of vertex numbers and enqueue source vertex for BFS traversal ; Run while there are vertices in queue ( Similar to BFS ) ; Find all non - colored adjacent vertices ; An edge from u to v exists and destination v is not colored ; Assign alternate color to this adjacent v of u ; An edge from u to v exists and destination v is colored with same color as u ; This function returns true if graph G [ V ] [ V ] is two colorable , else false ; Create a color array to store colors assigned to all veritces . Vertex number is used as index in this array . The value ' - 1' of colorArr [ i ] is used to indicate that no color is assigned to vertex ' i ' . The value 1 is used to indicate first color is assigned and value 0 indicates second color is assigned . ; 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.  ; Returns false if an odd cycle is present else true int info [ ] [ ] is the information about our graph int n is the number of nodes int m is the number of informations given to us ; Declaring adjacency list of a graph Here at max , we can encounter all the edges with even weight thus there will be 1 pseudo node for each edge ; For odd weight edges , we directly add it in our graph ; For even weight edges , we break it ; Entering a pseudo node between u -- - v ; Keeping a record of number of pseudo nodes inserted ; Making a new pseudo node for next time ; We pass number graph G [ ] [ ] and total number of node = actual number of nodes + number of pseudo nodes added . ; Driver function ; ' n ' correspond to number of nodes in our graph while ' m ' correspond to the number of information about this graph . ; This function break the even weighted edges in two parts . Makes the adjacency representation of the graph and sends it for two coloring .
def twoColorUtil ( G , src , N , colorArr ) : NEW_LINE INDENT colorArr [ src ] = 1 NEW_LINE q = [ src ] NEW_LINE while len ( q ) > 0 : NEW_LINE INDENT u = q . pop ( 0 ) NEW_LINE for v in range ( 0 , len ( G [ u ] ) ) : NEW_LINE INDENT if colorArr [ G [ u ] [ v ] ] == - 1 : NEW_LINE INDENT colorArr [ G [ u ] [ v ] ] = 1 - colorArr [ u ] NEW_LINE q . append ( G [ u ] [ v ] ) NEW_LINE DEDENT elif colorArr [ G [ u ] [ v ] ] == colorArr [ u ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def twoColor ( G , N ) : NEW_LINE INDENT colorArr = [ - 1 ] * N NEW_LINE 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 DEDENT def isOddSum ( info , n , m ) : NEW_LINE INDENT G = [ [ ] for i in range ( 2 * n ) ] NEW_LINE pseudo , pseudo_count = n + 1 , 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if info [ i ] [ 2 ] % 2 == 1 : NEW_LINE INDENT u , v = info [ i ] [ 0 ] , info [ i ] [ 1 ] NEW_LINE G [ u ] . append ( v ) NEW_LINE G [ v ] . append ( u ) NEW_LINE DEDENT else : NEW_LINE INDENT u , v = info [ i ] [ 0 ] , info [ i ] [ 1 ] NEW_LINE G [ u ] . append ( pseudo ) NEW_LINE G [ pseudo ] . append ( u ) NEW_LINE G [ v ] . append ( pseudo ) NEW_LINE G [ pseudo ] . append ( v ) NEW_LINE pseudo_count += 1 NEW_LINE pseudo += 1 NEW_LINE DEDENT DEDENT return twoColor ( G , n + pseudo_count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m = 4 , 3 NEW_LINE info = [ [ 1 , 2 , 12 ] , [ 2 , 3 , 1 ] , [ 4 , 3 , 1 ] , [ 4 , 1 , 20 ] ] NEW_LINE if isOddSum ( info , n , m ) == True : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT