Sub topic 3
Two-Colouring: Bipartiteness as a Colouring Problem
This chapter assumes you know basic graph terms (vertex, edge, path, cycle), can read adjacency lists in Python, and have seen the definition of a bipartite graph from the previous chapter. In this chapter the question "can the vertices be split into two sides?" becomes a colouring question. In that form it is easier to check and easier to turn into an algorithm.
Proper two-colourings
A two-colouring of a graph assigns every vertex one of two colours. We will use the integers 0 and 1. The colouring is proper when no edge joins two vertices of the same colour. Formally, it is a function c: V -> {0, 1} with c(u) != c(v) for every edge (u, v).
A graph is two-colourable if at least one proper two-colouring exists.
Why this is the same as bipartite
A bipartition splits the vertices into sets U and W so that every edge has one endpoint in each set. The two notions translate directly:
| Starting from | Build the other | Why it works |
|---|---|---|
| Bipartition `(U, W)` | Set `c(v) = 0` for `v` in `U`, `c(v) = 1` for `v` in `W` | Every edge crosses from `U` to `W`, so its endpoints get different colours |
| Proper colouring `c` | Let `U` = colour-0 vertices, `W` = colour-1 vertices | No edge joins two vertices of one colour, so every edge crosses between the sets |
The colour classes of a proper two-colouring are the two sides of a bipartition. From here on, "bipartite" and "two-colourable" mean the same thing.
Checking a colouring is easy
Given a proposed colouring, you can check it by looking at every edge once. That takes O(V + E) time on an adjacency list. A proper colouring is therefore a short, checkable proof that a graph is bipartite.
def is_proper_two_colouring(adj, colour):
"""Return True if colour (a list of 0/1) is a proper two-colouring of adj."""
if len(colour) != len(adj):
return False
for u, neighbours in enumerate(adj):
if colour[u] not in (0, 1):
return False
for v in neighbours:
if colour[u] == colour[v]:
return False
return True
square = [[1, 3], [0, 2], [1, 3], [2, 0]]
print(is_proper_two_colouring(square, [0, 1, 0, 1])) # True
print(is_proper_two_colouring(square, [0, 0, 1, 1])) # False: edge (0, 1)Colours are forced, not chosen
Once you colour one vertex, the rule leaves no further choices. Each neighbour must take the opposite colour, then each of their neighbours must take the opposite of that, and so on. Inside a connected graph, a vertex's colour is fixed by the parity of the length of any path from the starting vertex: even length gives the same colour, odd length gives the other.
This has two consequences:
- A connected bipartite graph has exactly two proper two-colourings. One is the swap of the other (replace every 0 with 1 and every 1 with 0).
- If two paths from the start to some vertex have different parities, the forced colours contradict each other and no proper colouring exists. The next chapter shows that this contradiction always comes from an odd cycle.
Common errors
- Forgetting self-loops. An edge
(v, v)requiresc(v) != c(v), which is impossible. Any graph with a self-loop is not bipartite. The checker above rejects it correctly, becausecolour[u] == colour[v]holds whenu == v. - Checking only the edges you walked. A colouring that alternates along one path can still break on an edge that leaves the path. A correct check visits every edge.
- Assuming the colouring is unique. In a graph with several components, each component can be swapped on its own. A graph with
kcomponents has2^kproper colourings, so tests should not expect one particular output. - Treating "few same-colour edges" as nearly bipartite. Bipartiteness has no partial credit. One bad edge is enough to rule it out.
Exercise
A graph has three connected components, and each component is bipartite. How many proper two-colourings does the whole graph have, and why?
Show answer
2^3 = 8. Each connected component has exactly two proper colourings (one and its swap), and the components share no edges, so each can be chosen independently: 2 × 2 × 2 = 8.
Exercise
The colouring [0, 1, 1] is proposed for the path graph adj = [[1], [0, 2], [1]]. Is it proper? If not, give a proper one.
Show answer
No. Edge (1, 2) joins two vertices of colour 1. A proper colouring is [0, 1, 0], or its swap [1, 0, 1].