Skip to main content

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

  1. Clone the repository:

    git clone https://github.com/h471x/dijkstra_solver.git
    
  2. Navigate to the project directory:

    cd dijkstra_solver
    
  3. Run the default code (just for testing):

    python dijkstra/main.py
    
  4. Build the package:

python setup.py sdist bdist_wheel
  1. 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:

  1. 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')
    
  2. 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)

Source distribution for dijkstra-solver 0.1.0
File Size Uploaded
dijkstra-solver-0.1.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dijkstra-solver 0.1.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page