progress

~/problems / Graphs / Bipartite graphs / 2-colouring

Is Graph Bipartite?

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or play this problem's boss fight .

An undirected graph on nodes 0 .. n - 1 is given as an adjacency list: graph[u] lists the neighbours of u (no self-loops, no repeated neighbours, and v in graph[u] exactly when u in graph[v]). The graph may be disconnected.

Write is_bipartite(graph) -> bool: can the nodes be split into two sets so that every edge goes between the sets?

is_bipartite([[1, 3], [0, 2], [1, 3], [0, 2]])        # True   (square: {0, 2} and {1, 3})
is_bipartite([[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]])  # False  (0-1-2 is a triangle)
Two four-node graphs: a square that splits into sides {0, 2} and {1, 3}, and the same square with a diagonal 0-2 that creates the odd triangle 0-1-2.

Graphs have up to 100,000 nodes and 100,000 edges.

⭐ Bonus: speed tests (one is a 100,000-node path) earn a star in O(n + edges) without deep recursion.

Show hint

Once you put one node of a connected piece on a side, every other node in that piece is forced; check whether the forced choices ever clash. (A union-find that puts all of a node's neighbours in one set also works.)

Topic: Bipartite graphs / 2-colouring. BFS/DFS colouring; an odd cycle means not bipartite.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc