FLOW-P5

Workers and jobs

Practice 5 of 5Network flow9 tests

The problem

A depot has nl drivers and nr routes. Each allowed pair (worker, job) says that a driver can take a route. Give as many drivers as possible one route each, with no route given twice: a maximum matching. Return the pairs you choose.

pairs = [(0, 0), (0, 1), (1, 0), (2, 1),
         (2, 2), (3, 2), (3, 3)]
matching(4, 4, pairs)
# [(0, 1), (1, 0), (2, 2), (3, 3)]

When several matchings are largest, any of them is accepted: the tests check that every pair is allowed, that no driver or route is used twice, and how many pairs there are.

Limits

  • Up to 60,000 drivers, 60,000 routes and 120,000 pairs. The big tests have 3 to 4 seconds each.
  • An augmenting path can be 10,000 steps long, so recursion is too deep.
  • The last test (30,000 drivers, 90,000 pairs) needs Hopcroft-Karp (lesson 5); one search per free driver passes the rest.

Builds on FLOW-5 Bipartite matching.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2Greedy is not enoughNot run
3More jobs than workersNot run
4A worker with no jobsNot run
5Repeated pairsNot run
6No pairsNot run
7A long chainNot run
860,000 workers, 120,000 pairsNot run
930,000 workers, 90,000 pairsNot run