Dijkstra

Finds the shortest distance from a start vertex to every other vertex when no edge weight is negative.

Time complexity O((V+E) log V), gograph function path.Dijkstra.

Metro region

A hundred towns, T1 to T100, where T stands for town, numbered row by row from the top left with ten to a row. Two fast motorways cross at T45: one runs from T41 to T50, the other through T5, T15 and on down to T95. Local roads join neighboring towns, a few of them one way, and each edge is the travel time in minutes.

A directed, weighted graph with 100 vertices and 346 edges.

How many minutes is the quickest drive from T45 to each town, taking the motorways where they help?

Use it in Go

dist := path.Dijkstra(g, "T45")
fmt.Println(dist["T80"])

Example graphs

More in Shortest paths

  • 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