3.5.17 · D4Graphs

Exercises — Bipartite graphs — 2-coloring test, bipartite matching — Hopcroft-Karp

2,898 words13 min readBack to topic

Quick symbol refresher, so line one is readable:

  • = the set of vertices (the dots). = how many dots.
  • = the set of edges (the lines). = how many lines.
  • = the two sides of a bipartite graph (Left, Right).
  • = a matching — a chosen set of edges that share no endpoint.
  • = a cycle graph on vertices (a closed chain of dots that returns to the start). Throughout this page a cycle always means such a closed chain; it is not a self-loop (an edge from a vertex to itself).

Level 1 — Recognition

L1.1 — Spot the bipartite graph

For each graph, say bipartite or not, and if bipartite give a valid split.

  • (a) Path .
  • (b) Triangle .
  • (c) Square .

The figure below draws all three: look at panel (b) — the red edge is the one that closes an odd cycle and causes the failure; in panels (a) and (c) every edge is black because no conflict arises.

Figure — Bipartite graphs — 2-coloring test, bipartite matching — Hopcroft-Karp
Recall Solution

The colouring rule. We label each vertex with one of two colours, written and . These are just two arbitrary names for the two colour classes (call them "black/white" if you prefer) — the numbers carry no arithmetic meaning beyond "different from each other." The rule is: fix one start vertex's colour, then every neighbour is forced to the opposite colour via . A clash (same colour on both ends of an edge) means no valid 2-colouring exists.

(a) Colour by walking: . No edge repeats a colour → bipartite. , . A path has no cycle at all, so certainly no odd cycle. (Panel (a): all edges black.)

(b) , then edge forces , but edge has both → clash. The cycle has length 3 (odd)not bipartite. (Panel (b): the red edge is the clash.)

(c) ; edge is vs ✅. Cycle length 4 (even)bipartite. , .


L1.2 — Is this set a valid matching?

Graph edges: . For each candidate, is it a matching?

  • (a)
  • (b)
Recall Solution

(a) Both edges touch → they share an endpointnot a matching.

(b) Endpoints used: — all distinct → valid matching of size .


Level 2 — Application

L2.1 — Run the 2-coloring test on a pentagon

Graph: . Decide bipartiteness by BFS colouring; report the clash edge if any.

In the figure the five vertices are placed on a ring with their forced colours printed beside each. Follow the black edges and watch the colours alternate; the red edge is where the alternation breaks.

Figure — Bipartite graphs — 2-coloring test, bipartite matching — Hopcroft-Karp
Recall Solution

We reuse the colouring rule from L1.1 (labels / are just the two classes). Start . Force: , , , . Now the closing edge : colour, coloursame colour → clash (the red edge in the figure). is an odd cycle (length 5) → not bipartite. It is triangle-free yet still fails — the classic warning.


L2.2 — Kuhn's algorithm by hand

, . Edges: . Find a maximum matching using augmenting paths, starting from the empty matching.

Recall Solution

Kuhn's algorithm runs a DFS from each free left vertex, trying to trace an augmenting path. Process left vertices in order.

  • : free neighbour → match . .
  • : neighbour is taken by . DFS tries to re-route: (matched), then 's other edge ( free). Augmenting path . Flip: , matched; freed. .
  • : free neighbour → match . .

Size . Since and all of is matched, this is a perfect matching — maximum.


Level 3 — Analysis

L3.1 — Count augmenting-path length growth (Hopcroft–Karp phases)

Bipartite graph , , edges . Starting empty, list the shortest augmenting path length at the start of each phase. (Recall: a phase = one BFS layering + its DFS augmentations.)

Recall Solution
  • Phase 1: all lefts free, all rights free. The BFS finds that any single edge to a free right is an augmenting path of length 1 (one unmatched edge — odd, as required). DFS augments ; then 's only edge is used this phase, so the phase yields .
  • Phase 2: free left , free right . BFS finds the shortest augmenting path must bounce through the matched edge: (free) (matched) (free) = length 3. DFS augments → matching .
  • Phase 3: no free left remains → BFS finds nothing → stop.

Length sequence: . It strictly increased by 2 — the Hopcroft–Karp guarantee. Because augmenting paths have odd length (), the shortest length rises by at least 2 each phase. Total phases . ✔


L3.2 — Apply König's theorem

For the graph in L2.2 you found max matching with , so . Compute (a) minimum vertex cover size and (b) maximum independent set size.

Recall Solution

(a) König's Theorem (bipartite only): .

