Check if there is a cycle with odd weight sum in an undirected graph | Java program to check if there is a cycle of total odd weight ; 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 @ SuppressWarnings ( " unchecked " ) ; 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 Code ; ' 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 .
import java . io . * ; import java . util . * ; class GFG { static boolean twoColorUtil ( Vector < Integer > [ ] G , int src , int N , int [ ] colorArr ) { colorArr [ src ] = 1 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( src ) ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . poll ( ) ; for ( int v = 0 ; v < G [ u ] . size ( ) ; ++ v ) { if ( colorArr [ G [ u ] . elementAt ( v ) ] == - 1 ) { colorArr [ G [ u ] . elementAt ( v ) ] = 1 - colorArr [ u ] ; q . add ( G [ u ] . elementAt ( v ) ) ; } else if ( colorArr [ G [ u ] . elementAt ( v ) ] == colorArr [ u ] ) return false ; } } return true ; } static boolean twoColor ( Vector < Integer > [ ] G , int N ) { int [ ] colorArr = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; ++ i ) colorArr [ i ] = - 1 ; for ( int i = 1 ; i <= N ; i ++ ) if ( colorArr [ i ] == - 1 ) if ( twoColorUtil ( G , i , N , colorArr ) == false ) return false ; return true ; } static boolean isOddSum ( int [ ] [ ] info , int n , int m ) { Vector < Integer > [ ] G = new Vector [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) G [ i ] = new Vector < > ( ) ; int pseudo = n + 1 ; int pseudo_count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( info [ i ] [ 2 ] % 2 == 1 ) { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( v ) ; G [ v ] . add ( u ) ; } else { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( pseudo ) ; G [ pseudo ] . add ( u ) ; G [ v ] . add ( pseudo ) ; G [ pseudo ] . add ( v ) ; pseudo_count ++ ; pseudo ++ ; } } return twoColor ( G , n + pseudo_count ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 3 ; int [ ] [ ] info = { { 1 , 2 , 12 } , { 2 , 3 , 1 } , { 4 , 3 , 1 } , { 4 , 1 , 20 } } ; if ( isOddSum ( info , n , m ) == true ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } }