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

Implement Dijkstra's Algorithm for Shortest Path #89

Closed
wants to merge 2 commits into from

Conversation

staging-devin-ai-integration[bot]
Copy link

@staging-devin-ai-integration staging-devin-ai-integration bot commented Aug 26, 2024

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

  1. Added graph/dijkstra.py:

    • Implements the dijkstra function that takes a graph represented as a dictionary and a start node.
    • Returns a dictionary with the shortest distances from the start node to all other nodes.
    • Includes type hints and docstrings for better readability and maintainability.
    • Provides an example usage in the __main__ section.
  2. Added graph/test_dijkstra.py:

    • Implements unit tests for the dijkstra function.
    • Covers various scenarios including simple graphs, disconnected graphs, single-node graphs, and complex graphs.
    • Includes a test for error handling when the start node is not in the graph.

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

  • Consider adding more advanced graph algorithms (e.g., Bellman-Ford, Floyd-Warshall).
  • Explore optimizations for large-scale graphs.

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!

@marcosfede
Copy link
Owner

add a funny story

Comment on lines 37 to 43
graph = {
0: [(1, 4), (2, 1)],
1: [(3, 1)],
2: [(1, 2), (3, 5)],
3: [(4, 3)],
4: []
}
Copy link
Owner

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

Copy link
Owner

@marcosfede marcosfede left a 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.
Copy link
Owner

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

Comment on lines +53 to +62
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: []
}
Copy link
Owner

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

@marcosfede
Copy link
Owner

test comment

Copy link
Author

Closing due to inactivity.

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.

1 participant