Possible Bipartition

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 label.

In this problem will have a three states or three labels zero being unlabelled, 1 is label1 and -1 is label2.

Send label 1 to the DFS and we toggle it using a negative symbol inside the DFS function so that the label changes at every iteration. When we encounter any labelled node which has the same label as we are trying to label 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