Skip to contentKEEP THE CRAFTTHE SANCTUARY

Validate a Bipartite Graph

Check whether an undirected graph can be painted with two colors by BFS coloring every part of it.

The problem

Implement is_bipartite(node_count, edges). The nodes are labeled 0 to node_count - 1, and each edge [a, b] connects a and b in both directions. Return True if you can paint every node with one of two colors so that every edge joins two different colors, and False otherwise. The graph may be split into several disconnected parts, and an edge [a, a] from a node to itself can never satisfy the rule. For example, the square 0-1-2-3-0 is bipartite, but the triangle 0-1-2-0 is not.

Starter code

def is_bipartite(node_count, edges):
    pass

Public examples

  1. node_count = 4, edges = [[0, 1], [1, 2], [2, 3], [3, 0]]givesTrue
  2. node_count = 3, edges = [[0, 1], [1, 2], [2, 0]]givesFalse

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.