Graph algorithms in Cython
Edsger is a Python library of graph algorithms for directed graphs, written in Cython. A graph is given as a dataframe of edges, from pandas or Polars, and the results come back as NumPy arrays, pandas Series or Polars DataFrames.
| Family | Classes |
|---|---|
| Shortest paths | Dijkstra, BellmanFord, AStar, BidirectionalDijkstra |
| Traversal and components | BFS, DFS, ConnectedComponents |
| Spanning trees and centrality | MinimumSpanningTree, Centrality (PageRank, HITS, Katz) |
| Transit assignment | HyperpathGenerating (Spiess and Florian's optimal strategies) |
Documentation: https://edsger.readthedocs.io/en/latest/
pip install edsgerEdsger requires Python 3.10 or later. Wheels are published for Linux (x86_64), Windows and macOS (arm64).
import pandas as pd
from edsger import Dijkstra
edges = pd.DataFrame(
{
"tail": [0, 0, 1, 2, 2, 3],
"head": [1, 2, 2, 3, 4, 4],
"weight": [1.0, 4.0, 2.0, 1.5, 3.0, 1.0],
}
)
dijkstra = Dijkstra(edges)
dijkstra.run(vertex_idx=0)array([0. , 1. , 3. , 4.5, 5.5])
The result is the length of the shortest path from vertex 0 to each vertex of the graph. The graph is built once, and run can be called any number of times with different vertices. With path_tracking=True, the paths themselves can be retrieved.
dijkstra.run(vertex_idx=0, path_tracking=True)
dijkstra.get_path(vertex_idx=4)array([4, 3, 2, 1, 0], dtype=uint32)
The other classes are described in the quick start.
Dijkstra's algorithm takes about one second on the USA road network of the 9th DIMACS implementation challenge, which has 23.9 million vertices and 57.7 million edges.
Single-source shortest paths from one vertex of the USA road network, best of 5 runs. The Linux and Windows timings come from two different machines. The method and the versions are given in the benchmarks page.
Bug reports and pull requests are welcome on GitHub. See the contributing page for the development setup.
Edsger is distributed under the MIT License, see the LICENSE file.
François Pacull, francois.pacull@architecture-performance.fr
