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 email@example.com. 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.
- 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
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an 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
- Karp's minimum mean (or average) weight cycle algorithm
- Detect cycle in the graph using degrees of nodes of graph
- Clone an Undirected Graph
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Eulerian Path in undirected graph
- Number of Triangles in an Undirected Graph
- Find k-cores of an undirected graph