Worked examples — Bipartite graphs — 2-coloring test, bipartite matching — Hopcroft-Karp
This page is a drill sheet. The parent topic note built the theory; here we walk every kind of input a problem can hand you and solve it fully. Before each answer, you forecast — guess the result, then check yourself.
The scenario matrix
Every question this topic throws at you is one of these cells:
| # | Engine | Case class | What makes it tricky | Example |
|---|---|---|---|---|
| A | 2-coloring | Even cycle | Should succeed — clean split | Ex 1 |
| B | 2-coloring | Odd cycle (triangle) | Must fail — the base non-bipartite shape | Ex 2 |
| C | 2-coloring | Odd cycle, length > 3 (pentagon) | Triangle-free but still fails | Ex 3 |
| D | 2-coloring | Disconnected graph | Must restart BFS per component | Ex 4 |
| E | 2-coloring | Degenerate: no edges / single vertex | Trivially bipartite | Ex 5 |
| F | Matching | Greedy gets stuck → augmenting path needed | Greedy ≠ maximum | Ex 6 |
| G | Matching | Hopcroft–Karp phase growth | Shortest path length strictly increases | Ex 7 |
| H | Matching | König twist: min vertex cover / max independent set | Derive from matching | Ex 8 |
| I | Word problem | Workers ↔ jobs assignment | Model then solve | Ex 9 |
| J | Exam twist | "No triangle ⟹ bipartite?" trap | Spot the false claim | Ex 10 |
Cells A–E exhaust the coloring side (even, odd-small, odd-large, disconnected, degenerate). Cells F–H exhaust the matching side (the greedy trap, the phase mechanics, the König bonus). I and J are the real-world / exam wrappers. Nothing else can appear.
Example 1 — Cell A: an even cycle (square)
Forecast: even cycle → your gut should say bipartite, split into alternate corners.

- Set . Why this step? Every component needs one arbitrary seed; the rest is forced. Here is an array holding or (our two paints), or for "not yet painted".
- Force neighbors of 1. Vertices and get . Why? The rule makes every edge cross between colors.
- From 2, paint 3. . Why? Same forcing rule, one hop further.
- Check the closing edge . , — different, no clash. Why does it close cleanly? Look at the figure: walking the ring returns to a -neighbor of the start. Length 4 is even, so colors line up.
Verify: Groups (color 0), (color 1). Every edge crosses . ✅ Bipartite.
Example 2 — Cell B: an odd cycle (triangle)
Forecast: triangle = smallest odd cycle → predict not bipartite.

- . Why? Seed the component.
- (forced opposite along edge ). Why? The deterministic rule.
- (forced opposite along edge ). Why? One more hop, .
- Check closing edge . Both are → clash. Why must it clash? The cycle has 3 vertices; the pattern makes the last vertex the same color as the start, but they are joined by an edge. Odd length forces this collision.
Verify: An edge with equal endpoints () means no valid 2-coloring exists. ❌ Not bipartite — consistent with "odd cycle ⟹ not bipartite".
Example 3 — Cell C: an odd cycle longer than 3 (pentagon)
Forecast: many people say "no triangle → bipartite". Resist! It's an odd cycle. Predict not bipartite.

- Paint around the ring: for vertices . Why? Keep forcing opposite along each edge.
- Reach vertex 5: color . Why? It's the 5th vertex, an even number of hops (4) from vertex 1, so same color as vertex 1.
- Check closing edge . Both → clash. Why? Length 5 is odd; the alternating pattern cannot return consistently. The figure shows the "stuck" vertex glowing red where two 0's meet.
Verify: Clash exists → ❌ not bipartite, even though there is no triangle. This kills the myth in Cell J. Odd-cycle length here .
Example 4 — Cell D: a disconnected graph
Forecast: the answer for a disconnected graph is bipartite ⟺ every component is bipartite. Component is a triangle → predict not bipartite.
- BFS from 1 (color 0), color 2 = 1. Why? One component only reaches its own vertices; edge is fine.
- BFS did NOT reach 3,4,5. Why restart? Vertices still have . The main loop must start a fresh BFS at the next uncolored vertex — this is the mistake the parent note warns about.
- BFS from 3 (color 0) → 4 = 1 → 5 = 0. Closing edge : both 0 → clash.
Verify: Component bipartite, component not → whole graph ❌ not bipartite. Missing the restart would have wrongly reported "bipartite" after only seeing . Number of components touched .
Example 5 — Cell E: degenerate inputs
Forecast: with no edges there is no constraint to violate → predict both bipartite (vacuously).
- Case (a): color the lone vertex 0. Why no clash possible? There are no edges, so the conflict test
color[v]==color[u]is never evaluated. - Case (b): the main loop starts a BFS at each of the 5 uncolored isolated vertices, painting each 0. Why loop? Every vertex is its own component (Cell D logic applied to the extreme).
- Put every vertex in , leave empty. Why allowed? The definition permits empty ; "every edge crosses" is vacuously true when .
Verify: No edge exists to break the rule → ✅ bipartite in both cases. Number of edges ; conflicts found .
Example 6 — Cell F: greedy gets stuck, augmenting path saves it
Forecast: greedy stopped at size 1. But you should sense a re-route via frees for → predict maximum size 2.

