Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-006

Bipartite Check

Problem

Decide whether an undirected graph with n nodes is bipartite.

A graph is bipartite when its nodes can be 2-colored so that every edge joins different colors. Use BFS coloring from every unvisited node to handle disconnected graphs.

Hint

An odd cycle makes 2-coloring impossible.

Test cases

case 1
in: [4,[[0,1],[1,2],[2,3],[3,0]]]
expected: true
case 2
in: [3,[[0,1],[1,2],[2,0]]]
expected: false
case 3
in: [4,[]]
expected: true
case 4
in: [5,[[0,1],[1,2],[2,0],[3,4]]]
expected: false
Starter codePython
def is_bipartite(n, edges):
    # Your code here
    pass

4 test cases · run your code against them in the editor