Dijkstar

raw JSON →
2.6.0 verified Fri May 01 auth: no python

Dijkstar is an implementation of Dijkstra's single-source shortest paths algorithm and the A* algorithm for weighted multigraphs. Current version 2.6.0 (release frequency: occasional). It is simple to use and allows custom graph structures.

pip install dijkstar
error AttributeError: module 'dijkstar' has no attribute 'Graph'
cause Importing incorrectly (e.g., import dijkstar then dijkstar.Graph) or having a different version where Graph is not top-level.
fix
Use: from dijkstar import Graph
error KeyError: node not in graph
cause Attempting to find a path from or to a node that has not been added to the graph.
fix
Ensure all nodes in the path query are present in the graph.
gotcha Graph is a multigraph; adding multiple edges between the same nodes with different costs is allowed and may lead to unexpected shortest paths if not careful.
fix Ensure that duplicate edges with unintended costs are not added inadvertently.
gotcha Node IDs must be hashable; using unhashable types (like lists) as nodes will raise an error.
fix Use hashable node types such as integers, strings, or tuples.

Create a weighted graph and find the shortest path between two nodes.

from dijkstar import Graph, find_path

graph = Graph()
graph.add_edge(1, 2, 100)
graph.add_edge(2, 3, 200)

path_info = find_path(graph, 1, 3)
print('Shortest path:', path_info.nodes)
print('Total cost:', path_info.total_cost)