Given a graph, the task is to find if it has a cycle of odd length or not.

The idea is based on an important fact that a graph does **not** contain a cycle of odd length if and only if it is Bipartite, i.e., it can be colored with two colors.

It is obvious that if a graph has odd length cycle then it cannot be Bipartite. In Bipartite graph there are two sets of vertices such that no vertex in a set is connected with any other vertex of same set). For a cycle of odd length, two vertices must of same set must be connected which contradicts Bipartite definition.

Let us understand converse, if a graph has no odd cycle then it must be Bipartite. Below is a induction based proof of this taken from http://infohost.nmt.edu/~math/faculty/barefoot/Math321Spring98/BipartiteGraphsAndEvenCycles.html

Assume that (X, Y) is a bipartition of *G *and let *C = u _{1}*,

*u*, . . . ,

_{2}*u*be a cycle of

_{k }*G*, where

*u*is in the vertex set X (abbreviated

_{1 }*u*∈ X). If

_{1}*u*X then

_{1 ∈}*u*Y, . . . and, in general,

_{2 ∈ }*u*X and

_{2j+1 ∈}*u*Y. Since C is a cycle,

_{2i ∈}*u*Y, so that

_{k ∈}*k*= 2

*s*for some positive integer

*s.*Therefore cycle

*C*is even.

Assume that graph *G* has no odd cycles. It will be shown that such a graph is bipartite. The proof is induction on the number of edges. The assertion is clearly true for a graph with at most one edge. Assume that every graph with no odd cycles and at most *q* edges is bipartite and let *G* be a graph with *q* + 1 edges and with no odd cycles. Let *e = uv *be an edge of *G* and consider the graph *H = G – uv. *By induction, *H* has a bipartition (X, Y). If *e* has one end in *X* and the other end in *Y* then (*X*, *Y*) is a bipartition of *G*. Hence, assume that *u *and *v* are in *X*. If there were a path, *P*, between *u* and *v* in *H* then the length of *P* would be even. Thus, *P + uv *would be an odd cycle of *G*. Therefore, *u* and *v* must be in lie in differenet “pieces” or components of *H*. Thus, we have:

where *X = X1 & X2 * and *Y = Y1 ∪ Y2*. In this case it is clear that (*X1 ∪ Y2, X2 ∪ Y1) * is a bipartition of *G.*

Therefore we conclude that every graph with no odd cycles is bipartite. One can construct a bipartition as follows:

(1) Choose an arbitrary vertex *x _{0 }* and set

*X*= {

_{0 }*x*}.

_{0}(2) Let

*Y*be the set of all vertices adjacent to

_{0 }*x*and iterate steps 3-4.

_{0 }(3) Let

*X*be the set of vertices not chosen that are adjacent to a vertex of

_{k }*Y*.

_{k-1}(4) Let

*Y*be the set of vertices not chosen that are adjacent to a vertex of

_{k }*X*.

_{k-1}(5) If all vertices of

*G*have been chosen then

*X = X*and

_{0}∪ X_{1}∪ X_{2}∪. . .*Y = Y*

_{0}∪ Y_{1}∪ Y_{2}∪ . . .Below is code to check if a graph has odd cycle or not. The code basically checks whether graph is Bipartite.

## C++

