I want to write an algorithm to find whether a directed circuit whose length is odd exists in a strongly connected digraph.
Can anyone help me how to proceed with this problem???
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.Sign up to join this community
König's Theorem (1936): A graph is 2-colorable iff it has no circuits of odd length.
- A graph is bipartite if and only if it does not contain an odd cycle.
- A graph is bipartite if and only if it is 2-colorable, (i.e. its chromatic number is less than or equal to 2).
So we see we are looking for a 2-colorable graph, or a bipartite graph. These are all the same thing.
A digraph has an odd-length directed cycle if and only if one (or more) of its strong components is nonbipartite (when treated as an undirected graph).
Since your graph is strongly connected, we can treat it as an undirected graph and test for bipartiteness using the regular testing algorithms.
A strongly connected digraph is bipartite if and only if it has no directed cycle of odd length. (See Digrpah thm 2.2.1)
So you can decompose your digraph into strongly connected components (SCC). And for each SCC, run a depth-first-search to see if it is bipartite. If all SCCs are bipartite, then the whole digraph has no cycle of odd length. Otherwise it has one such cycle. This is a linear time algorithm.