Skip to main content

PyDijkstra

This python package provides an implementation of the Dijkstra Search Algorithm for any kind of graphs. The focus of this realization lies on the usability.
If you have already written your own class which represents some sort of graph, and you want to use the dijkstra algorithm for path searching in this graph, this package is extremely easy to integrate. You don't have to transform your graph into another special search graph or something. The only things you have to do, is inheriting from the provided class and override some methods.

How to Use

Install Package

pip install pydijkstra

Import Package and Inherit from Dijkstra Class

import pydijkstra

class MyGraph(pydijkstra.Dijkstra):
    ...

Implement the Abstract Methods

def all_nodes(self) -> List[Any]:
    """ Return all nodes in the graph """

def neighbors(self, node: Any) -> List[Any]:
    """ Return all neighbor nodes of the given node """

def weight(self, node1: Any, node2: Any) -> float | int:
    """ Return the weight for going from node1 to node2 (i.e. the distance) """

The typing 'Any' here stands for the representation of one node. So you are free in the choice how you want to represent a node, i.e. use your own defined class, coordinates, ...

Use the dijkstra_search Method
which was inherited from the Dijkstra class

dijkstra_search(start: Any, 
                target: Any = None, 
                output_format: str = 'dijkstra')
  • (param) start: Node to start the dijkstra search from.
  • (param) target: Target node in order to perform early stopping.
  • (param) output_format: String which defines the format of the output. More on this at Output Formats.
  • (return) Result of the dijkstra search based on the output_format.

Output Formats

For different uses you may be need different outputs from the dijkstra search, so you can define the output of the function by the output_format parameter.
Important! The algorithm to calculate the path finding stays the same, independent of the output format. So changing the output format does not lead to a change in calculation cost.

The different options:

  • dijkstra For each found node, it gives the previous visited node and the distance from the start node. The value pair for each node is represented as a dict with 'prev' and 'dist' as its keys.
  • path For each found node, it gives a list of nodes, which represents the shortest path from the start node.
  • path+dist For each found node, it gives the shortest path and the distance from the start node. The value pair for each node is represented as a dict with 'path' and 'dist' as its keys.
  • target_path Gives a list of nodes, which represents the shortest path from the start node to the target node. (Only possible if target is given).

If the output format defines outputs for multiple nodes (i.e. when using 'dijkstra' or 'path'), it tries to return it as a dictionary with the nodes as the keys. If the node is not hashable (and so not usable as a key in the dictionary), it instead returns a list with tuples where the node is the first object and the output the second object of the tuple.

Examples

Some examples on how the package can be used are found in the tests package.
In simple_graph.py, a new graph structure is defined, which inherits from pydijkstra.Dijkstra and implements the needed functions to be able to perform a dijkstra search on the graph it represents.
In nx_search.py, a search class is defined, which gets a NetworkX graph and implements the needed functions with the help of the functions provided by the graph, so it can serve as a path finding class for NetworkX graphs.
In test.py, some unittests for the dijkstra algorithm are defined, which use the two implemented classes in order to test the correctness of the dijkstra algorithm.

Metadata

Release files for pydijkstra 1.0.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 pydijkstra 1.0.0
File Size Uploaded
pydijkstra-1.0.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydijkstra 1.0.0
File Interpreter ABI Platform
pydijkstra-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / pydijkstra-1.0.0.tar.gz

Download URL pydijkstra-1.0.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7202f9fde43a5a58d0904631b26c90233defc604948e5f4c9d54b2b26507a16e
BLAKE2b-256 checksum
How to use checksums
925fd847b2c2aad730a1b63e77a973be6855321f068c4312bba97ce12cd17f05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / pydijkstra-1.0.0-py3-none-any.whl

Download URL pydijkstra-1.0.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be3ee287277f858922de349385f796c7d37112c0f3b734ac8a4f6378bff19264
BLAKE2b-256 checksum
How to use checksums
256a322b0f346255845bdb97e1c41516eabc6209c00fbc3766498e767107bd0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.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