Skip to the workspace
Pebellslearn by building
Graph Structure & Decomposition  ›  Lesson

Bipartite Graphs & Matching

Bipartite Graphs & Matching 30 minPair drivers with jobs, and flip a chain when you get stuck

dsa / Bipartite Graphs & Matching

Explore each scene at your own pace.

01 · Two sides, links only across

Two sides, links only across

Bipartite Graphs & Matching

Definition
A bipartite graph has nodes in two groups with edges only between the groups, and a matching is a set of edges that share no node. A maximum matching is found by repeatedly flipping an augmenting path, which adds one more pair each time.
In plain words
Think of handing out jobs to drivers, where each driver can do only some jobs and each job needs one driver. You want as many pairs as possible, and when someone is stuck you may ask an existing pair to switch to make room.

In short: a bipartite graph splits into two sides with links only crossing between them, and a matching pairs them up with no driver or job reused twice. An augmenting path runs from a free driver to a free job, alternating unused and used links; flipping every link on it grows the matching by exactly one. Kuhn's algorithm is a DFS that looks for such a path from each driver in turn, bumping whoever currently holds a wanted job one level deeper, and a matching is maximum exactly when no augmenting path is left to find.

Four drivers ana, ben, cy, dee on the left and four jobs van, truck, bike, crane on the right; the matched links dee-van, ana-truck, ben-bike and cy-crane are bold green, the rest grey dashed

Every morning at six, Meridian's dispatch board fills up with jobs: van runs, a truck delivery, a bike courier loop, a crane lift at the harbour. Every driver is licensed for some of them. Each driver can take at most one job, and each job needs one driver. The shift lead wants as many jobs covered as possible, and wants it before the first van leaves.

The obvious method is first come, first served. Walk down the drivers and give each one any free job they're licensed for. It's fast, and it's often wrong.

Drivers on the left, jobs on the right, and a link wherever a driver holds the licence. No link ever joins two drivers or two jobs. A graph whose nodes split into two sides like this, with every link crossing between them, is bipartite. A set of links that share no endpoint (no driver twice, no job twice) is a matching, and the shift lead wants a maximum matching.

Not every graph can be split into two sides. BFS can test it by colouring each newly found node the opposite colour to the node that found it. A link between two nodes of the same colour means there's no split:

What does this print?
from collections import deque

near = {"a": ["b", "c"], "b": ["a", "c"], "c": ["a", "b"]}
side = {"a": 0}
queue = deque(["a"])
ok = True
while queue:
    n = queue.popleft()
    for m in near[n]:
        if m not in side:
            side[m] = 1 - side[n]
            queue.append(m)
        elif side[m] == side[n]:
            ok = False
print(ok)

A graph is bipartite exactly when it has no ring of odd length. Driver-and-job graphs are bipartite by construction, so the question here is the matching.

Scene 1 of 12
Project context and further resources
You're building a piece ofMeridian — the network model
This piece — assign_drivers(): Covers as many jobs as possible with drivers licensed for them.
Scenario Every morning Meridian has a board of jobs (van runs, truck runs, bike couriers) and a list of drivers, each licensed for some of them. Handing out jobs first-come-first-served leaves jobs uncovered that a smarter shuffle would have filled. Driver assignment finds the largest set of jobs that can go out at once.
assign_drivers.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
assign_drivers(can_do) → intThe maximum number of jobs covered at once, one driver per job.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.