Beam search

Looks for a route from one vertex to another while keeping only the few best partial paths at each step, so its memory and time stay bounded, at the cost of sometimes missing the best route.

Time complexity O(L·w·(L + d log w)), gograph function path.Beam.

How it works

Beam search grows paths from the start one edge at a time. At each step it extends every path it kept by each edge that doesn't go back to a vertex already on it, ranks the new paths by a score, and keeps only the best few, the width of the beam. The rest are dropped for good. It stops at the first step that keeps a path ending at the goal.

Since it never holds more than the width, its memory and time stay bounded however large the graph is. That is the trade: a path dropped early may have been the start of the best route, so the route found can be slower, and when every kept path runs into a dead end, there is no route at all. It is used where a full search is too big: speech and translation decoders, planners and puzzle solvers.

The score on this page is the minutes so far plus the straight-line guess of the time left from the A* page, so a path that heads toward the goal ranks first. A nil score ranks by the minutes so far alone, which at the same width usually finds slower routes or none, since it can't tell which way the goal is.

path.Beam takes the width and the score, which gets the edge and the cost so far and returns the rank, lower first. Paths with the same score keep the order they were made in. A width below 1 returns ErrInvalidWidth, and a beam whose kept paths never reach the goal returns ErrUnreachable. With L edges on the route and d the most edges out of a vertex, it costs O(L·w·(L + d log w)) for a width of w.

London streets

The main roads of central London from Westminster to the City, from OpenStreetMap, with the bridges over the Thames to Southwark and Waterloo. Each intersection is named after its streets, one-way streets go one way, and each edge is the minutes to drive it at the speed limit, or at 20 to 32 km/h by road type where the map has no limit.

A directed, weighted graph with 274 vertices and 598 edges.

Map data © OpenStreetMap contributors.

How quick a route does a beam of three find from Trafalgar Sq & Strand to Tower Bridge Rd & Tooley St, and how much less of the map does it look at than Dijkstra?

A beam of 3 finds a drive of 21.57 minutes from Trafalgar Sq & Strand to Tower Bridge Rd & Tooley St. The quickest route takes 8.37, so the beam dropped the paths that led to it.

Step by step

  1. Start one path at Trafalgar Sq & Strand. Each step extends every kept path by one edge, ranks the new paths, and keeps the best 3. The rank is the minutes so far plus the straight-line guess of the time left from the A* page, so a path that heads toward Tower Bridge Rd & Tooley St comes first.
  2. Step 1: extending the kept paths by one edge makes 2 paths, no more than the width, so keep them all.
  3. Step 2: extending the kept paths by one edge makes 5 paths. Keep the best 3 paths, ending at Trafalgar Sq & Cockspur St, St Martin's Lane & St Martin's Pl and Northumberland Ave & Victoria Embankment, and drop 2.
  4. Step 3: extending the kept paths by one edge makes 4 paths. Keep the best 3 paths, ending at Trafalgar Sq near Cockspur St, Cockspur St near Warwick House St and Victoria Embankment near Bridge St, and drop 1.
  5. Step 4: extending the kept paths by one edge makes 2 paths, no more than the width, so keep them all.
  6. Step 5: extending the kept paths by one edge makes 4 paths. Keep the best 3 paths, ending at Pall Mall East & Haymarket, Pall Mall & Waterloo Pl and Victoria Embankment & Bridge St, and drop 1.

69 more steps lead to the last one:

  1. Step 75: extending the kept paths by one edge makes 5 paths. Keep the best 3 paths, ending at Tooley St & Queen Elizabeth St, Tower Bridge Rd & Tooley St and Tower Bridge Rd & Queen Elizabeth St, and drop 2. A kept path reaches Tower Bridge Rd & Tooley St, at 21.57 minutes, so the beam stops.

Use it in Go

goal := where["Tower Bridge Rd & Tooley St"]
score := func(e *gograph.Edge[string], soFar float64) float64 {
	p := where[e.Destination().Label()]
	return soFar + e.Weight() + math.Hypot(p[0]-goal[0], p[1]-goal[1])/speed
}

route, minutes, err := path.Beam(g, "Trafalgar Sq & Strand", "Tower Bridge Rd & Tooley St", 3, score)
if err != nil {
	return err // path.ErrUnreachable when every kept path runs into a dead end
}
fmt.Println(minutes)
for _, v := range route {
	fmt.Println(v.Label())
}

Example graphs

More in Shortest paths

  • Dijkstra: Finds the shortest distance from a start vertex to every other vertex when no edge weight is negative.
  • A*: Finds the quickest route from one vertex to another, looking first where a guess of the time left says the goal is, so it settles far fewer vertices than Dijkstra.
  • Bidirectional search: Finds the quickest route from one vertex to another by searching forward from the start and backward from the goal at once, until the two searches meet.
  • k shortest paths: Finds the k quickest routes from one vertex to another that don't repeat a vertex, quickest first, the way a navigation app offers a few routes.
  • Multi-source Dijkstra: Finds the nearest of several sources for every vertex, and the distance from it, in one search.
  • k-center: Chooses k centers so that the vertex farthest from its nearest center is as near as it can make it.
  • Bellman-Ford: Finds the shortest distance from a start vertex to every other vertex, even with negative weights, and reports a negative cycle when the start can reach one.
  • Floyd-Warshall: Finds the shortest distance between every pair of vertices, even with negative weights, by letting paths stop at one more vertex in each round.

All algorithms