← All Exercises
Check Bipartiteness with BFS Coloring
intermediatepython
Background
Bipartite checks detect odd cycles and underpin matching algorithms, scheduling, and conflict-graph analysis.
Problem
Given n vertices and undirected edges, determine whether the graph is bipartite, meaning its vertices can be split into two groups with no edge inside a group.
Input
An integer n and a list of [u, v] undirected edges with vertex labels from 0 to n-1.
Output
True when the graph is bipartite, otherwise False; an empty graph is bipartite.
Sample tests
Sample 1
Input: n = 4, edges = [[0,1],[1,2],[2,3],[3,0]]
Expected: True
Sample 2
Input: n = 3, edges = [[0,1],[1,2],[2,0]]
Expected: False
Sample 3
Input: n = 4, edges = [[0,1],[2,3]]
Expected: True
Your solution
Learning objective
Implement two-coloring with BFS and detect odd cycles through same-color edges.