Sub topic 2
What Makes a Graph Bipartite
This chapter assumes you know basic graph terms (vertex, edge, path, cycle) and can read a graph stored as an adjacency list in Python, as covered in the previous chapter.
The definition
A graph is bipartite if you can split its vertices into two sets, call them L and R, so that every edge has one end in L and the other end in R. No edge may join two vertices in the same set. The pair (L, R) is called a bipartition, and L and R are its parts.
Each part is an independent set: a set of vertices with no edges between any two of them. Seen that way, a bipartite graph is one whose vertices can be covered by two independent sets.
The definition only says which edges are forbidden. It does not require any particular edges to exist. A graph with no edges at all is bipartite, because any split works.
Checking a proposed split
Given a split, checking whether it is a valid bipartition is mechanical. Look at every edge and confirm its endpoints fall in different parts.
def is_bipartition(n, adj, left):
"""Return True if (left, rest) is a valid bipartition of the graph."""
left = set(left)
for u in range(n):
for v in adj[u]:
if (u in left) == (v in left):
return False
return True
square = [[1, 3], [0, 2], [1, 3], [2, 0]] # 4-cycle 0-1-2-3-0
triangle = [[1, 2], [0, 2], [0, 1]]
print(is_bipartition(4, square, {0, 2})) # True
print(is_bipartition(4, square, {0, 1})) # False: edge 0-1 inside L
print(is_bipartition(3, triangle, {0})) # False: edge 1-2 inside RChecking a given split is easy. The hard part is finding a split when nobody hands you one, and trying all 2^n splits is hopeless. The next chapters fix this by turning the question into a colouring problem with a linear-time algorithm.
Examples worth recognising
| Graph | Bipartite? | Why |
|---|---|---|
| Even cycle (4, 6, 8, ... vertices) | Yes | Alternate vertices around the cycle |
| Odd cycle (3, 5, 7, ... vertices) | No | Alternating breaks when you get back to the start |
| Any tree | Yes | Put even-depth vertices in L, odd-depth in R |
| Grid graph | Yes | Split by whether row + column is even or odd, like a chessboard |
| Complete bipartite K(m, n) | Yes | Every L vertex joined to every R vertex |
| Complete graph on 3 or more vertices | No | It contains a triangle |
| Graph with a self-loop | No | The loop joins a vertex to itself |
Many real relationships are bipartite by nature: students and courses, jobs and workers, users and the products they rate. Edges only ever join one kind of thing to the other kind.
Common misconceptions
- The parts must be equal in size. They don't. A star with one centre and 100 leaves is bipartite with parts of size 1 and 100.
- Every L vertex must connect to every R vertex. That describes the special case K(m, n). In general, edges between L and R are allowed but not required.
- The bipartition is unique. For a connected bipartite graph it is unique apart from swapping L and R. A disconnected graph has more choices, because you can swap the parts of each component independently. A graph with k components has 2^(k-1) distinct bipartitions.
- One bad split proves the graph is not bipartite. It does not. In the code above,
{0, 1}fails for the square, but{0, 2}succeeds. To show a graph is not bipartite you must rule out every split, and a later chapter shows that an odd cycle does exactly that.
Exercise
The graph has vertices 0 to 5 and edges 0-1, 1-2, 2-3, 3-0, 4-5. Is it bipartite? If so, give a bipartition, and say how many distinct bipartitions exist (counting (L, R) and (R, L) as the same).
Show answer
Yes. One bipartition is L = {0, 2, 4}, R = {1, 3, 5}. The graph has two components (the 4-cycle and the edge 4-5), and each can be flipped independently, so there are 2^(2-1) = 2 distinct bipartitions. The other is L = {0, 2, 5}, R = {1, 3, 4}.
Exercise
Why can no graph containing a triangle be bipartite?
Show answer
Two of the triangle's three vertices must land in the same part. The triangle has an edge between every pair of its vertices, so those two are joined by an edge inside one part, which the definition forbids.