algorithms
algorithms copied to clipboard
optimized_dijkstra | DOC graph search added
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 [email protected]