Bipartite graphs
When every edge of a network runs between two kinds of things, students and projects, applicants and jobs, what does that force on the network, and when can every student get a project of their own?
Where you are 10 in Introductory graph theory
Why this exists
Many networks join two kinds of things and never two of one kind: students to the projects they would accept, applicants to the jobs they qualify for. Such a graph can be drawn with one kind in a top row and the other below, every edge running between the rows. That one feature decides a great deal: two colors suffice, there is no cycle of odd length, there are at most \displaystyle \frac{n^2}{4} edges, and there is the question of matching: can each student get a project of their own?
Definition
Graphs are finite and simple, in the notation of Degree, the handshake lemma and complete graphs and Walks, paths, cycles and connected graphs (walks, cycles, components, the distance d(u, v), the cycle graph C_n). Colorings and \chi(G) are as in Graph coloring, and the grids and cube graphs as in Hamiltonian paths and cycles.
- G = (V, E) is bipartite if V is the union of two disjoint sets X and Y, either of which may be empty, such that every edge has one end in X and the other in Y. The pair (X, Y) is a bipartition and X, Y are its parts.
- For m, n \ge 1 the complete bipartite graph K_{m,n} has parts of sizes m and n, every vertex of one part joined to every vertex of the other, and no other edges.
- The m \times n grid has vertices (i, j) with 1 \le i \le m, 1 \le j \le n, two adjacent when they agree in one coordinate and differ by 1 in the other. For n \ge 1 the cube graph Q_n has the 2^n strings of 0s and 1s of length n as vertices, two adjacent when they differ in exactly one place.
- A matching is a set M of edges no two of which share an end. M covers each end of its edges. A matching is perfect if it covers every vertex.
- For a set S of vertices, N(S) is the set of vertices adjacent to at least one vertex of S. In a bipartite graph with parts X and Y, Hall’s condition for X is
Intuition
Sevara runs a capstone seminar. Five students s_1, \ldots, s_5 each list the projects A, \ldots, E they would accept, and each student is joined to the projects on their list. Every edge runs from a student to a project: the graph is bipartite, students in X, projects in Y.
Walk along edges and you change rows at every step, so a walk back to the start changes rows an even number of times, and every cycle is even. The surprise is the converse: a graph with no odd cycle can always be split into two rows. Sevara wants each student on a project of their own, a matching covering X. If three students listed only two projects between them, nothing could work; Hall’s theorem says that kind of shortage is the only obstacle.
Results
1. Two colors. G is bipartite exactly when \chi(G) \le 2. The parts of a bipartition are the color classes of a proper coloring with two colors.
2. Complete bipartite graphs. K_{m,n} has m + n vertices and mn edges. Each vertex of the part of size m has degree n, and each vertex of the other part degree m.
3. Degrees on each side. If G is bipartite with parts X and Y, then
4. The odd cycle test. A graph is bipartite exactly when it has no cycle of odd length.
5. Families. Every tree and every forest is bipartite, C_n is bipartite exactly when n is even, and every grid and every cube graph Q_n is bipartite. A graph with a triangle is not.
6. The edge bound. A bipartite graph with n vertices has at most \displaystyle \left\lfloor \frac{n^2}{4} \right\rfloor edges, and K_{\lfloor n/2 \rfloor, \lceil n/2 \rceil} has exactly that many (for n \ge 2).
7. Hall’s condition is needed. If a bipartite graph with parts X and Y has a matching that covers every vertex of X, then X satisfies Hall’s condition.
8. Hall’s theorem. Conversely, if X satisfies Hall’s condition, then some matching covers every vertex of X. Philip Hall proved it in 1935. It is stated here without proof.
Why it is true
Result 1. Proof. Given a bipartition (X, Y), color X with 1 and Y with 2: every edge has one end of each color, so the coloring is proper. Given a proper coloring with colors 1 and 2, let X be the vertices colored 1 and Y the rest. No edge joins two vertices of one color, so every edge runs between X and Y. \square
Result 2. Proof. A vertex of the part of size m is joined to the n vertices of the other part and to nothing else, so it has degree n; likewise the others have degree m. Each of the m vertices meets n edges, and each edge has exactly one end in that part, so there are m \cdot n edges. \square
Result 3. Proof. \sum_{x \in X} \deg(x) counts the pairs (a vertex x \in X, an edge at x). Every edge has exactly one end in X, so it is counted exactly once. The same holds for Y. \square
Result 4. Proof. Suppose G is bipartite and (v_0, v_1, \ldots, v_k = v_0) is a cycle. Each edge changes part, so by induction on i, v_i lies in the part of v_0 exactly when i is even. Since v_k = v_0, k is even.
Now suppose G has no odd cycle. In each component H choose a vertex r_H, and put a vertex v of H in X when d(r_H, v) is even and in Y when it is odd; the distance is finite, since H is connected. An edge uv has both ends in one component H. Suppose a = d(r_H, u) and b = d(r_H, v) had the same parity. A shortest walk from r_H to u, then the edge uv, then a shortest walk from v back to r_H, is a closed walk of length a + b + 1, which is odd because a + b is even. By Result 5 of Walks, paths, cycles and connected graphs it contains an odd cycle, which is impossible. So every edge joins an even distance to an odd one, and (X, Y) is a bipartition. \square
Result 5. Proof. A forest has no cycle at all, so Result 4 applies; in a tree, the parity of the level gives the parts directly (Trees, Result 10). C_n is a cycle of length n, so for odd n it is not bipartite, and for even n it is 2-colorable (Graph coloring, Result 3). In a grid, put (i, j) in X when i + j is even: an edge changes one coordinate by 1 and so the parity of i + j. In Q_n, put a string in X when it has an even number of 1s: changing one place changes the number of 1s by one. A triangle is an odd cycle. \square
Result 6. Proof. Let the parts have a and n - a vertices. Each edge is a pair (a vertex of X, a vertex of Y) and different edges give different pairs, so
and \lvert E \rvert is an integer. For n = 2k, K_{k,k} has \displaystyle k^2 = \frac{n^2}{4} edges; for n = 2k + 1, K_{k,k+1} has \displaystyle k^2 + k = \left\lfloor k^2 + k + \frac{1}{4} \right\rfloor edges, which is \displaystyle \left\lfloor \frac{n^2}{4} \right\rfloor. \square
Result 7. Proof. Let M cover X, and call the other end of the edge of M at x the partner of x. For S \subseteq X, the partners of the vertices of S lie in N(S), and no two edges of M share an end, so they are \lvert S \rvert different vertices. \square
Worked examples
Testing by layers
The graph G has vertices a to h and edges ab, ac, bd, be, cd, cf, dh, eg, eh, fg. Is it bipartite? If so, find the parts and check Result 3.
SolutionSort the vertices into layers by distance from a, as in Walks, paths, cycles and connected graphs: layer 0 is \{a\}, and layer k + 1 is every vertex adjacent to layer k and not yet placed. Layer 1 is \{b, c\}, layer 2 is \{d, e, f\}, and from these h (by dh) and g (by eg) are new, so layer 3 is \{g, h\}.
By the triangle inequality for distance, the ends of an edge lie in one layer or in neighboring layers. None of the ten edges joins two vertices of one layer, so, as in the proof of Result 4, the even layers X = \{a, d, e, f\} and the odd layers Y = \{b, c, g, h\} form a bipartition (Figure 2).
Degrees in X: 2, 3, 3, 2 for a, d, e, f, sum 10. In Y: 3, 3, 2, 2 for b, c, g, h, sum 10. Both equal the number of edges.
When the layers find an odd cycle
The graph H has vertices 1 to 7 and edges 12, 13, 24, 35, 46, 47, 56, 57, 67. Is it bipartite?
SolutionLayers from 1: \{1\}, \{2, 3\}, \{4, 5\}, \{6, 7\}. The edge 67 joins two vertices of layer 3, and the proof of Result 4 turns this into an odd cycle. Close up shortest walks to the two ends, (1, 2, 4, 6) and (1, 3, 5, 7), with the edge 67:
a closed walk of length 3 + 1 + 3 = 7 that repeats no vertex, so a cycle of length 7. With (1, 2, 4, 7) instead, the closed walk (1, 2, 4, 6, 7, 4, 2, 1) repeats 4, and cutting at the repeat leaves the odd cycle (4, 6, 7, 4). Either way, by Result 4, H is not bipartite, whatever split one tries.
Counting edges from one side
Next term, each of 7 students in Sevara’s seminar lists exactly 3 of 6 projects. Show that some project is on at least 4 lists, and that the projects cannot all be on the same number of lists.
SolutionBy Result 3 on the student side there are 7 \cdot 3 = 21 edges, so the six project degrees also add to 21. If every project were on at most 3 lists, the sum would be at most 6 \cdot 3 = 18 < 21. If all six were on k lists, then 6k = 21, which has no whole-number solution.
The cube graphs
Find the sizes of the parts of Q_n given in Result 5, and the number of edges of Q_n.
SolutionChanging the first place of a string turns a string with an even number of 1s into one with an odd number and back, and doing it twice returns the string. So it matches the even strings one to one with the odd ones (Matching two sets one to one), and each part has \displaystyle \frac{2^n}{2} = 2^{n-1} vertices. Every vertex has degree n, one neighbor per place, so by Result 3 on the even part,
Check: Q_3, the corners and edges of a cube, has 3 \cdot 4 = 12 edges.
Too many edges for two sides
A graph has 11 vertices and 31 edges. Show that it has an odd cycle. Would 30 edges force one?
SolutionBy Result 6 a bipartite graph on 11 vertices has at most \displaystyle \left\lfloor \frac{121}{4} \right\rfloor = 30 edges. With 31 the graph is not bipartite, so by Result 4 it has an odd cycle. Thirty edges do not force one: K_{5,6} has 5 \cdot 6 = 30 edges and no odd cycle.
A shortage of projects
Sevara’s five students list these projects (Figure 1): s_1: A, B; s_2: B; s_3: A, B; s_4: C, D, E; s_5: A, D. Can every student get a project of their own? What is the largest number who can? If s_3 adds C to their list, can everyone?
SolutionTake S = \{s_1, s_2, s_3\}. Then N(S) = \{A, B\} and \lvert N(S) \rvert = 2 < 3 = \lvert S \rvert, so by Result 7 no matching covers all five.
A matching gives the students of S different projects from \{A, B\}, so it covers at most 2 of them, and at most 2 + 2 = 4 students in all. The thick edges of Figure 1, s_1 A, s_2 B, s_4 C, s_5 D, cover four, so four is the largest number.
With C added for s_3, a perfect matching exists: s_1 A, s_2 B, s_3 C, s_4 E, s_5 D.
Boundaries
- Simple graphs. A loop has both ends in one part, so a graph with a loop is never bipartite. Result 6 counts edges of a simple graph.
- Parts are not always unique. A connected bipartite graph has one bipartition, up to swapping X and Y (Exercise 12). A disconnected one has a choice in each component.
- No odd cycle, not just no triangle. C_5 has no triangle and is not bipartite.
- One side at a time. Hall’s condition for X promises a matching covering X, not Y (Exercise 11). Checking it directly means all 2^{\lvert X \rvert} sets S, so large graphs are searched instead, by network flow methods beyond this lesson.
Where this goes next
A bipartite graph is a table of zeros and ones with a row for each vertex of X and a column for each vertex of Y, much like a two-way table. Hall’s theorem leads to the theory of matchings and to network flows, where a largest matching becomes a largest flow. Odd cycles return in Adjacency matrices and their powers: a graph with no odd cycle has no closed walk of odd length.
Exercises
Bipartite or not, and counting
- The graph with vertices a to g has edges ab, ag, bc, bf, cd, de, ef, fg. Test it by layers from a. If it is not bipartite, find an odd cycle.
- The prism P_n (n \ge 3) has vertices 0, \ldots, n - 1 and 0', \ldots, (n-1)', with i joined to i + 1 and i' to (i + 1)' (modulo n) and each i joined to i'. For which n is P_n bipartite?
- K_{m,n} with m \le n has 11 vertices and 24 edges. Find m and n.
- How many cycles of length 4 does K_{m,n} have? Evaluate it for K_{3,4}.
- A bipartite graph has a part X of 6 vertices, each of degree 4, and every vertex of the other part Y has degree 3. How many vertices are in Y, and how many edges are there?
- Is there a bipartite graph with parts of 5 and 7 vertices in which every vertex has degree 3?
- What is the largest number of edges of a bipartite graph with 13 vertices, and which graph has that many?
- A bipartite graph with 12 vertices has 35 edges. What sizes can its two parts have?
Matchings
- Four students list projects: s_1: P, Q; s_2: Q, R; s_3: P; s_4: R, T. Find a matching that covers every student, and show it is the only one.
- Six students list projects: s_1: A, B, C; s_2: A, B; s_3: B, C; s_4: A, C; s_5: D, E, F; s_6: D, F. Is there a matching covering every student? Find the largest number of students a matching can cover.
- Give a bipartite graph whose part X satisfies Hall’s condition while its part Y does not.
Reasoning
- Prove that a connected bipartite graph has exactly one bipartition, up to swapping the two parts.
- Prove that a bipartite graph with n \ge 2 vertices and exactly \displaystyle \left\lfloor \frac{n^2}{4} \right\rfloor edges is K_{\lfloor n/2 \rfloor, \lceil n/2 \rceil}.
- For n \ge 2, join two different numbers i, j in \{1, \ldots, n\} when i + j is prime. Prove that this graph is bipartite.
- Disprove: if every vertex of a graph lies on a cycle of even length, the graph is bipartite.
- In a bipartite graph with parts X and Y, every vertex of X has degree at least d and every vertex of Y degree at most d, where d \ge 1. Use Hall’s theorem to prove that some matching covers X.
- Prove that K_{n,n} has exactly n! perfect matchings.
Answers
- Not bipartite; (b, c, d, e, f, b) is a cycle of length 5. Layers: \{a\}, \{b, g\}, \{c, f\}, \{d, e\}, and de joins two vertices of layer 3. Closing (a, b, c, d) and (a, g, f, e) with de gives the 7-cycle (a, b, c, d, e, f, g, a).
- Exactly when n is even. For odd n, (0, 1, \ldots, n - 1, 0) is an odd cycle. For even n, put i in X when i is even and i' in X when i is odd: the edges i(i + 1) and i'(i + 1)' change parity (from n - 1 to 0 too, as n is even), and ii' runs between parts.
- m = 3, n = 8. They are the roots of t^2 - 11t + 24 = (t - 3)(t - 8).
- \binom{m}{2}\binom{n}{2}, which is 3 \cdot 6 = 18 for K_{3,4}. A 4-cycle alternates parts, so it uses two vertices x, x' of one part and two y, y' of the other, and each such choice gives exactly one cycle, (x, y, x', y', x).
- 8 vertices in Y and 24 edges. By Result 3, \lvert E \rvert = 6 \cdot 4 = 24 = 3\lvert Y \rvert.
- No. Result 3 would give both 5 \cdot 3 = 15 edges and 7 \cdot 3 = 21 edges.
- 42 edges, by K_{6,7}. \displaystyle \left\lfloor \frac{169}{4} \right\rfloor = 42 = 6 \cdot 7, and by Exercise 13 no other bipartite graph on 13 vertices has 42.
- 5 and 7, or 6 and 6. Parts a and 12 - a need a(12 - a) \ge 35: 5 \cdot 7 = 35 and 6 \cdot 6 = 36 work, but 4 \cdot 8 = 32 does not.
- s_1 Q, s_2 R, s_3 P, s_4 T. s_3 must take P, so s_1 must take Q, so s_2 must take R, so s_4 must take T: every step was forced.
- No; at most 5. S = \{s_1, s_2, s_3, s_4\} has N(S) = \{A, B, C\}, so at most 3 of them are covered. s_1 A, s_2 B, s_3 C, s_5 E, s_6 D covers five.
- X = \{x\}, Y = \{y_1, y_2\}, edges xy_1, xy_2. \{x\} has 2 neighbors, but N(Y) = \{x\} has 1 < 2 vertices.
- Let (X, Y) be a bipartition and r \in X (swap names if needed). For any v, a path of length d(r, v) from r to v changes part at every step, so v \in X exactly when d(r, v) is even. So X and Y are fixed by r alone.
- In the proof of Result 6, equality needs \displaystyle a(n - a) = \left\lfloor \frac{n^2}{4} \right\rfloor, so \displaystyle \left(a - \frac{n}{2}\right)^2 < 1 (any other a gives at least 1 for even n, at least \displaystyle \frac{9}{4} for odd n). That forces a = \lfloor n/2 \rfloor or \lceil n/2 \rceil, and \lvert E \rvert = a(n - a) means every pair (a vertex of X, a vertex of Y) is an edge.
- For i \ne j, i + j \ge 3, so a prime i + j is odd and i, j have opposite parity. The even numbers and the odd numbers are a bipartition.
- K_4 on 1, 2, 3, 4: the 4-cycle (1, 2, 3, 4, 1) passes through every vertex, but (1, 2, 3, 1) is a triangle, so by Result 4 K_4 is not bipartite.
- For S \subseteq X, at least d\lvert S \rvert edges have an end in S, and each has its other end in N(S). The vertices of N(S) are ends of at most d\lvert N(S) \rvert edges, so d\lvert S \rvert \le d\lvert N(S) \rvert and \lvert N(S) \rvert \ge \lvert S \rvert. Hall’s theorem gives the matching.
- A perfect matching of K_{n,n} gives each of x_1, \ldots, x_n a different partner in Y, and every such choice is a perfect matching, since all pairs are edges. The partner of x_1 can be chosen in n ways, of x_2 in n - 1, and so on: n! in all.