Given a weighted and undirected graph, we need to find if a cycle exist in this graph such that the sum of weights of all the edges in that cycle comes out to be odd.
Input : Number of vertices, n = 4, Number of edges, m = 4 Weighted Edges = 1 2 12 2 3 1 4 3 1 4 1 20 Output : No! There is no odd weight cycle in the given graph Input : Number of vertices, n = 5, Number of edges, m = 3 Weighted Edges = 1 2 1 3 2 1 3 1 1 Output : Yes! There is an odd weight cycle in the given graph
The solution is based on the fact that “If a graph has no odd length cycle then it must be Bipartite, i.e., it can be colored with two colors”
The idea is to convert given problem to a simpler problem where we have to just check if there is cycle of odd length or not. To convert, we do following
- Convert all even weight edges into two edges of unit weight.
- Convert all odd weight edges to a single edge of unit weight.
Let’s make an another graph for graph shown above (in example 1)
Here, edges [1 — 2] have be broken in two parts such that [1-pseudo1-2] a pseudo node has been introduced. We are doing this so that each of our even weighted edge is taken into consideration twice while the edge with odd weight is counted only once. Doing this would help us further when we color our cycle. We assign all the edges with weight 1 and then by using 2 color method traverse the whole graph. Now we start coloring our modified graph using two colors only. In a cycle with even number of nodes, when we color it using two colors only, none of the two adjacent edges have the same color. While if we try coloring a cycle having odd number of edges, surely a situation arises where two adjacent edges have the same color. This is our pick! Thus, if we are able to color the modified graph completely using 2 colors only in a way no two adjacent edges get the same color assigned to them then there must be either no cycle in the graph or a cycle with even number of nodes. If any conflict arises while coloring a cycle with 2 colors only, then we have an odd cycle in our graph.
This article is contributed by Parth Trehan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find minimum weight cycle in an undirected graph
- Detect cycle in an undirected graph
- Detect cycle in an undirected graph using BFS
- Shortest cycle in an undirected unweighted graph
- Number of single cycle components in an undirected graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Check if longest connected component forms a palindrome in undirected graph
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Detect cycle in the graph using degrees of nodes of graph
- Karp's minimum mean (or average) weight cycle algorithm
- Convert undirected connected graph to strongly connected directed graph
- Clone an Undirected Graph
- Print all the cycles in an undirected graph
- Connected Components in an undirected graph
- Number of Triangles in an Undirected Graph
- Find k-cores of an undirected graph
- Eulerian Path in undirected graph