Is Graph Bipartite?

Problem:

Intuition

The ideas we perform in normal DFS operation on the graph but with a little twist. When we are performing the DFS operation, we also send an extra parameter called colour.

In this problem will have a three states or three colours zero being uncoloured 1 is colour1 and -1 is colour2.

Send colour 1 to the DFS and we toggle it using a negative symbol inside the DFS function so that the colour changes at every iteration. When we encounter any coloured node which has the same colour as we are trying to colour now then we can safely say that the graph cannot be bipartitioned

Time and Space Complexity

V -> Vertices

E -> Edges

circle-info

Time Complexity: O(V+E)

It is a standard DFS operation

circle-check

Code

Last updated