DEV Community

Implementation of Dijkstra using heap in Go

Douglas Makey Mendez Molero on July 14, 2019

Simple implementation of Dijkstra using heap in Go. What is Dijkstra? MEGA SHORT DESCRIPTION: Dijkstra's algorithm to find ...
Collapse
 
curtisfenner profile image
Curtis Fenner • Edited

I don't see the definition of wasVisited. However, it seems like you should use a map[string]bool instead of []string for your visited set so that you don't have to walk over the list.

If you're walking over the list of visited nodes for every edge, your implementation runs in time proportional to $O(|V|*|E|)$! (which defeats the point of using a heap)

You can also simplify yourgetPath implementation. There's no reason to treat the source node specially. You can instead initialize the visited set to empty and the heap with just the source, to be visited first. (Notice how the beginning of the function duplicates the contents of the loop!)

Collapse
 
douglasmakey profile image
Douglas Makey Mendez Molero

for this reason I love to share because is good for me, to improve and learn, at the moment that I wrote the code I was focus in other things and ignore other, thanks master you are 1000% right. <3

Collapse
 
rishabh625 profile image
rishabh625

Checkout this algorithm using min priority queue, please follow me on medium if u like
medium.com/@rishabhmishra131/golan...

Collapse
 
ajinkyax profile image
Ajinkya Borade

this is still over my head :(