(b) Complement identity: .


Level 4 — Synthesis

L4.1 — Build the layered graph and augment

, . Edges: . Start with matching . Run one Hopcroft–Karp phase (one BFS layering + DFS augment): give the new matching.

The figure shows on the left column and on the right column. The red highlighted chain is the augmenting path that the DFS traces through the BFS layers — read it top to bottom and note it alternates unmatched (), matched (), unmatched ().

Figure — Bipartite graphs — 2-coloring test, bipartite matching — Hopcroft-Karp
Recall Solution

Free lefts: . Free rights: .

  • BFS layers from (dist 0). Unmatched edges out of : at dist 1. is matched (to ) so follow the matched edge at dist 2. Unmatched edges out of : at dist 3; is free → augmenting path found at length 3 (odd ✓).
  • DFS augment along (the red chain in the figure): flip edges. and become matched; freed.
  • New matching: , size (up from ).

All of matched → this is maximum (perfect on the left).


L4.2 — Odd cycle ⟺ certificate of non-bipartiteness

A graph is not bipartite. Explain how the 2-coloring BFS produces an explicit odd cycle as proof, and give it for the pentagon of L2.1.

Recall Solution

When BFS finds a clash on edge with colourcolour, both and were reached from the same start via BFS-tree paths. Walk the tree path , cross edge , walk back . Since share a colour, the two tree paths have the same parity of length; adding the extra edge makes the total cycle length odd. That cycle is the odd-cycle certificate.

For : clash on . Tree path plus edge gives the cycle of length 5 — odd. Certificate delivered.


Level 5 — Mastery

L5.1 — Derive the phase count bound

Prove that Hopcroft–Karp uses at most phases, using: (i) each phase raises the shortest augmenting length by , and (ii) if the shortest augmenting path exceeds then at most augmentations remain.

Recall Solution

Let = shortest augmenting-path length measured in edges. By (i), after phases (it starts at and rises by each phase).

Stage A — phases until . How many phases is that? We need , i.e. . So once reaches , the shortest length has passed . That is at most phases in Stage A.

Berge's lemma (the decomposition we need for Stage B). Let be the current matching and a maximum matching. Consider the symmetric difference = edges in exactly one of the two matchings. Look at the degree of any vertex in this edge set: it touches at most one edge of and at most one edge of , so its degree is at most 2. A graph whose every vertex has degree is a disjoint union of simple paths and cycles. Along any such path or cycle the edges must alternate between and (two -edges can't meet at a vertex, nor two -edges). The cycles have equal counts of each, but every path whose two ends are unmatched-in- is exactly an augmenting path for . So decomposes into vertex-disjoint augmenting paths (plus alternating cycles that don't help). This is Berge's lemma. The number of these augmenting paths equals = the augmentations still owed.

Stage B — the tail, once . Each remaining augmenting path has more than edges, so it visits more than vertices (a chain of edges has distinct vertices ). By Berge's lemma these paths are vertex-disjoint, so they carve the vertices into disjoint groups each of size . You cannot fit more than disjoint groups of that size into vertices. Hence at most augmenting paths remain, and since each phase removes at least one, Stage B needs at most phases.

Sum the two stages. Each phase is one BFS + one DFS sweep , so total work is — the famous Hopcroft–Karp bound.


L5.2 — When to reach for the Blossom algorithm

You are asked for a maximum matching in a general (non-bipartite) graph containing odd cycles. Explain why Hopcroft–Karp's BFS-layer trick can fail, and name the correct tool.

Recall Solution

Hopcroft–Karp relies on the bipartite structure so that alternating BFS layers are consistent: matched/unmatched edges alternate cleanly across the divide. In a graph with odd cycles (blossoms) an augmenting path can enter and leave the same odd cycle, and the naive alternating search may miss it — a vertex can appear in a layer with the "wrong" parity. The fix is to contract each blossom into a single super-vertex, search, then expand. That is the Blossom Algorithm (Edmonds), which handles general graphs in polynomial time. Maximum Flow and König/Hall arguments only give the clean bipartite guarantees.


Recall Self-test summary

Bipartite iff no odd cycle ::: True — 2-coloring succeeds exactly when there is no odd cycle. Augmenting path length parity ::: Always odd (one more unmatched edge than matched). Hopcroft–Karp complexity ::: . König's theorem scope ::: Bipartite graphs only; max matching = min vertex cover. Tool for general-graph matching ::: Blossom (Edmonds) algorithm.