-
Notifications
You must be signed in to change notification settings - Fork 142
Implement Dijkstra's Algorithm for Shortest Path #89
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
Conversation
add a funny story |
graph/dijkstra.py
Outdated
graph = { | ||
0: [(1, 4), (2, 1)], | ||
1: [(3, 1)], | ||
2: [(1, 2), (3, 5)], | ||
3: [(4, 3)], | ||
4: [] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a more complex graph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add a readme file with a funny joke. do it fast
start (int): The starting node. | ||
|
||
Returns: | ||
Dict[int, int]: A dictionary with nodes as keys and shortest distances from start as values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some more comments under this line
complex_graph = { | ||
0: [(1, 4), (2, 2)], | ||
1: [(2, 1), (3, 5)], | ||
2: [(3, 8), (4, 10)], | ||
3: [(4, 2), (5, 6)], | ||
4: [(5, 3)], | ||
5: [(6, 1)], | ||
6: [(4, 4), (7, 2)], | ||
7: [] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add an even more complex graph
test comment |
Closing due to inactivity. |
Implement Dijkstra's Algorithm for Shortest Path
This pull request adds an implementation of Dijkstra's algorithm to the
algorithms
repository. Dijkstra's algorithm is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge weights.Changes
Added
graph/dijkstra.py
:dijkstra
function that takes a graph represented as a dictionary and a start node.__main__
section.Added
graph/test_dijkstra.py
:dijkstra
function.Implementation Details
The algorithm uses a priority queue (implemented with Python's
heapq
module) to efficiently select the next node to process. This ensures an optimal time complexity of O((V + E) log V), where V is the number of vertices and E is the number of edges in the graph.Testing
The implementation has been thoroughly tested with various graph configurations to ensure correctness and robustness. All tests are passing.
Next Steps
This implementation was created as part of a Devin run. You can find the details of the run here: https://staging.itsdev.in/devin/972023a91df94403a18c3e6f869b61a4
@federico, could you please review this implementation and provide any feedback or suggestions for improvement? Thank you!
If you have any feedback, you can leave comments in the PR and I'll address them in the app!