Data Structures & AlgorithmsVery hardProduct company OA

Problem: Assign interns to projects; intern u may take only projects in adj[u], each project at most one intern. Maximum bipartite matching with augmenting paths (Kuhn) versus 'each intern grabs the first free project'.

Problem: Assign interns to projects; intern u may take only projects in adj[u], each project at most one intern. Maximum bipartite matching with augmenting paths (Kuhn) versus 'each intern grabs the first free project'.
def kuhn(nL, adj):
    match = [-1] * 10
    def try_(u, seen):
        for v in adj[u]:
            if v in seen: continue
            seen.add(v)
            if match[v] == -1 or try_(match[v], seen):
                match[v] = u; return True
        return False
    return sum(try_(u, set()) for u in range(nL))
def greedy(nL, adj):
    used, c = set(), 0
    for u in range(nL):
        for v in adj[u]:
            if v not in used: used.add(v); c += 1; break
    return c
adj = {0: [0, 1], 1: [0], 2: [2, 1], 3: [2], 4: [3, 4], 5: [4]}
print(kuhn(6, adj), greedy(6, adj))
What is printed?
  1. A. 5 5
  2. B. 5 4
  3. C. 4 4
  4. D. 6 4

Correct answer: B. 5 4

Kuhn tries to place u; if the project is taken it recursively asks the current owner to move to another project – an augmenting path. Interns 0 and 1 compete for project 0 and 1 has no alternative, so 0 must move to project 1. Interns 2 and 3 then compete for project 2 (2's other option, project 1, is now taken), so only one of them is placed. Interns 4 and 5 resolve like 0 and 1. Maximum 5. Greedy gives 0 project 0 and 4 project 3, blocking 1 and 5 → 4. O(V·E).

More Coding Interview Problems questions

Exams that ask this topic