## Linkedin Interview Question

Software Engineer / Developerspublic Boolean bipartite() {

Boolean flag = true;

for (Node u : graph.getNodes)

if(u.state == 0)

BDFS(u,1);// first vertex node

return flag;

}

public void Boolean BDFS(Node u,int c) {

u.state = c;

for (Node v: graph.getadjacent())

if (v.state == 0)

BDFS(v,-c);

else if(v.state == c) return false;

}

This can be done using DFS. I'm not sure though if its the most efficient way.

```
// source: google
for all v in V do visited(v) = 0
for all v in V do if (visited(v)==0) DFS(v,0)
DFS(v,p) {
visited(v) = 1
part(v) = p
for all w in adj(v) do {
if (visited(w)==0) {
DFS(w,1-p)
} else {
if (part(v) == part(w)) print "graph not bipartite"
}
}
}
```

We can do this by marking each node with 0 or 1.The first one we mark with 1, then a node in his list of neighbours with 0, then a neighbour of the neighbour with 1 and so until we mark all the nodes(and return true) or we arrive in a node that is 0 and we want to mark it with 1 or viceversa (and we return false).

- bogdan.cebere October 20, 2010