Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dijkstra O(N^2) #8

Closed
yanpozka opened this issue Sep 9, 2015 · 4 comments
Closed

Dijkstra O(N^2) #8

yanpozka opened this issue Sep 9, 2015 · 4 comments

Comments

@yanpozka
Copy link

yanpozka commented Sep 9, 2015

Hey man, good job, I just detected that your Go implementation of Dijkstra algorithms is O(N^2)
https://github.com/gyuho/learn/tree/master/doc/go_graph_shortest_path#dijkstra-algorithm

So the issue is that you have a O(N logN) solution in your description but in your Golang implementation if you include heap.Init(...) inside the loop:
for minHeap.Len() != 0
you'll have a N^2 time because the method heap.Init() has a time of O(N) so that call is inside of a O(N) loop.

Happy coding !!

@gyuho
Copy link
Owner

gyuho commented Sep 9, 2015

Great catch. I will update the code, which I believe I only need to heapify once after the for-loop.
The code is from https://github.com/gyuho/goraph.
I will test the code and commit to this repo as well.

Thanks @yanpozka

@gyuho
Copy link
Owner

gyuho commented Sep 9, 2015

Original fix and tests are at:
gyuho/goraph#50

And the fix for this repository here:
#9

Thanks again @yanpozka

@gyuho gyuho closed this as completed Sep 9, 2015
@yanpozka
Copy link
Author

yanpozka commented Sep 9, 2015

Good job @gyuho !! You're welcome! Always it's a pleasure to find people who cares about algorithm.

@gyuho
Copy link
Owner

gyuho commented Sep 9, 2015

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants