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

optimized_dijkstra | DOC graph search added #748

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Koubae
Copy link

@Koubae Koubae commented Dec 17, 2020

Hi, I've find this repository really good, as I'm coding and learning new algorithm along, I think is a great Idea if I can also Optimized it and add some Documentation (which I Added as a README.MD, if another format for the DOCS is preferred please let me know)

As this is my first contribution in this repo I just added one example for Dijkstra and other algorithm, I have different one to share and there will be more if this first pull request is accepted.

Added

* I add a ShortestPathTree  in Dijkstra Class init as a NamedTuple, makes run the Algorithm slower but it outputs a really cool result for the Shortest-path tree.

* I added some documentation for Dijkstra Algorithm.

Optimized

* More Pythonic code.

* Readability and refactoring of the code
import timeit


test_1 = '''
from main import Dijkstra
g = Dijkstra(8+1)

g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],  # 0
           [4, 0, 8, 0, 0, 0, 0, 11, 0], # 1
           [0, 8, 0, 7, 0, 4, 0, 0, 2],  # 2
           [0, 0, 7, 0, 9, 14, 0, 0, 0],  # 3
           [0, 0, 0, 9, 0, 10, 0, 0, 0], # 4
           [0, 0, 4, 14, 10, 0, 2, 0, 0], # 5
           [0, 0, 0, 0, 0, 2, 0, 1, 6], # 6
           [8, 11, 0, 0, 0, 0, 1, 0, 7], # 7
           [0, 0, 2, 0, 0, 0, 6, 7, 0] # 8
           ]
'''

test_2 = '''
from main2 import Dijkstra_
g = Dijkstra_(8+1)

g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],  # 0
           [4, 0, 8, 0, 0, 0, 0, 11, 0], # 1
           [0, 8, 0, 7, 0, 4, 0, 0, 2],  # 2
           [0, 0, 7, 0, 9, 14, 0, 0, 0],  # 3
           [0, 0, 0, 9, 0, 10, 0, 0, 0], # 4
           [0, 0, 4, 14, 10, 0, 2, 0, 0], # 5
           [0, 0, 0, 0, 0, 2, 0, 1, 6], # 6
           [8, 11, 0, 0, 0, 0, 1, 0, 7], # 7
           [0, 0, 2, 0, 0, 0, 6, 7, 0] # 8
           ]
'''

print('Dijkstra', min(timeit.Timer(test_1).repeat(5, 100_000)))  # New Code
print('Dijkstra_',min(timeit.Timer(test_2).repeat(5, 100_000))) # Old Code
# New Code => Dijkstra 0.20088510000000004
# Old Code =>  Dijkstra_ 0.6164977

Added

* I run Faster! This is a result with a simple benchmark:

Signed-off-by: Koubae 58447627+Koubae@users.noreply.github.com

Signed-off-by: Koubae <58447627+Koubae@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

None yet

1 participant