Sikau

Data structures · Day 13 · about 50 min

Capstone: Delivery dispatcher

A heap for urgent orders plus BFS distances on the town graph.

Who gets the next rider?

Riders leave from the café. Each order has a destination and a deadline. Combine the structures you built: a graph of the town, BFS for travel distance, and a priority queue to always dispatch the order with the least slack.

Plan the dispatcher

Break the problem into the structures you know. Build an adjacency list from the roads. Run BFS once from the café to get every destination's distance in hops, then multiply by five minutes. For each order, slack is the deadline minus the travel time. A priority queue ordered by slack hands out the most urgent order first.

  • Graph: Map of town → neighbours, both directions.
  • Distances: one BFS from "Cafe" — O(V + E).
  • Slack: deadline - hops * 5.
  • Order: priority queue by slack, ties by id.
  • Unreachable destinations: decide and document what happens (skip or report).

Compute everything that doesn't depend on the order once, before the loop over orders.

The rest of this lesson

  • Slack and tiesexplain
  • Why these structuresexplain
  • Compute the slackpredict
  • Structure to jobmatch
  • dispatchchallenge
  • Badge quizquiz
  • Data structures complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 13 lessons of Data structures