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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find minimum weight cycle in an undirected graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Find if there is a path between two vertices in an undirected graph
- Print negative weight cycle in a Directed Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Detect cycle in an undirected graph
- Number of single cycle components in an undirected graph
- Detect cycle in an undirected graph using BFS
- Shortest cycle in an undirected unweighted graph
- Find any simple cycle in an undirected unweighted Graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Convert undirected connected graph to strongly connected directed graph
- Detect cycle in the graph using degrees of nodes of graph
- Karp's minimum mean (or average) weight cycle algorithm
- 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
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Sum of the minimum elements in all connected components of an undirected graph
- Sum of degrees of all nodes of a undirected graph