Bipartite graphs
A graph is bipartite when its vertices can be split into two sides with every edge going across. Colour with BFS or DFS: give a start vertex one colour and each neighbour the other; an edge between two vertices of the same colour proves it is impossible. That happens exactly when the graph has an odd cycle.
How to recognise it
- Split into two groups so that every edge goes between them.
- Two teams, two colours, or two cliques.
- Odd cycles.
Two-colouring
for s in vertices: if uncoloured: colour s = 1; BFS
for each edge u-v: uncoloured v gets -colour[u]; same colour means not bipartite
Vertices, edges, adjacency lists and the other words used here are on the Structures page.
Start a search from every uncoloured vertex: the graph may be in pieces.
Common mistakes
- Only colouring from vertex 0 in a disconnected graph.
- Forgetting that the two-cliques test is on the complement.
Problems
- Is Graph Bipartite?mediumguide ready
- Two Clique Problemmediumguide ready