`// C++ program to find out whether a given graph is ` `// Bipartite or not ` `#include <iostream> ` `#include <queue> ` `#define V 4 ` `using` `namespace` `std; ` ` ` `// This function returns true if graph G[V][V] contains ` `// odd cycle, else false ` `bool` `containsOdd(` `int` `G[][V], ` `int` `src) ` `{ ` ` ` `// 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. ` ` ` `int` `colorArr[V]; ` ` ` `for` `(` `int` `i = 0; i < V; ++i) ` ` ` `colorArr[i] = -1; ` ` ` ` ` `// Assign first color to source ` ` ` `colorArr[src] = 1; ` ` ` ` ` `// Create a queue (FIFO) of vertex numbers and ` ` ` `// enqueue source vertex for BFS traversal ` ` ` `queue <` `int` `> q; ` ` ` `q.push(src); ` ` ` ` ` `// Run while there are vertices in queue (Similar to BFS) ` ` ` `while` `(!q.empty()) ` ` ` `{ ` ` ` `// Dequeue a vertex from queue ` ` ` `int` `u = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// Return true if there is a self-loop ` ` ` `if` `(G[u][u] == 1) ` ` ` `return` `true` `; ` ` ` ` ` `// Find all non-colored adjacent vertices ` ` ` `for` `(` `int` `v = 0; v < V; ++v) ` ` ` `{ ` ` ` `// An edge from u to v exists and destination ` ` ` `// v is not colored ` ` ` `if` `(G[u][v] && colorArr[v] == -1) ` ` ` `{ ` ` ` `// Assign alternate color to this adjacent ` ` ` `// v of u ` ` ` `colorArr[v] = 1 - colorArr[u]; ` ` ` `q.push(v); ` ` ` `} ` ` ` ` ` `// An edge from u to v exists and destination ` ` ` `// v is colored with same color as u ` ` ` `else` `if` `(G[u][v] && colorArr[v] == colorArr[u]) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we reach here, then all adjacent ` ` ` `// vertices can be colored with alternate ` ` ` `// color ` ` ` `return` `false` `; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `G[][V] = {{0, 1, 0, 1}, ` ` ` `{1, 0, 1, 0}, ` ` ` `{0, 1, 0, 1}, ` ` ` `{1, 0, 1, 0} ` ` ` `}; ` ` ` ` ` `containsOdd(G, 0) ? cout << ` `"Yes"` `: cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code For Check if a graphs has a cycle ` `// of odd length ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `public` `static` `int` `V =` `4` `; ` ` ` ` ` `// This function returns true if graph G[V][V] ` ` ` `// contains odd cycle, else false ` ` ` `public` `static` `boolean` `containsOdd(` `int` `G[][], ` `int` `src) ` ` ` `{ ` ` ` `// Create a color array to store colors assigned ` ` ` `// to all vertices. 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. ` ` ` `int` `colorArr[] = ` `new` `int` `[V]; ` ` ` `for` `(` `int` `i = ` `0` `; i < V; ++i) ` ` ` `colorArr[i] = -` `1` `; ` ` ` ` ` `// Assign first color to source ` ` ` `colorArr[src] = ` `1` `; ` ` ` ` ` `// Create a queue (FIFO) of vertex numbers and ` ` ` `// enqueue source vertex for BFS traversal ` ` ` `LinkedList<Integer> q = ` `new` `LinkedList<Integer>(); ` ` ` `q.add(src); ` ` ` ` ` `// Run while there are vertices in queue ` ` ` `// (Similar to BFS) ` ` ` `while` `(!q.isEmpty()) ` ` ` `{ ` ` ` `// Dequeue a vertex from queue ` ` ` `int` `u = q.peek(); ` ` ` `q.pop(); ` ` ` ` ` `// Return true if there is a self-loop ` ` ` `if` `(G[u][u] == ` `1` `) ` ` ` `return` `true` `; ` ` ` ` ` `// Find all non-colored adjacent vertices ` ` ` `for` `(` `int` `v = ` `0` `; v < V; ++v) ` ` ` `{ ` ` ` `// An edge from u to v exists and ` ` ` `// destination v is not colored ` ` ` `if` `(G[u][v] == ` `1` `&& colorArr[v] == -` `1` `) ` ` ` `{ ` ` ` `// Assign alternate color to this ` ` ` `// adjacent v of u ` ` ` `colorArr[v] = ` `1` `- colorArr[u]; ` ` ` `q.push(v); ` ` ` `} ` ` ` ` ` `// An edge from u to v exists and ` ` ` `// destination v is colored with same ` ` ` `// color as u ` ` ` `else` `if` `(G[u][v] == ` `1` `&& colorArr[v] == ` ` ` `colorArr[u]) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we reach here, then all adjacent ` ` ` `// vertices can be colored with alternate ` ` ` `// color ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `G[][] = {{` `0` `, ` `1` `, ` `0` `, ` `1` `}, ` ` ` `{` `1` `, ` `0` `, ` `1` `, ` `0` `}, ` ` ` `{` `0` `, ` `1` `, ` `0` `, ` `1` `}, ` ` ` `{` `1` `, ` `0` `, ` `1` `, ` `0` `}}; ` ` ` ` ` `if` `(containsOdd(G, ` `0` `)) ` ` ` `System.out.println(` `"Yes"` `) ; ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

Output :

No

The above algorithm works only if the graph is strongly connected. We can extend it for the cases when graph is not strongly connected (Please refer this for details). In above code, we always start with source 0 and assume that vertices are visited from it. One important observation is a graph with no edges is also Bipiartite. Note that the Bipartite condition says all edges should be from one set to another.

This article is contributed by **Kartik**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

## Recommended Posts:

- Check if there is a cycle with odd weight sum in an undirected graph
- Check if it is possible to reach a number by making jumps of two given length
- Number of Triangles in Directed and Undirected Graphs
- Erdos Renyl Model (for generating Random Graphs)
- Count single node isolated sub-graphs in a disconnected graph
- Hamiltonian Cycle | Backtracking-6
- Coloring a Cycle Graph
- Degree of a Cycle Graph
- Detect Cycle in a Directed Graph
- Detect Cycle in a Directed Graph using BFS
- Detect cycle in an undirected graph using BFS
- Detect cycle in an undirected graph
- Detect Cycle in a directed graph using colors
- Detecting negative cycle using Floyd Warshall
- Total number of Spanning trees in a Cycle Graph