A hostel dorm can be half empty and still turn guests away.
Here's a real case: a 4-bed dorm, one week, seven bookings placed the way most systems place them, on the first bed that's free for the whole stay. Then a guest asks for Wednesday to Saturday (three nights).
There is a free bed on Wednesday (bed 3), on Thursday (bed 2) and on Friday (bed 4). There is no single bed free for all three nights, so the answer is "sold out". Only 14 of the 28 bed-nights are booked.
Move two of the existing guests (neither has arrived yet) to an equal bed, and the same seven bookings leave room for the new one:
I build HostelBeds, a hostel PMS that sells dorms per bed. This post is about the allocation engine behind that second picture: why it's a matching problem, how we pick between legal moves, and the guardrails that keep it from annoying guests.
Why this is a RevPAB problem
Hostels measure dorms in RevPAB: revenue per available bed-night. Turning away that three-night guest doesn't just lose one booking. It lowers revenue for inventory you were going to have empty anyway.
With an illustrative €25 a night, the week above earns 14 × €25 = €350, or €12.50 RevPAB over 28 bed-nights. Fit the extra guest and it's €425, or €15.18: about 21% more from the same beds, with no discount and no extra marketing.
The gaps come from first-fit placement. Every early booking is placed without knowing what comes next, so the free nights end up scattered over different beds. OTAs sell "a bed in a 4-bed dorm", not "bed 3", so the guest doesn't care which bed they get. That gives us room to rearrange, as long as we're careful.
It's bipartite matching, not first-fit
Each reservation has a set of beds it's allowed on: at least the bed and room it booked, under the hostel's rules, for the whole stay. Placing guests means choosing one compatible bed per reservation, with no two overlapping stays on the same bed. That's bipartite matching with a time dimension.
The classic way to add one more guest to a matching is an augmenting path: put the guest on a bed, and if someone is in the way, try to re-home that guest, recursively. A visited set guarantees the search ends.
Simplified from our solver:
// Try to give `res` a bed. If the bed is taken, recursively re-home the occupants. private bool TryPlace(BedReservation res, HashSet<Guid> visited) { if (++_expansions > _budget) return false; // safety valve, see below visited.Add(res.Id); var options = _compatibleBeds[res.Id] .Where(bedId => !FixedBlocked(bedId, res)) // checked-in guests, maintenance .Select(bedId => (bedId, displace: DisplaceesFor(bedId, res))) .Where(x => x.displace.All(o => CanDisplace(o.Id, visited))) .OrderBy(x => CostOf(res, x.bedId, x.displace)) // cheapest legal option first .ToList(); foreach (var (bedId, displace) in options) { var snapshot = new Dictionary<Guid, Guid?>(_assignment); foreach (var o in displace) _assignment[o.Id] = null; _assignment[res.Id] = bedId; if (displace.All(o => TryPlace(o, visited))) return true; foreach (var kv in snapshot) _assignment[kv.Key] = kv.Value; // roll back, try the next bed } return false; } Two details matter:
- Beds are date-aware. Two guests whose stays don't overlap can share a bed.
DisplaceesForonly returns occupants whose stay actually overlaps. - Most constrained first. When several guests need a bed (a bed goes out of service, say), we place the ones with the fewest compatible beds first. It's the same idea as in sudoku solvers: deal with the hardest cell while you still have options.
Legal isn't the same as good: the cost function
Usually many moves are legal. Picking the wrong one is how you end up moving five people to save one booking, or putting a €20 guest in a €45 bed. So every option gets a cost, measured in "moves". Displacing one guest costs 1.0, and every other weight reads as "worth N extra moves to avoid".
private double CostOf(BedReservation res, Guid bedId, List<BedReservation> displace) => _strategy.OverProvisionWeight * FractionDearer(res, _bedById[bedId]) // giving away a dearer bed + BedTypeChangeCost(res, _bedById[bedId]) // bottom bunk -> top bunk + RoomFillCost(_bedById[bedId].RoomId, res) // consolidate vs spread + displace.Sum(DisplaceCost); // who else has to move Over-provisioning is measured as a fraction of what the guest paid ("this bed is 25% dearer than their rate"), not in euros. That way one weight works the same for an €18 dorm and a €140 private. Moving a guest down costs nothing here, because it costs the hostel nothing.
Disruption depends on when the guest arrives. Moving someone who arrives next month is harmless. Moving someone who arrives today means a confused guest at the desk. So the cost rises steeply as check-in approaches:
// Flat 1.0 per move, plus a convex ramp: an arrival today costs the full weight, // one in a week costs almost nothing extra. private double DisplaceCost(BedReservation other) { var days = Math.Max((other.Booking.CheckIn - _now).TotalDays, 0); return 1 + _strategy.DisruptionWeight / Math.Pow(days + 1, 2); } Room fill is a policy choice, so it's a setting: consolidate (top up partly used rooms first, keeping whole rooms free for groups) or spread (keep rooms emptier, which guests like). It's one weighted term in the same sum, not a separate filter. That matters because it can pull against the pricing term, and a sum lets the weights decide.
Guardrails: what never moves
An allocator that maximises RevPAB at any cost will eventually move someone who's already asleep in their bed. The hard rules are enforced before the search, by removing edges from the graph rather than by penalising them:
- Checked-in guests never move. Their bed is a fixed blocker, like a bed under maintenance.
- Never a worse bed. A guest only moves to a bed that gives at least what they booked: bunks to singles or better, never to a smaller bed type, for their whole stay.
- Never a different price. A move made by the hostel never changes what the guest pays, even when the new bed is worth more.
- Per-booking locks. Staff can mark a booking "identical bed only" or "never move". A lock just removes edges, so the search never even sees those options.
- Groups stay together. A booking for four is placed into one room as a unit, or not at all. Rooms are ranked first by the cost of their N cheapest beds, then the members are placed.
Hard constraints remove edges; preferences go in the cost. Keeping those two separate made the code much easier to reason about than one big score with "infinity" sprinkled in.
Keeping it bounded
Backtracking over a densely packed dorm can blow up, so the search has two limits: an expansion budget that scales with the number of candidate beds, and a wall-clock timeout. Whichever trips first stops the search. Whatever is already placed stays placed, and any guest left over goes on a list for staff instead of being silently dropped.
public static long BudgetFor(RelocationEffort effort, int candidateBedCount) { var n = Math.Max(candidateBedCount, 8); return effort switch { RelocationEffort.Low => 100L * n, RelocationEffort.Medium => 1_000L * n, RelocationEffort.High => 10_000L * n, _ => long.MaxValue, }; } At hostel scale (tens to a couple of hundred beds) the budget practically never trips. It's there for the pathological case, not the normal one.
What I'd do differently at a bigger scale
The cost ordering is greedy per step: at every node it tries the cheapest option first. That's not a global minimum-cost matching. A successive-shortest-path min-cost flow would give provably cheaper plans. I haven't needed it yet: on the layouts I've tested, the greedy plans look the way a good front-desk manager would arrange things by hand. It's marked in the code as the upgrade path if plan quality ever falls short.
The other simplification: when a new booking arrives from an OTA, we only use beds that are already free (no cascading). A sale coming in at 2 a.m. should never quietly reshuffle guests who are already placed. The cascading search runs when something forces it, like a bed going out of service, or when staff ask for it.
Takeaways
- If you sell interchangeable units over time (dorm beds, desks, parking spots, rental bikes), first-fit leaves fragments you can't sell. Augmenting-path matching recovers them.
- Put hard rules in the graph and preferences in the cost. Express costs in one unit ("moves") so the weights stay readable.
- Make disruption depend on time. The same move is harmless next month and terrible today.
- Bound the search and fail toward a human, not toward silence.
If you want to see what this looks like from a hostel owner's side, there's a plain-language write-up of the bed allocation rules, and a demo hostel you can click through without an account on hostel-beds.com.
Happy to answer questions about the solver in the comments, especially from anyone who has done the min-cost-flow version of this.




