InferenceAI

Enter at least two characters to search.

Start free

Two-Colour a Graph

mediumGraphs25 min25 XP

graph[i] lists the neighbours of node i in an undirected graph (which may be disconnected). Return True if the nodes can be split into two groups so every edge joins nodes from different groups.

Examples

EXAMPLE WALKTHROUGHeven cycle

A guided view of the published example, not a trace of your code.

INPUT[[[1,3],[0,2],[1,3],[0,2]]]
TASKbipartite_check()
EXPECTED OUTPUTtrue

Start with the input values shown in this example.

Example 1 of 3

Input: graph = [[1,3],[0,2],[1,3],[0,2]]

Output: true

Explanation: even cycle

Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]

Output: false

Explanation: triangle

Input: graph = [[]]

Output: true

Explanation: single node

Hint 1

BFS-colour each component with alternating colours; a conflict means an odd cycle.

Topics

DSAGraphsPython