Dijkstra's Algorithm Solver
This Python implementation solves the shortest path problem in a weighted graph using Dijkstra's algorithm. It allows you to create a graph, specify source and destination nodes, and compute the shortest path between them.
Features
- Graph Representation: The graph is represented as a dictionary, where each node has a list of neighbors and their respective weights.
- Customizable Source and Destination: If no source or destination is provided, the first and last nodes in the graph are chosen by default.
- Heap-based Priority Queue: The algorithm uses a heap (priority queue) for efficient node selection.
- Shortest Path Calculation: Returns both the shortest distance and the shortest path between two nodes.
Installation
From PyPI
To install the Dijkstra solver from PyPI, use the following command:
pip install dijkstra-solver
From Source
-
Clone the repository:
git clone https://github.com/h471x/dijkstra_solver.git
-
Navigate to the project directory:
cd dijkstra_solver
-
Run the default code (just for testing):
python dijkstra/main.py -
Build the package:
python setup.py sdist bdist_wheel
- Install the package:
pip install dist/pypass_tool-*.whl
Usage
To use the Dijkstra solver, create a new python file, then here is the sample example:
-
Import the Dijkstra class and initialize the graph:
from dijkstra import Dijkstra # Define a weighted graph graph = { 'A': {'B': 1, 'C': 4}, 'B': {'C': 2, 'D': 5}, 'C': {'D': 1}, 'D': {} } # Initialize the Dijkstra solver dijkstra = Dijkstra(graph) # Solve for the shortest path from A to D dijkstra.solve(source_node='A', destination_node='D')
-
The output will display the shortest distance and the path:
Path: A -> D Shortest Distance: 4 Shortest Path: A -> B -> C -> D
Methods Overview
-
get_graph_size(graph: dict): Returns the number of nodes in the graph. -
get_node_data(graph: dict): Initializes each node with an infinite cost and an empty predecessor. -
get_src_dest_node(graph: dict): Returns a list of all nodes in the graph. -
get_default_nodes(source: str, destination: str, keys: list[str]): Sets default source and destination if none are provided. -
solve(graph: dict, source: str, destination: str): Computes the shortest path from the source node to the destination node using Dijkstra's algorithm.
Example Graph
Here is an example of a weighted graph:
graph = {
'A': {'B': 2, 'C': 5},
'B': {'C': 1, 'D': 4},
'C': {'D': 2},
'D': {}
}
Release files for dijkstra-solver 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dijkstra-solver-0.1.0.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dijkstra_solver-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / dijkstra-solver-0.1.0.tar.gz
| Download URL | dijkstra-solver-0.1.0.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
78224f2ac64d8c7c73b5710ebddf0bfdce46269a72c92982066d3acdcee70d1c
|
|
BLAKE2b-256 checksum How to use checksums |
9abc2ccee60773d26909cba925f982b03648348b2553b4db30832662b149fb1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.9
|
Release files / dijkstra_solver-0.1.0-py3-none-any.whl
| Download URL | dijkstra_solver-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0f0f4d5d217c86539fcfd2a5459a8a0ba69ef0c4324d6c27cc98670af42972d
|
|
BLAKE2b-256 checksum How to use checksums |
9ee3196353c0cac2d74e7dbec258a380c6633a685fa64dfc41f9832257b9354a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.9
|