- Current matching , size 1. Free vertices: (left), (right). Why stuck? 's only neighbor is already taken; greedy has no move.
- Search an augmenting path from . Path . Why this shape? An augmenting path alternates free, matched, free and ends at an unmatched right vertex (). It has length 3 (odd).
- Flip every edge on the path. becomes matched, becomes free, becomes matched. Why flip? Flipping turns matched + free into matched — a net +1. See the figure's before/after.
Verify: New matching , size 2. Both matched, both matched — perfect. No unmatched-to-unmatched path remains, so by Berge's theorem it's maximum. Greedy maximum . See Berge's Theorem.
Example 7 — Cell G: Hopcroft–Karp phase mechanics
Forecast: Phase 1 finds length-1 paths; Phase 2 must go longer (length 3). Predict total matching 2 in two phases.

- Phase 1 BFS. Free lefts at distance 0; each reaches a free right in one edge → shortest length 1. Why BFS? It measures the shortest alternating distance from all free lefts at once — the BFS layer number is exactly that distance.
- Phase 1 DFS (maximal disjoint length-1 paths). Take . Try : only neighbor already used this phase → no disjoint length-1 path for . Augment gives +1. Why stop at one? A phase uses only shortest, vertex-disjoint paths; 's path would reuse .
- Phase 2 BFS. Only free left , free right . Shortest augmenting path now bounces through matched edge: (free), (matched), (free) → length 3. Why longer? Exactly the Hopcroft–Karp guarantee: the shortest augmenting length strictly increases each phase.
- Phase 2 DFS. Augment along → +1. Why terminate next? Phase 3 BFS finds no free left → no augmenting path → maximum.
Verify: Phase 1 length , Phase 2 length ( ✅ strictly increasing). Final matching size . Phases used bound. See Maximum Flow for the flow view of the same augmentation.
Example 8 — Cell H: König's theorem twist
Forecast: König says these are pinned by the matching. Predict cover , independent set .
- Min vertex cover. By König's Theorem, in a bipartite graph . Why? A vertex cover must touch every edge; König proves the tightest such set equals the matching size exactly in bipartite graphs.
- Max independent set. Complement of a vertex cover: . Why? Removing a minimum cover leaves the largest edge-free set.
Verify: cover , independent set , and ✅ (cover and independent set partition the vertices). Units: all counts of vertices.
Example 9 — Cell I: word problem (workers ↔ jobs)
Forecast: three workers, jobs all reachable → predict a perfect assignment of size 3.
- Model as bipartite graph. , , edge = "can do". Why bipartite? Two kinds of things, edges only between kinds — the natural home of matching.
- Assign forced ones first. has only → match . Why? A degree-1 vertex forces its single edge.
- Now 's is taken, use . Match . Why? Its other option is gone.
- 's taken, use . Match . Why? Remaining free job.
Verify: Matching , size 3 — every worker and every job used, no augmenting path remains. All 3 jobs staffed. ✅
Example 10 — Cell J: the exam trap
Forecast: you've already seen the pentagon. Predict False.
- Recall the real criterion. Bipartite ⟺ no odd cycle (any odd length), not merely "no triangle". Why? Any odd cycle forces a coloring clash (Ex 2, Ex 3).
- Produce a counterexample. The pentagon is connected, triangle-free, and (Ex 3) not bipartite. Why does it work? Its shortest odd cycle has length 5, so it dodges the triangle test yet still fails.
Verify: Counterexample exists → statement is False. Odd girth of , triangle count , bipartite false. ✅
Recall Quick self-check
Even cycle bipartite? ::: Yes — alternating colors close consistently. Smallest non-bipartite structure? ::: A triangle (odd cycle length 3). Triangle-free ⟹ bipartite? ::: No — any odd cycle (e.g. ) breaks it. Why loop over all vertices in the 2-coloring test? ::: Disconnected components each need their own BFS seed. Greedy matching size vs maximum? ::: Greedy can be smaller; augmenting paths reach the maximum. In Hopcroft–Karp, what strictly increases each phase? ::: The length of the shortest augmenting path. König in a bipartite graph relates which two quantities? ::: Max matching size min vertex cover size.