Video in progress
How do you choose the algorithm for assigning couriers to orders on a delivery platform: greedy, optimal assignment, or batching?
Greedy (the nearest free courier for each order) is fast and simple, but leaves bad pairings in the overall result. Optimal assignment (Hungarian, min-cost flow) gives the global best for a batch collected in a short time window. In practice a 10-30 second batch with optimal or a good heuristic is the compromise between waiting and quality.
📖 Answer
One example shows greedy's problem. For order A, the nearest courier is K1 (1 km), and K2 is 2 km away. For order B, only K1 is close (1 km), and K2 is 10 km away. Greedy gives K1 to A, and B is left with K2: 11 km in total. The optimum is A–K2, B–K1: 3 km in total.
The options.
Greedy, immediate: you assign as soon as the order arrives. O(couriers), or even faster with a spatial index. There's no waiting for the user, but when demand is high the total distance and the delay grow.
Batch + Hungarian. For N orders and M couriers, a cost matrix (ETA, distance, courier rating, time until the food is ready) and a minimum-cost matching. Hungarian is O(n³): milliseconds for 300 × 300, but already heavy for 5,000 × 5,000. That's why I split the city into zones and solve each zone separately.
Min-cost flow: if one courier can take several orders (batching) or there are capacity constraints, the problem turns into a network flow. Ready-made solvers (Google OR-tools) are much more reliable than what we'd write ourselves.
Routing (VRP): giving one courier 3 orders in sequence is NP-hard. There's no exact solution; with heuristics and local search (OptaPlanner/Timefold, OR-tools) you get a "good enough" solution within a time limit.
What I look at when deciding:
- The batch window. A 20-second wait isn't noticed by the customer, but pairing quality improves noticeably. I choose the window length with an A/B test: average delivery time, kilometers the courier rides empty.
- The cost function matters even more than the algorithm. If it's only distance, the system keeps using the same nearby couriers and leaves the ones at the edge starving; you need a fairness component.
- Stability. Reassigning in every batch is optimal, but couriers having their assignments keep changing is a bad experience. Either "freeze" assigned pairs or add a penalty for changes.
- If the optimization service doesn't respond, the greedy fallback must work, so orders don't get stuck in the queue.
Pure algorithms alone don't settle this: it's weighed together with the product, operations and the courier experience. I usually launch with greedy and move to batch optimization once the metrics have been collected.
Sign up to mark studied questions and save your progress.