Google Interview Question

Write the code to check if graph is bipartite or not?

Interview Answers

Anonymous

Apr 27, 2015

A graph is bipartite iff it does not contain any odd length cycle. We can use DFS algorithms and whenver there is back edge we need to make an extra assertion whether it create an odd length cycle or not. Simple way to implement is to give visited nodes a label 1 and 2 alternatively i.e if current node has label 1, give label 2 to next neighbour of current node. If back is from node of some label to node with same label then its making odd length cycle.

Anonymous

Jun 30, 2016

u can also use graph coloring to find if it is bipartite or not