DEV Community

Discussion on: Implementation of Dijkstra using heap in Go

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