A Python implementation of Dijkstra's algorithm for finding the shortest path in a weighted graph.
Project description
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': {}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dijkstra-solver-0.1.0.tar.gz
.
File metadata
- Download URL: dijkstra-solver-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78224f2ac64d8c7c73b5710ebddf0bfdce46269a72c92982066d3acdcee70d1c |
|
MD5 | c702275e8459a772727c724b92518647 |
|
BLAKE2b-256 | 9abc2ccee60773d26909cba925f982b03648348b2553b4db30832662b149fb1f |
File details
Details for the file dijkstra_solver-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: dijkstra_solver-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0f0f4d5d217c86539fcfd2a5459a8a0ba69ef0c4324d6c27cc98670af42972d |
|
MD5 | 1aa4ba23764d9fd444f9458791c2435f |
|
BLAKE2b-256 | 9ee3196353c0cac2d74e7dbec258a380c6633a685fa64dfc41f9832257b9354a |