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.

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.

How many minutes is the quickest drive from Pine & 1st to each intersection, with Market closed between 2nd and 3rd?

Use it in Go

dist := path.Dijkstra(g, "Pine & 1st")
fmt.Println(dist["River & 5th"])

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