Ch.27: Algorithms: How the Hungarian Algorithm Finds Matches
Outline
Transcript
0:00 The Hungarian algorithm finds the cheapest one-to-one assignment: one worker per job, nobody used twice. Giving a technician their nearest job can raise the whole bill. We'll find the cheaper assignment and show why it wins. Take a simple example: three technicians, A, B and C, and three jobs, X, Y and Z. The table shows their travel times in minutes. Rows are technicians; columns are jobs. Each cell gives the travel cost of pairing those two. My first thought is to send each technician to the cheapest remaining job.
0:32 A can reach X in 4 minutes, Y in one, or Z in three. Okay, A takes Y for one. For B, that removes the 0-minute option: B is already at Y, but somebody else has claimed it. B's remaining choices are X for five or Z for six. Take X, leaving C with Z for one. The total is seven. Every local choice looks defensible. A got its nearest job, B took the cheaper remaining trip, and nobody was assigned twice. Yet we've decided A's convenience matters before considering the others. If I process the rows in another order, the result could change. Yeah, we gave the first technician first choice.
1:12 The objective never gave A that privilege; it only asked us to minimize the sum. Can we find a better total without trying every possible complete plan? We can. Hungarian assignment changes the numbers while preserving which complete assignment is cheapest. Then zero-valued cells expose useful pairings. I don't quite see why changing travel times is legitimate. A van doesn't move faster because we edited its spreadsheet. The spreadsheet gets a discount; the van still has to drive. Keep the original table for the final bill.
1:43 The new numbers are an accounting device. Okay, the vans haven't saved a minute. Where do the subtracted minutes go? Into a tracked baseline. Add it back to each full plan. We need an operation that discounts all legal assignments equally. Now apply that equal discount to A's row. Every complete assignment uses one entry there. Subtract one from all three entries, and each complete total drops by one. A's 4, 1, 3 becomes 3, 0, 2. Every plan loses one. Kuhn's method starts with these fixed shifts; the original paper is in the description.
2:18 Row B already contains 0, so its row minimum changes nothing. For C, subtract one from three, 2, 1 to get 2, 1, 0. We've removed 2 minutes altogether, one from A and one from C. Original total now equals two plus the chosen reduced values. Now consider a column: X contains 3, 5, 2. Every legal full plan takes that job once, so a common column discount also preserves the winner. Its smallest value is 2. Subtract that from the whole column, leaving 1, 3, 0. The tracked discount grows from two to four.
2:55 The other two columns already contain zeros, so they're finished. For any legal full assignment, add four to its reduced total to recover the original. Can we get the greedy seven back from these reduced costs? Yes. Those greedy choices now have values 0, 3 and 0. Add the baseline four and the original seven returns. Right. A reduced zero adds nothing above that baseline. It doesn't make the original trip free; the unchanged travel table still determines what the vans do. So none of these reduced values can be negative.
3:27 A full assignment made entirely of zeros would hit the baseline exactly; no sum of them can go below 0. So if we can choose three zeros without repeating a row or column, we've got both a plan and a reason it's optimal. Yes. Start with A to Y, and put C on X. Both chosen cells are 0; their rows are distinct, as are their jobs. But B's only zero is Y as well. That's occupied by A. We still have one technician waiting. We need to distinguish having zeros everywhere from being able to choose enough of them together.
4:02 That leaves A and B needing the same zero-priced column. We have a lower bound of four, but the available zero pairs can't attain it yet. Now examine the blocked zero choices. We've selected A with Y, plus C with X. A matching is a set of pairs that reuse neither side. Here its maximum size is two. A and B share their only zero job, Y, while C can use X or Z. For instance, moving C to Z frees X, but neither A nor B has a zero there yet. Shuffling the existing zeros cannot assign everybody. That's stronger than saying our first attempt got stuck.
4:41 The actual zero pattern prevents us from covering all three technicians, whichever pairs we try. Now we cover every zero with as few whole rows or columns as possible. Those lines will tell us where to change the table. I think I can draw several different covers. How do we choose systematically rather than guess which set of lines looks tidy? Start from an unmatched row, B. Mark that row, follow its zero to column Y, and mark Y too. Y already belongs to A in our selected matching. Following that selected pair backwards brings us to A, so mark A's row.
5:17 Right. From A, the only zero again leads to Y. There's nowhere new to reach, so the marking stops. Okay, I've marked A and B. Do I draw the cover through those rows? Cover the unmarked rows and the marked columns. In our table that means row C and column Y. Keep those two kinds of mark separate. Ah, we reached A and B precisely because they were competing for Y. We cover that shared column, plus C's other zeros along its row. Our two selected zero pairs share neither a row nor a column. One covering line cannot hit both, so any cover needs at least two.
5:55 Every zero lies under our two lines. So two is enough, and your lower bound rules out one. We've found a minimum cover. Exactly. Here two covering lines fall short of the three technicians, so we need more useful zero choices before we can finish. Yeah, A and B need an escape from Y. The cells outside both cover lines are their alternatives in X and Z. That gives us the places to adjust: outside the cover, plus its intersection. Let's work through the smallest uncovered value and the compensating change.
6:28 Now use our cover, row C and column Y. The uncovered values are 1, 2, 3 and 6. Their minimum is one. Subtract one from those four uncovered cells. A's X becomes 0, A's Z becomes one, B's X becomes two, and B's Z becomes five. Leave every singly covered cell unchanged. At C to Y, both lines cross, so add 1: that cell goes from one to two. Hold on. We wanted cheaper alternatives, but we also increased a cell. How does this preserve the best assignment? Think of the adjustment as two whole-row discounts, on A and B, followed by a whole-column surcharge on Y.
7:08 Each has size 1. Every complete plan uses both those rows and that column. So it loses two, gains one, and drops by exactly one overall. Yes. On A to Y and B to Y, the discount and surcharge cancel. C to Y gets only the surcharge; uncovered cells get only discounts. The tracked baseline therefore rises from four to five. We haven't changed which original plan is cheapest, and all reduced values remain nonnegative. A now has zeros in X and Y, B still has Y, and C can choose Z as well as X. We have new options. Take an intermediate state: A holds Y, C holds X.
7:47 B is still waiting, and its zero leads to occupied Y. B's zero reaches Y, owned by A. A has another zero at X. Skip the Y we've already reached, and follow X instead. X belongs to C. C can reach unassigned Z, so move C there, A to X, and B to Y. Now everybody has a distinct job. Oh, that new zero did more than offer A a different trip. It let us move a whole chain of assignments and fit B in. Right, and every selected cell is 0. Use our untouched travel costs: A to X costs four, B to Y 0, C to Z one.
8:27 Oh, A does more driving, but the bill falls. Three extra minutes for A save five for B. That's seven down to five. I hadn't accounted for that cost of taking Y away from B. Choosing A's cheapest trip was a decision about everybody else's remaining options. Yeah, and now the pairs are backed by the accounting, not by which technician happened to be processed first. Can we also rule out some other assignment costing four, instead of merely celebrating that five beats seven? We can. Keep the final baseline and the nonnegative table together; they provide the lower bound we need.
9:03 Now the baseline certifies the result. Any complete assignment costs five plus its reduced entries, which cannot sum to a negative number. No original total can beat five. Our three zero pairs cost five. We can attain that lower bound. Nice. We can rule out cheaper plans without trying all the complete arrangements. But how do we know a zero assignment is as large as we can make it on a bigger table? Start from every unmatched row. Follow a zero to a job; if it's occupied, step back along its current assignment and try that owner's other zeros. If that branch stops, try the next 0.
9:40 Mark each visited technician and job so a cycle cannot trap the search. Skip any job you've already reached. A free destination completes the chain, like Z did for us. What if every waiting row gets blocked? Then build the cover from a fresh marking search. Start with all unmatched rows, follow their zeros, and step back through occupied jobs' owners. Take a separate zero-choice pattern. A is paired with Y; C has X. B and D are waiting, and both have only Y at 0. B reaches Y, then its owner A. A has no other 0. That search stops; mark B, A and Y.
10:17 D reaches Y as well, so it reaches the same A. We add D's row to those marks. Both waiting rows are included now. Marked rows A, B, D; marked column Y. C is still unmarked. C's row plus column Y cover all six zeros. Now adjust uncovered costs and search again. Repeat until all rows are assigned, then return their original costs. Does that make five the best plan for any dispatcher? Not if they care about when all the technicians arrive. Say they depart together. Our trips are 4, 0 and 1. The last arrival is at 4 minutes.
10:52 Send A to Z for three, keep B at Y for 0, and let C travel to X for three. Everybody arrives by 3 minutes. But total travel is six, instead of five. A dispatcher could say, 'I need everybody there sooner.' I'd choose that plan for them. Right. The table supports different questions. Hungarian minimizes the sum here; it doesn't choose the objective. Tracking objects between camera frames gives us another assignment problem. With that tracking assignment, compare new camera detections with predicted positions for objects the tracker already knows.
11:25 Which detection belongs with which track? Existing tracks become rows and new detections become columns. Each cell measures how poorly a detection fits a predicted object, rather than how long a van travels. The original SORT paper uses Hungarian assignment for that pairing, alongside a Kalman filter for prediction. We've linked the authors' paper and repository. One detection shouldn't be claimed by two tracks just because it looks plausible to both. That's our shared-job conflict in another setting.
11:53 But suppose an object disappears behind a truck while an unrelated object enters the frame. Forcing those two together would invent a continuation that the camera hasn't established. We'd need permission to leave items unmatched and a way to reject an unacceptable pairing. Those are modeling choices; reducing a table cannot decide them for us. Exactly. Assignment handles the pairing step. Predictions, suitable costs and track updates still belong to the larger system, with their own assumptions.
12:20 That boundary gives us a code-review question: are we minimizing the right bill, or just optimizing numbers because they're available? For our technicians, minimizing total travel led to 5 minutes. Another goal needs its own test. Thanks for listening to Learning Podcasts.