Bipartite

A bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint and independent sets U and V such that every edge connects a vertex in U to one in V. Vertex sets U and V are usually called the parts of the graph. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

Bipartite

Is graph bipartite?

785. Is Graph Bipartite? (Medium)

DFS

DFS to jump between two parts and assign part number to nodes

Take each node in the Graph as starting point of DFS.

Assign the nodes to one of two parts, -1 and 1, in an alternating order.

If a node is already assigned to a part, once it's visited again, its part should be the same as the part of the previous node.

BFS

Take each node in the Graph as starting point of BFS.

Assign nodes to part -1 or 1 layer by layer in an alternating order.

If a node is already assigned to a part, once it's visited again, its part should be the same as the part of the previous node.

Problems

Last updated

Was this helpful?