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.
Time complexity O((V+E) log V), gograph function path.AStar.
How it works
A* is how a navigation app finds one route from A to B. It runs like Dijkstra, but it orders its queue by the time so far plus a guess of the time left to B. So it always looks first at the vertex that seems to lead to B soonest, and it stops as soon as B leaves the queue. Dijkstra orders by the time so far alone and spreads out in every direction alike.
The guess on this page is the straight-line distance to B divided by the fastest speed on the map: the most distance per minute between its two ends that any street covers. No route can beat a straight line at that speed, so the guess never says more than the true time, and the route A* returns is the quickest. A guess that can say more than the true time can return a slower route. The guess needs each vertex's place, which only works where the vertices have positions, such as on a map.
This guess is also consistent: it never drops by more than the time of the street between two vertices. Then each vertex leaves the queue once, and A* costs O((V+E) log V), less in practice, since it stops at B. gograph calls the guess at most once for each vertex and never for B. With a guess that isn't consistent, it can take a vertex out of the queue again when a quicker way to it turns up.
path.AStar takes the edge cost and the guess as functions. A nil cost uses the edge weight, and a nil guess makes it Dijkstra. A negative cost returns ErrNegativeWeight, and a goal the start can't reach returns ErrUnreachable. Landmarks, the ALT method, give a sharper guess than a straight line on road networks.
Downtown grid
Twenty intersections on a downtown street grid. Streets go both ways, Market is closed to cars between 2nd and 3rd, and each edge is the travel time in minutes.
A directed, weighted graph with 20 vertices and 60 edges.
What is the quickest drive from Pine & 1st to River & 5th, with Market closed between 2nd and 3rd, and how many intersections does the search look at?
The quickest drive from Pine & 1st to River & 5th takes 15 minutes, along 7 streets. path.DijkstraPaths finds the same time by settling all 20 vertices Pine & 1st can reach.
Step by step
- Every time starts at ∞, except Pine & 1st, which is 0. The guess of the time left is the straight-line distance to River & 5th at the fastest any street on the drawing goes between its two ends, so it never says more than the true time. From Pine & 1st it is 8.6855 minutes, so Pine & 1st goes in the queue at 0 + 8.6855 = 8.6855.
- Take Pine & 1st from the queue: 0 minutes so far plus a guess of 8.6855 to go makes 8.6855, the lowest in the queue.
- Pine & 1st to Pine & 2nd: 0 + 2 = 2. The guess from Pine & 2nd is 7.2839, so Pine & 2nd goes in the queue at 2 + 7.2839 = 9.2839.
- Pine & 1st to Main & 1st: 0 + 2 = 2. The guess from Main & 1st is 7.7821, so Main & 1st goes in the queue at 2 + 7.7821 = 9.7821.
- Take Pine & 2nd from the queue: 2 minutes so far plus a guess of 7.2839 to go makes 9.2839, the lowest in the queue.
- Pine & 2nd to Pine & 3rd: 2 + 3 = 5. The guess from Pine & 3rd is 6.1784, so Pine & 3rd goes in the queue at 5 + 6.1784 = 11.1784.
41 more steps lead to the last one:
- Take River & 5th from the queue at 15 minutes. That's the goal, so the quickest route there is known, and A* stops. It settled 18 vertices, where Dijkstra settles 20 by the time it reaches River & 5th.
Use it in Go
goal := where["River & 5th"]
guess := func(v string) float64 {
p := where[v]
return math.Hypot(p[0]-goal[0], p[1]-goal[1]) / speed
}
route, minutes, err := path.AStar(g, "Pine & 1st", "River & 5th", nil, guess)
if err != nil {
return err // path.ErrUnreachable when the goal is out of reach
}
fmt.Println(minutes)
for _, v := range route {
fmt.Println(v.Label())
}
Example graphs
- New York streets, 295 vertices and 669 edges
- London streets, 274 vertices and 598 edges
- San Francisco streets, 248 vertices and 548 edges
- Rasht streets, 298 vertices and 568 edges
- School run, 6 vertices and 13 edges
- Delivery area, 8 vertices and 12 edges
- Downtown grid, 20 vertices and 60 edges
- One-way city, 45 vertices and 88 edges
- Metro region, 100 vertices and 346 edges
More in Shortest paths
- Dijkstra: Finds the shortest distance from a start vertex to every other vertex when no edge weight is negative.
- 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.
- 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.
- 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.