Multi-source Dijkstra
Finds the nearest of several sources for every vertex, and the distance from it, in one search.
Time complexity O((V+E) log V), gograph function path.DijkstraMultiSource.
How it works
Multi-source Dijkstra answers which source is nearest to each vertex, such as which warehouse can reach each address soonest. It puts every source in the queue at distance 0 and then runs Dijkstra's algorithm once. Each vertex is settled by whichever source gets there first, so the areas around the sources grow together until they meet.
One search settles each vertex once, so it costs O((V+E) log V) however many sources there are. Running path.Dijkstra from each source instead settles every vertex once per source. The performance card on this page times both.
path.DijkstraMultiSource returns a NearestSources value with Source, Distance and PathTo for any vertex. When two sources are equally near, the one listed first wins, so the same sources give the same answer on every run. Distances run along the edges, from a source to the vertex. On a map with one-way streets, that is the drive from the warehouse to the address, and the drive back can differ.
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.
With warehouses at opposite corners of downtown, which one should deliver to each intersection, and what is the longest drive?
Warehouse 1 at Pine & 1st serves 10 and 2 at River & 5th serves 10. The longest drive is 8, from warehouse 2 to River & 2nd.
Step by step
- Put every warehouse in the queue at 0: 1 at Pine & 1st and 2 at River & 5th. Each place goes to the warehouse that can reach it soonest, so one search serves them all.
- Take Pine & 1st from the queue. It is warehouse 1, at 0 from itself.
- Pine & 1st to Pine & 2nd: 0 + 2 = 2, so Pine & 2nd goes to warehouse 1 at 2.
- Pine & 1st to Main & 1st: 0 + 2 = 2, so Main & 1st goes to warehouse 1 at 2.
- Take River & 5th from the queue. It is warehouse 2, at 0 from itself.
- River & 5th to Market & 5th: 0 + 2 = 2, so Market & 5th goes to warehouse 2 at 2.
45 more steps lead to the last one:
- Done. Every place has its nearest warehouse. One search settled 20 places. path.Dijkstra from each warehouse in turn would settle 40 between them.
Use it in Go
nearest, err := path.DijkstraMultiSource(g, "Pine & 1st", "River & 5th")
if err != nil {
return err
}
for _, v := range g.GetAllVertices() {
from, ok := nearest.Source(v.Label())
if !ok {
continue
}
minutes, _ := nearest.Distance(v.Label())
fmt.Println(v.Label(), from.Label(), minutes)
}
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
- 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.
- 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.