Shaft/ algorithms
Back

Dispatch Algorithms

Elevator scheduling is a classic resource allocation problem. Each strategy makes different trade-offs between average latency, fairness, and travel efficiency.

FCFS

Serves requests in arrival order. Simple, but can starve distant floors.

O(1) per dispatch
Best For
Low-traffic, predictable loads
Weakness
Starvation of distant floors under heavy load
function dispatch(queue):
  return queue[0]  // serve in arrival order
SSTF

Always moves to the nearest pending floor. Low average latency, but risks starvation.

O(n) per dispatch
Best For
Minimising average latency
Weakness
Distant floors can starve indefinitely
function dispatch(elevator, targets):
  return min(targets, key = |t - elevator.floor|)
SCAN

Sweeps from bottom to top and back, like a pendulum. Predictable and fair under uniform traffic.

O(n log n) per dispatch
Best For
Uniform traffic across all floors
Weakness
Unnecessary travel to floor endpoints
function dispatch(elevator, targets):
  if direction == UP:
    above = targets >= floor (sorted asc)
    return above[0] ?? 0  // reverse at bottom
  else:
    below = targets <= floor (sorted desc)
    return below[0] ?? MAX_FLOOR
LOOK

Like SCAN but reverses at the last real request instead of the endpoint. More efficient travel.

O(n log n) per dispatch
Best For
General purpose — best all-rounder
Weakness
Slightly more complex state management
function dispatch(elevator, targets):
  if direction == UP:
    above = targets > floor (sorted asc)
    if above: return above[0]
    return max(targets)  // reverse at last request
  else:
    below = targets < floor (sorted desc)
    if below: return below[0]
    return min(targets)
Zoning

Divides the building into vertical zones, one elevator per zone. Excels with concentrated floor traffic.

O(n) per dispatch
Best For
Buildings with concentrated per-floor traffic
Weakness
Inefficient when traffic is evenly distributed
function dispatch(elevator, targets, zone):
  inZone = targets in [zone.min .. zone.max]
  if inZone: return sstf(elevator, inZone)
  return sstf(elevator, targets)  // assist other zones

Architecture Note

The simulation engine (lib/engine/engine.ts) is a plain TypeScript class with no React dependencies. Strategies live in lib/engine/strategies.ts as pure functions. The Zustand store in lib/store/simStore.ts wraps the engine and drives the React UI, snapshotting state after each tick. This separation means the algorithms can be benchmarked independently — no DOM required.