Graph Theory37 sections · 1633 units
Open in Course

LeetCode 785 Is Graph Bipartite? - Algorithm

Step by step

Here is the full algorithm:

1.1. Create color array of size nn, all initialized to 1-1.

2.2. For each node ii from 00 to n1n-1: if color[i] == -1, start BFS from ii.

3.3. In BFS: color start node 00, add to queue.

While queue not empty: pop node, check all neighbors. If neighbor uncolored, give opposite color, add to queue. If neighbor has same color, return false.

4.4. If all BFS complete without conflict, return true.

This runs in O(V+E)O(V + E) time and uses O(V)O(V) space.