I have an exam next week and I found a question that I have difficults to solve:
Given the following:
Input: Simple undirected graph $G(V, E)$.
Output: Find an odd-length cycle in $G$ or show a message that all the cycles are even-length.
Suggest an algorithm to do so.
Can you please give me some ideas to do so? Please don't give me the full answer, just a direction.
Thanks!
If you wanted to find just a cycle, you'd use something like BFS or DFS, marking nodes as "visited" as you touched them. Coming back to a visited node means you've found a cycle.
For your problem, coming back to a visited node whose "edge distance" is odd ("edge distance" being the number of edges in the path you've taken) means you've found an odd-length cycle. Can you think of a way to enhance the label-markings to easily detect this?