Data Structures & AlgorithmsVery hardICPC regional · bipartite matching

Problem: What is the maximum matching size in the bipartite graph with left vertices {0,1,2}, right vertices {0,1,2} and edges (0,0), (0,1), (1,0), (2,1)? Run Kuhn's augmenting-path code.

Problem: What is the maximum matching size in the bipartite graph with left vertices {0,1,2}, right vertices {0,1,2} and edges (0,0), (0,1), (1,0), (2,1)? Run Kuhn's augmenting-path code.
adj = {0: [0, 1], 1: [0], 2: [1]}
match_r = {}
def try_kuhn(u, seen):
    for v in adj[u]:
        if v in seen:
            continue
        seen.add(v)
        if v not in match_r or try_kuhn(match_r[v], seen):
            match_r[v] = u
            return True
    return False
res = 0
for u in adj:
    if try_kuhn(u, set()):
        res += 1
print(res)
What is printed?
  1. A. 3
  2. B. 1
  3. C. 0
  4. D. 2

Correct answer: D. 2

Vertex 1 can only take right 0 and vertex 2 can only take right 1, so at most two can be matched and two are achievable.

More Coding Interview Problems questions

Exams that ask this topic