Skip to content

Instantly share code, notes, and snippets.

@sausheong
Last active March 20, 2022 03:09
Show Gist options
  • Save sausheong/840a39c061dc196ac7c3a4e5e3f2d94c to your computer and use it in GitHub Desktop.
Save sausheong/840a39c061dc196ac7c3a4e5e3f2d94c to your computer and use it in GitHub Desktop.
da
func dijkstra(graph *WeightedGraph, city string) {
visited := make(map[string]bool)
heap := &Heap{}
startNode := graph.GetNode(city)
startNode.value = 0
heap.Push(startNode)
for heap.Size() > 0 {
current := heap.Pop()
visited[current.name] = true
edges := graph.Edges[current.name]
for _, edge := range edges {
if !visited[edge.node.name] {
heap.Push(edge.node)
if current.value+edge.weight < edge.node.value {
edge.node.value = current.value + edge.weight
edge.node.through = current
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment