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.

One-way city

Forty-five intersections, I1 to I45, where I stands for intersection, numbered row by row from the top left with nine to a row. Most streets are one way, and the outer streets form a clockwise loop. Only the middle row (I19 to I27) and the middle column (I5, I14, I23, I32 and I41) go both ways, and each edge is the travel time in minutes.

A directed, weighted graph with 45 vertices and 88 edges.

How quick a route does a beam of three find from I23 to I18, and how much less of the map does it look at than Dijkstra?

A beam of 3 finds a drive of 26 minutes from I23 to I18. That's the quickest route, the one Dijkstra finds.

Step by step

  1. Start one path at I23. 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 I18 comes first.
  2. Step 1: extending the kept paths by one edge makes 4 paths. Keep the best 3 paths, ending at I14, I24 and I32, and drop 1.
  3. Step 2: extending the kept paths by one edge makes 6 paths. Keep the best 3 paths, ending at I25, I15 and I5, and drop 3.
  4. Step 3: extending the kept paths by one edge makes 5 paths. Keep the best 3 paths, ending at I26, I34 and I14, and drop 2.
  5. Step 4: extending the kept paths by one edge makes 6 paths. Keep the best 3 paths, ending at I17, I27 and I35, and drop 3.
  6. Step 5: extending the kept paths by one edge makes 5 paths. Keep the best 3 paths, ending at I16, I8 and I36, and drop 2.

1 more steps lead to the last one:

  1. Step 7: extending the kept paths by one edge makes 4 paths. Keep the best 3 paths, ending at I18, I14 and I6, and drop 1. A kept path reaches I18, at 26 minutes, so the beam stops.

Use it in Go

goal := where["I18"]
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, "I23", "I18", 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