learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Sweep Algorithm Calculator

Cluster by angle, then route each truck

Capacitated vehicle routing: sweep the customers by angle around the depot into trucks, then route each truck

Time: O(n log n) + routing
Space: O(n)
Use Case: Delivery rounds around a depot, school buses, waste collection
Showing the example graph for this algorithm.
Auto10
10200
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Sweep Vehicle Routing

The sweep algorithm is a classic heuristic for the capacitated vehicle routing problem: trucks of limited capacity leave a depot, deliver to customers with known demands and come back, and the total distance should be as small as possible. Billy Gillett and Leland Miller published it in 1974. It is a cluster-first, route-second method: first decide which customers share a truck, then find the best order within each truck.

How it works

Every customer gets an angle around the depot, and the customers are sorted by angle. A ray from the depot then sweeps around, starting after the widest gap between two neighbouring customers, and loads each customer it meets onto the current truck. When the next customer's demand would exceed the truck's capacity, that truck is closed and a new one starts. Finally each truck's customers are put in the shortest round trip from the depot, a small travelling salesman problem solved exactly when there are only a few stops. The sweep itself takes O(n log n) time; distances along the road network are shortest-path distances.

Applications

The sweep suits delivery and collection rounds where customers are spread around a central depot: parcel and grocery delivery, school buses, waste collection and field service. It is quick, easy to explain and gives compact service areas that do not overlap, so it is often the first heuristic taught for vehicle routing. On this site it can be compared with the Clarke-Wright savings method on the same instance.

Pseudocode

The sweep algorithm of Gillett and Miller is a cluster-first, route-second heuristic. A ray rotates around the depot and collects customers into a truck until the next one would exceed its capacity, and then each truck gets its own short round trip.

Sweep(depot, customers, capacity):
    sort the customers by their angle around the depot
    start just after the widest gap between two angles
    truck = 1,  load = 0
    for each customer c in angle order:
        if load + demand(c) > capacity:
            truck = truck + 1,  load = 0
        put c on the truck,  load = load + demand(c)
    for each truck:
        order its customers by the shortest round trip
    return the routes

The angular order keeps every cluster in one wedge of the map, so customers on the same truck are near each other and the routes do not cross. Capacity is checked as customers are added, so every cluster is feasible by construction. The only freedom left is the starting angle, which is why practical versions try several.

Worked example, step by step

Plan deliveries on the example this page loads: a depot A in the middle of eight customers, with trucks of capacity 20. It is the same network as the capacitated vehicle routing page, so the two methods can be compared.

Example graph: Depot A with spokes to every customer and a ring road around them. Demands B 4, C 6, D 3, E 7, F 5, G 4, H 6 and I 3, 38 in total, so at least two trucks are needed. Spoke lengths range from 10 to 14 and ring segments from 8 to 10.

  1. Sort by angle. Seen from A, the customers lie at D 37 degrees, C 90, B 143, I 178, H 216, G 261, F 313 and E 354. The two widest gaps, 53 degrees each, are on either side of C, and the sweep starts at C.
  2. Fill truck 1. The ray picks up C (6), B (4), I (3) and H (6), for a load of 19. The next customer, G with demand 4, would bring it to 23, so truck 1 is closed.
  3. Fill truck 2. Truck 2 takes G (4), F (5), E (7) and D (3), for a load of 19, and the sweep has gone all the way round.
  4. Route each truck. Truck 1 drives A-H-I-B-C-A, with length 13 + 9 + 8 + 10 + 10 = 50. Truck 2 drives A-D-E-F-G-A, with length 12 + 10 + 9 + 10 + 10 = 51. Both follow the ring road between their customers.

The sweep plan uses 2 trucks and a total distance of 101, both loaded to 19 out of 20. On this network it beats Clarke-Wright savings, which uses 3 trucks and 111, because Clarke-Wright merges greedily and is left with C, whose demand of 6 no longer fits on either of the other routes. On other networks the order is often reversed, which is why the two are usually run side by side.

Complexity, and where it comes from

Time: O(n log n) plus the routing step · Space: O(n)

Computing the angles is O(n) and sorting them O(n log n). The sweep itself is one pass. The routing step solves a small travelling salesman problem for each truck. Clusters are usually small enough for an exact method, or else a fast heuristic such as nearest neighbour plus 2-opt is used, so the routing cost depends on the cluster size k rather than on n. Running the sweep from every possible starting customer and keeping the best plan multiplies the cost by n and usually improves the result noticeably.

When to use Sweep Vehicle Routing, and when not to

The sweep is one of the two classical construction heuristics for capacitated routing. Others improve on both.

AlternativePrefer it whenCost
Clarke-Wright savingsCustomers are not spread radially around the depot, or travel times do not follow straight-line geometry.O(n^2 log n)
Fisher-JaikumarYou want a better clustering step, choosing seed customers and solving a generalised assignment problem.heavier
k-means then TSPClusters should be compact rather than wedge shaped, for example with a depot at the edge of the area.O(n k i) plus routing
Large neighbourhood searchLarge real instances where a few percent of distance matters. It repeatedly destroys and repairs part of the plan.tunable

Common pitfalls

  • Using a single starting angle. The result depends strongly on where the ray starts, because that decides where the first truck is cut off. Try every customer as the start, or at least the widest gaps, and keep the best plan.
  • A depot at the edge of the region. The method assumes customers surround the depot. If the depot is in a corner, all customers lie within a narrow range of angles and the wedges become long thin strips with poor routes.
  • Ignoring the road network. Angles are computed from coordinates, but trucks drive on roads. Rivers, motorways and one-way systems can make customers in the same wedge far apart in travel time.
  • Not improving the routes afterwards. Customers near a cluster boundary are often better served by the neighbouring truck. A final pass of swaps and moves between routes usually removes several percent of distance.

Frequently asked questions

What is the sweep algorithm for vehicle routing?
It is a heuristic for the capacitated vehicle routing problem, published by Gillett and Miller in 1974. It sorts customers by their angle around the depot, fills trucks in that order until capacity would be exceeded, and then builds a short round trip for each truck.
What is cluster-first, route-second?
A two-stage strategy for vehicle routing: first assign customers to vehicles, respecting capacity, then solve a travelling salesman problem inside each group. The sweep algorithm is the best-known example. The opposite, route-first, cluster-second, builds one giant tour and then cuts it into feasible pieces.
What is the difference between the sweep algorithm and Clarke-Wright?
The sweep clusters customers by angle and then routes each cluster. Clarke-Wright starts with one route per customer and merges routes in order of savings. The sweep favours compact wedges around a central depot, Clarke-Wright adapts better to irregular geography. On the example of this page the sweep needs 2 trucks and 101, Clarke-Wright 3 trucks and 111.
What is the time complexity of the sweep algorithm?
O(n log n) for sorting the customers by angle, plus the cost of routing each cluster. Because clusters are small, the routing step is cheap even when it is solved exactly.
When does the sweep algorithm work well?
When the depot lies near the middle of the service area, customers are spread around it, and travel distance is close to straight-line distance. It works poorly with a depot at the edge, with very uneven demands, or with road networks that make nearby customers hard to reach from one another.

Related algorithms: Capacitated Vehicle Routing (CVRP), Fleet Dispatching (mTSP), Traveling Salesman Problem, Christofides

Interactive Controls

Zoom Controls

41%
Nodes: 9
Edges: 16