Odd cycle witness
Standard · Trace the algorithm · about 8 min
BFS 2-colors this graph from node 0 and stops at a conflict. Nodes and their colors are shown.
Click every node on the odd cycle that makes 2-coloring impossible.
Example 1
Input:
{"n":7,"edges":[[0,1],[0,2],[1,3],[2,4],[3,4],[1,5],[2,6]],"directed":false,"source":0,"positions":[[1.5,0],[0,1.2],[3,1.2],[0.6,2.8],[2.4,2.8],[-1,0.4],[4,0.4]]}
Output:
["G/node:0","G/node:1","G/node:2","G/node:3","G/node:4"]
The cycle 0, 1, 3, 4, 2 has 5 nodes. Nodes 5 and 6 hang off it.
Constraints
- The trace uses the graph shown
Learn the idea first: Bipartite check by 2-coloring, a visual lesson.
More Bipartite check by 2-coloring problems
- Is the graph bipartite? Standard
- Possible bipartition Standard
- Return the 2-coloring Standard