edsger 0.1.4__tar.gz → 0.1.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {edsger-0.1.4 → edsger-0.1.6}/.github/workflows/docs.yml +4 -0
- {edsger-0.1.4 → edsger-0.1.6}/.github/workflows/tests.yml +3 -0
- {edsger-0.1.4 → edsger-0.1.6}/.pre-commit-config.yaml +11 -1
- edsger-0.1.6/PKG-INFO +304 -0
- edsger-0.1.6/README.md +263 -0
- edsger-0.1.6/clean_build.sh +29 -0
- edsger-0.1.6/docs/TYPING.md +107 -0
- edsger-0.1.6/docs/source/api.md +553 -0
- edsger-0.1.6/docs/source/assets/dijkstra_benchmark_comparison.png +0 -0
- edsger-0.1.6/docs/source/index.md +92 -0
- {edsger-0.1.4 → edsger-0.1.6}/docs/source/installation.md +13 -1
- {edsger-0.1.4 → edsger-0.1.6}/docs/source/quickstart.md +273 -17
- {edsger-0.1.4 → edsger-0.1.6}/pyproject.toml +11 -0
- edsger-0.1.6/pyrightconfig.json +82 -0
- {edsger-0.1.4 → edsger-0.1.6}/requirements-dev.txt +5 -0
- edsger-0.1.6/scripts/bellman_ford_dimacs.py +371 -0
- edsger-0.1.6/scripts/benchmark_dimacs_USA_linux.json +84 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/benchmark_dimacs_USA_windows.json +29 -29
- edsger-0.1.6/scripts/compare_bellman_ford_scipy.py +538 -0
- edsger-0.1.6/scripts/compare_dijkstra_scipy.py +361 -0
- edsger-0.1.6/setup.py +234 -0
- edsger-0.1.6/src/edsger/_version.py +1 -0
- edsger-0.1.6/src/edsger/bellman_ford.c +35284 -0
- edsger-0.1.6/src/edsger/bellman_ford.pyx +551 -0
- edsger-0.1.6/src/edsger/bfs.c +33575 -0
- edsger-0.1.6/src/edsger/bfs.pyx +243 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/commons.c +286 -278
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/commons.pyx +7 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/dijkstra.c +2433 -1857
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/dijkstra.pyx +7 -0
- edsger-0.1.6/src/edsger/graph_importer.py +340 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/networks.py +4 -2
- edsger-0.1.6/src/edsger/path.py +2040 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/path_tracking.c +423 -302
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/path_tracking.pyx +7 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/pq_4ary_dec_0b.c +1175 -1016
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/pq_4ary_dec_0b.pyx +7 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/spiess_florian.c +1410 -1140
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/spiess_florian.pyx +7 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/star.c +1240 -767
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/star.pyx +7 -0
- edsger-0.1.6/src/edsger/utils.py +128 -0
- edsger-0.1.6/src/edsger.egg-info/PKG-INFO +304 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger.egg-info/SOURCES.txt +18 -0
- edsger-0.1.6/tests/test_arrow_backend.py +404 -0
- edsger-0.1.6/tests/test_backend_selection.py +356 -0
- edsger-0.1.6/tests/test_bellman_ford.py +1014 -0
- edsger-0.1.6/tests/test_bfs.py +691 -0
- edsger-0.1.6/tests/test_dataframe_integration.py +429 -0
- {edsger-0.1.4 → edsger-0.1.6}/tests/test_dijkstra.py +0 -5
- edsger-0.1.6/tests/test_graph_importer.py +445 -0
- {edsger-0.1.4 → edsger-0.1.6}/tests/test_path.py +527 -26
- {edsger-0.1.4 → edsger-0.1.6}/tests/test_path_tracking.py +0 -5
- edsger-0.1.6/tests/test_polars_support.py +330 -0
- edsger-0.1.6/tests/test_pq_4ary_dec_0b.py +42 -0
- edsger-0.1.6/tests/test_spiess_florian.py +14 -0
- edsger-0.1.4/PKG-INFO +0 -125
- edsger-0.1.4/README.md +0 -84
- edsger-0.1.4/docs/source/api.md +0 -116
- edsger-0.1.4/docs/source/assets/dijkstra_benchmark_comparison.png +0 -0
- edsger-0.1.4/docs/source/index.md +0 -80
- edsger-0.1.4/scripts/benchmark_dimacs_USA_linux.json +0 -84
- edsger-0.1.4/setup.py +0 -94
- edsger-0.1.4/src/edsger/_version.py +0 -1
- edsger-0.1.4/src/edsger/path.py +0 -894
- edsger-0.1.4/src/edsger/utils.py +0 -63
- edsger-0.1.4/src/edsger.egg-info/PKG-INFO +0 -125
- edsger-0.1.4/tests/test_pq_4ary_dec_0b.py +0 -47
- edsger-0.1.4/tests/test_spiess_florian.py +0 -19
- {edsger-0.1.4 → edsger-0.1.6}/.github/workflows/publish.yml +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/.gitignore +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/.readthedocs.yaml +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/AUTHORS.rst +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/CHANGELOG.rst +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/CONTRIBUTING.rst +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/LICENSE +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/MANIFEST.in +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/docs/Makefile +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/docs/requirements.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/docs/source/conf.py +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/docs/source/contributing.md +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/requirements.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/README_benchmark_os.md +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/benchmark_comparison_os.py +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/dijkstra_dimacs.py +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/plot_benchmark_comparison.py +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/requirements_linux.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/scripts/requirements_windows.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/setup.cfg +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/.gitignore +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/__init__.py +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/commons.pxd +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/pq_4ary_dec_0b.pxd +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger/prefetch_compat.h +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger.egg-info/dependency_links.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger.egg-info/not-zip-safe +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger.egg-info/requires.txt +0 -0
- {edsger-0.1.4 → edsger-0.1.6}/src/edsger.egg-info/top_level.txt +0 -0
@@ -10,6 +10,9 @@ jobs:
|
|
10
10
|
|
11
11
|
steps:
|
12
12
|
- uses: actions/checkout@v4
|
13
|
+
with:
|
14
|
+
fetch-depth: 0 # Fetch all history for all tags and branches
|
15
|
+
ref: ${{ github.event.release.tag_name }}
|
13
16
|
|
14
17
|
- name: Set up Python
|
15
18
|
uses: actions/setup-python@v5
|
@@ -20,6 +23,7 @@ jobs:
|
|
20
23
|
run: |
|
21
24
|
python -m pip install --upgrade pip
|
22
25
|
pip install -r docs/requirements.txt
|
26
|
+
pip install .
|
23
27
|
|
24
28
|
- name: Build documentation
|
25
29
|
run: |
|
@@ -11,4 +11,14 @@ repos:
|
|
11
11
|
hooks:
|
12
12
|
- id: cython-lint
|
13
13
|
files: '\.(pyx|pxd)$'
|
14
|
-
args: [--max-line-length=88]
|
14
|
+
args: [--max-line-length=88]
|
15
|
+
|
16
|
+
- repo: local
|
17
|
+
hooks:
|
18
|
+
- id: pyright
|
19
|
+
name: pyright
|
20
|
+
entry: pyright
|
21
|
+
language: system
|
22
|
+
types: [python]
|
23
|
+
pass_filenames: false
|
24
|
+
always_run: true
|
edsger-0.1.6/PKG-INFO
ADDED
@@ -0,0 +1,304 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: edsger
|
3
|
+
Version: 0.1.6
|
4
|
+
Summary: Graph algorithms in Cython.
|
5
|
+
Author-email: François Pacull <francois.pacull@architecture-performance.fr>
|
6
|
+
Maintainer-email: François Pacull <francois.pacull@architecture-performance.fr>
|
7
|
+
License: MIT License
|
8
|
+
Project-URL: Repository, https://github.com/aetperf/Edsger
|
9
|
+
Project-URL: Documentation, https://edsger.readthedocs.io
|
10
|
+
Keywords: python,graph,shortest path,Dijkstra
|
11
|
+
Platform: any
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
19
|
+
Classifier: License :: OSI Approved :: MIT License
|
20
|
+
Classifier: Operating System :: OS Independent
|
21
|
+
Classifier: Topic :: Scientific/Engineering
|
22
|
+
Requires-Python: >=3.9
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
License-File: LICENSE
|
25
|
+
License-File: AUTHORS.rst
|
26
|
+
Requires-Dist: setuptools
|
27
|
+
Requires-Dist: setuptools_scm
|
28
|
+
Requires-Dist: numpy>=1.26
|
29
|
+
Requires-Dist: Cython>=3
|
30
|
+
Requires-Dist: pandas
|
31
|
+
Provides-Extra: dev
|
32
|
+
Requires-Dist: black; extra == "dev"
|
33
|
+
Provides-Extra: test
|
34
|
+
Requires-Dist: pytest; extra == "test"
|
35
|
+
Requires-Dist: scipy<1.11; extra == "test"
|
36
|
+
Provides-Extra: doc
|
37
|
+
Requires-Dist: sphinx; extra == "doc"
|
38
|
+
Requires-Dist: sphinx_design; extra == "doc"
|
39
|
+
Requires-Dist: sphinx_rtd_theme; extra == "doc"
|
40
|
+
Dynamic: license-file
|
41
|
+
|
42
|
+
|
43
|
+

|
44
|
+
[](https://codecov.io/gh/aetperf/edsger)
|
45
|
+
[](https://edsger.readthedocs.io/en/latest/?badge=latest)
|
46
|
+
[](https://pypi.org/project/edsger/)
|
47
|
+
[](https://pepy.tech/project/edsger)
|
48
|
+
[](https://pypi.org/project/edsger/)
|
49
|
+
[](https://github.com/psf/black)
|
50
|
+
[](https://github.com/MarcoGorelli/cython-lint)
|
51
|
+
[](https://microsoft.github.io/pyright/)
|
52
|
+
[](https://opensource.org/licenses/MIT)
|
53
|
+
|
54
|
+
# Edsger
|
55
|
+
|
56
|
+
*Graph algorithms in Cython*
|
57
|
+
|
58
|
+
Welcome to our Python library for graph algorithms. The library includes both Dijkstra's and Bellman-Ford's algorithms, with plans to add more common path algorithms later. It is also open-source and easy to integrate with other Python libraries. To get started, simply install the library using pip, and import it into your Python project.
|
59
|
+
|
60
|
+
Documentation : [https://edsger.readthedocs.io/en/latest/](https://edsger.readthedocs.io/en/latest/)
|
61
|
+
|
62
|
+
## Small example : Dijkstra's Algorithm
|
63
|
+
|
64
|
+
To use Dijkstra's algorithm, you can import the `Dijkstra` class from the `path` module. The function takes a graph and a source node as input, and returns the shortest path from the source node to all other nodes in the graph.
|
65
|
+
|
66
|
+
```python
|
67
|
+
import pandas as pd
|
68
|
+
|
69
|
+
from edsger.path import Dijkstra
|
70
|
+
|
71
|
+
# Create a DataFrame with the edges of the graph
|
72
|
+
edges = pd.DataFrame({
|
73
|
+
'tail': [0, 0, 1, 2, 2, 3],
|
74
|
+
'head': [1, 2, 2, 3, 4, 4],
|
75
|
+
'weight': [1, 4, 2, 1.5, 3, 1]
|
76
|
+
})
|
77
|
+
edges
|
78
|
+
```
|
79
|
+
|
80
|
+
| | tail | head | weight |
|
81
|
+
|---:|-------:|-------:|---------:|
|
82
|
+
| 0 | 0 | 1 | 1.0 |
|
83
|
+
| 1 | 0 | 2 | 4.0 |
|
84
|
+
| 2 | 1 | 2 | 2.0 |
|
85
|
+
| 3 | 2 | 3 | 1.5 |
|
86
|
+
| 4 | 2 | 4 | 3.0 |
|
87
|
+
| 5 | 3 | 4 | 1.0 |
|
88
|
+
|
89
|
+
```python
|
90
|
+
# Initialize the Dijkstra object
|
91
|
+
dijkstra = Dijkstra(edges)
|
92
|
+
|
93
|
+
# Run the algorithm from a source vertex
|
94
|
+
shortest_paths = dijkstra.run(vertex_idx=0)
|
95
|
+
print("Shortest paths:", shortest_paths)
|
96
|
+
```
|
97
|
+
|
98
|
+
Shortest paths: [0. 1. 3. 4.5 5.5]
|
99
|
+
|
100
|
+
We get the shortest paths from the source node 0 to all other nodes in the graph. The output is an array with the shortest path length to each node. A path length is the sum of the weights of the edges in the path.
|
101
|
+
|
102
|
+
## Bellman-Ford Algorithm: Handling Negative Weights
|
103
|
+
|
104
|
+
The Bellman-Ford algorithm can handle graphs with negative edge weights and detect negative cycles, making it suitable for more complex scenarios than Dijkstra's algorithm.
|
105
|
+
|
106
|
+
```python
|
107
|
+
from edsger.path import BellmanFord
|
108
|
+
|
109
|
+
# Create a graph with negative weights
|
110
|
+
edges_negative = pd.DataFrame({
|
111
|
+
'tail': [0, 0, 1, 1, 2, 3],
|
112
|
+
'head': [1, 2, 2, 3, 3, 4],
|
113
|
+
'weight': [1, 4, -2, 5, 1, 3] # Note the negative weight
|
114
|
+
})
|
115
|
+
edges_negative
|
116
|
+
```
|
117
|
+
|
118
|
+
| | tail | head | weight |
|
119
|
+
|---:|-------:|-------:|---------:|
|
120
|
+
| 0 | 0 | 1 | 1.0 |
|
121
|
+
| 1 | 0 | 2 | 4.0 |
|
122
|
+
| 2 | 1 | 2 | -2.0 |
|
123
|
+
| 3 | 1 | 3 | 5.0 |
|
124
|
+
| 4 | 2 | 3 | 1.0 |
|
125
|
+
| 5 | 3 | 4 | 3.0 |
|
126
|
+
|
127
|
+
```python
|
128
|
+
# Initialize and run Bellman-Ford
|
129
|
+
bf = BellmanFord(edges_negative)
|
130
|
+
shortest_paths = bf.run(vertex_idx=0)
|
131
|
+
print("Shortest paths:", shortest_paths)
|
132
|
+
```
|
133
|
+
|
134
|
+
Shortest paths: [ 0. 1. -1. 0. 3.]
|
135
|
+
|
136
|
+
The Bellman-Ford algorithm finds the optimal path even with negative weights. In this example, the shortest path from node 0 to node 2 has length -1 (going 0→1→2 with weights 1 + (-2) = -1), which is shorter than the direct path 0→2 with weight 4.
|
137
|
+
|
138
|
+
### Negative Cycle Detection
|
139
|
+
|
140
|
+
Bellman-Ford can also detect negative cycles, which indicate that no shortest path exists:
|
141
|
+
|
142
|
+
```python
|
143
|
+
# Create a graph with a negative cycle
|
144
|
+
edges_cycle = pd.DataFrame({
|
145
|
+
'tail': [0, 1, 2],
|
146
|
+
'head': [1, 2, 0],
|
147
|
+
'weight': [1, -2, -1] # Cycle 0→1→2→0 has total weight -2
|
148
|
+
})
|
149
|
+
|
150
|
+
bf_cycle = BellmanFord(edges_cycle)
|
151
|
+
try:
|
152
|
+
bf_cycle.run(vertex_idx=0)
|
153
|
+
except ValueError as e:
|
154
|
+
print("Error:", e)
|
155
|
+
```
|
156
|
+
|
157
|
+
Error: Negative cycle detected in the graph
|
158
|
+
|
159
|
+
## Breadth-First Search: Unweighted Directed Graphs
|
160
|
+
|
161
|
+
The BFS (Breadth-First Search) algorithm finds shortest paths in directed graphs where edge weights are ignored (or all edges are treated as having equal weight). It's particularly efficient for finding paths based on the minimum number of hops/edges rather than weighted distances.
|
162
|
+
|
163
|
+
```python
|
164
|
+
from edsger.path import BFS
|
165
|
+
|
166
|
+
# Create an unweighted directed graph
|
167
|
+
edges_unweighted = pd.DataFrame({
|
168
|
+
'tail': [0, 0, 1, 2, 2, 3],
|
169
|
+
'head': [1, 2, 3, 3, 4, 4]
|
170
|
+
})
|
171
|
+
edges_unweighted
|
172
|
+
```
|
173
|
+
|
174
|
+
| | tail | head |
|
175
|
+
|---:|-------:|-------:|
|
176
|
+
| 0 | 0 | 1 |
|
177
|
+
| 1 | 0 | 2 |
|
178
|
+
| 2 | 1 | 3 |
|
179
|
+
| 3 | 2 | 3 |
|
180
|
+
| 4 | 2 | 4 |
|
181
|
+
| 5 | 3 | 4 |
|
182
|
+
|
183
|
+
```python
|
184
|
+
# Initialize BFS
|
185
|
+
bfs = BFS(edges_unweighted)
|
186
|
+
|
187
|
+
# Run BFS from vertex 0 with path tracking
|
188
|
+
predecessors = bfs.run(vertex_idx=0, path_tracking=True)
|
189
|
+
print("Predecessors:", predecessors)
|
190
|
+
|
191
|
+
# Extract the path to vertex 4
|
192
|
+
path = bfs.get_path(4)
|
193
|
+
print("Path from 0 to 4:", path)
|
194
|
+
```
|
195
|
+
|
196
|
+
Predecessors: [-9999 0 0 1 2]
|
197
|
+
Path from 0 to 4: [4 2 0]
|
198
|
+
|
199
|
+
The BFS algorithm is ideal for directed graphs when:
|
200
|
+
- All edges should be treated equally (ignoring edge weights)
|
201
|
+
- You need to find paths with the minimum number of edges/hops
|
202
|
+
- You want the fastest path-finding algorithm for unweighted directed graphs (O(V + E) time complexity)
|
203
|
+
|
204
|
+
Note: The predecessor value -9999 indicates either the start vertex or an unreachable vertex. In the path output, vertices are listed from target to source.
|
205
|
+
|
206
|
+
## Installation
|
207
|
+
|
208
|
+
### Standard Installation
|
209
|
+
|
210
|
+
```bash
|
211
|
+
pip install edsger
|
212
|
+
```
|
213
|
+
|
214
|
+
### Development Installation
|
215
|
+
|
216
|
+
For development work, clone the repository and install in development mode:
|
217
|
+
|
218
|
+
```bash
|
219
|
+
git clone https://github.com/aetperf/Edsger.git
|
220
|
+
cd Edsger
|
221
|
+
pip install -r requirements-dev.txt
|
222
|
+
pip install -e .
|
223
|
+
```
|
224
|
+
|
225
|
+
## Development
|
226
|
+
|
227
|
+
This project uses several development tools to ensure code quality:
|
228
|
+
|
229
|
+
### Type Checking
|
230
|
+
|
231
|
+
We use [Pyright](https://github.com/microsoft/pyright) for static type checking:
|
232
|
+
|
233
|
+
```bash
|
234
|
+
# Run type checking
|
235
|
+
make typecheck
|
236
|
+
|
237
|
+
# Or directly with pyright
|
238
|
+
pyright
|
239
|
+
```
|
240
|
+
|
241
|
+
For more details on type checking configuration and gradual typing strategy, see [TYPING.md](TYPING.md).
|
242
|
+
|
243
|
+
### Running Tests
|
244
|
+
|
245
|
+
```bash
|
246
|
+
# Run all tests
|
247
|
+
make test
|
248
|
+
|
249
|
+
# Run with coverage
|
250
|
+
make coverage
|
251
|
+
```
|
252
|
+
|
253
|
+
### Code Formatting and Linting
|
254
|
+
|
255
|
+
```bash
|
256
|
+
# Format code with black
|
257
|
+
make format
|
258
|
+
|
259
|
+
# Check code style
|
260
|
+
make lint
|
261
|
+
```
|
262
|
+
|
263
|
+
### Pre-commit Hooks
|
264
|
+
|
265
|
+
This project uses pre-commit hooks to maintain code quality. The hooks behave differently based on the branch:
|
266
|
+
|
267
|
+
- **Protected branches (main, release*)**: All hooks run including pyright type checking
|
268
|
+
- **Feature branches**: Only formatting hooks run (black, cython-lint) for faster commits
|
269
|
+
- Run `make typecheck` or `pre-commit run --all-files` to manually check types before merging
|
270
|
+
|
271
|
+
```bash
|
272
|
+
# Install pre-commit hooks
|
273
|
+
pre-commit install
|
274
|
+
|
275
|
+
# Run all hooks manually
|
276
|
+
pre-commit run --all-files
|
277
|
+
|
278
|
+
# Skip specific hooks if needed
|
279
|
+
SKIP=pyright git commit -m "your message"
|
280
|
+
```
|
281
|
+
|
282
|
+
### Available Make Commands
|
283
|
+
|
284
|
+
```bash
|
285
|
+
make help # Show all available commands
|
286
|
+
```
|
287
|
+
|
288
|
+
## Why Use Edsger?
|
289
|
+
|
290
|
+
Edsger is designed to be **dataframe-friendly**, providing seamless integration with pandas workflows for graph algorithms. Also it is rather efficient on Linux. Our benchmarks on the USA road network (23.9M vertices, 57.7M edges) demonstrate nice performance:
|
291
|
+
|
292
|
+
<img src="https://raw.githubusercontent.com/aetperf/edsger/release/docs/source/assets/dijkstra_benchmark_comparison.png" alt="Dijkstra Performance Comparison" width="700">
|
293
|
+
|
294
|
+
## Contributing
|
295
|
+
|
296
|
+
We welcome contributions to the Edsger library. If you have any suggestions, bug reports, or feature requests, please open an issue on our [GitHub repository](https://github.com/aetperf/Edsger).
|
297
|
+
|
298
|
+
## License
|
299
|
+
|
300
|
+
Edsger is licensed under the MIT License. See the LICENSE file for more details.
|
301
|
+
|
302
|
+
## Contact
|
303
|
+
|
304
|
+
For any questions or inquiries, please contact me at [francois.pacull@architecture-performance.fr](mailto:francois.pacull@architecture-performance.fr).
|
edsger-0.1.6/README.md
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
|
2
|
+

|
3
|
+
[](https://codecov.io/gh/aetperf/edsger)
|
4
|
+
[](https://edsger.readthedocs.io/en/latest/?badge=latest)
|
5
|
+
[](https://pypi.org/project/edsger/)
|
6
|
+
[](https://pepy.tech/project/edsger)
|
7
|
+
[](https://pypi.org/project/edsger/)
|
8
|
+
[](https://github.com/psf/black)
|
9
|
+
[](https://github.com/MarcoGorelli/cython-lint)
|
10
|
+
[](https://microsoft.github.io/pyright/)
|
11
|
+
[](https://opensource.org/licenses/MIT)
|
12
|
+
|
13
|
+
# Edsger
|
14
|
+
|
15
|
+
*Graph algorithms in Cython*
|
16
|
+
|
17
|
+
Welcome to our Python library for graph algorithms. The library includes both Dijkstra's and Bellman-Ford's algorithms, with plans to add more common path algorithms later. It is also open-source and easy to integrate with other Python libraries. To get started, simply install the library using pip, and import it into your Python project.
|
18
|
+
|
19
|
+
Documentation : [https://edsger.readthedocs.io/en/latest/](https://edsger.readthedocs.io/en/latest/)
|
20
|
+
|
21
|
+
## Small example : Dijkstra's Algorithm
|
22
|
+
|
23
|
+
To use Dijkstra's algorithm, you can import the `Dijkstra` class from the `path` module. The function takes a graph and a source node as input, and returns the shortest path from the source node to all other nodes in the graph.
|
24
|
+
|
25
|
+
```python
|
26
|
+
import pandas as pd
|
27
|
+
|
28
|
+
from edsger.path import Dijkstra
|
29
|
+
|
30
|
+
# Create a DataFrame with the edges of the graph
|
31
|
+
edges = pd.DataFrame({
|
32
|
+
'tail': [0, 0, 1, 2, 2, 3],
|
33
|
+
'head': [1, 2, 2, 3, 4, 4],
|
34
|
+
'weight': [1, 4, 2, 1.5, 3, 1]
|
35
|
+
})
|
36
|
+
edges
|
37
|
+
```
|
38
|
+
|
39
|
+
| | tail | head | weight |
|
40
|
+
|---:|-------:|-------:|---------:|
|
41
|
+
| 0 | 0 | 1 | 1.0 |
|
42
|
+
| 1 | 0 | 2 | 4.0 |
|
43
|
+
| 2 | 1 | 2 | 2.0 |
|
44
|
+
| 3 | 2 | 3 | 1.5 |
|
45
|
+
| 4 | 2 | 4 | 3.0 |
|
46
|
+
| 5 | 3 | 4 | 1.0 |
|
47
|
+
|
48
|
+
```python
|
49
|
+
# Initialize the Dijkstra object
|
50
|
+
dijkstra = Dijkstra(edges)
|
51
|
+
|
52
|
+
# Run the algorithm from a source vertex
|
53
|
+
shortest_paths = dijkstra.run(vertex_idx=0)
|
54
|
+
print("Shortest paths:", shortest_paths)
|
55
|
+
```
|
56
|
+
|
57
|
+
Shortest paths: [0. 1. 3. 4.5 5.5]
|
58
|
+
|
59
|
+
We get the shortest paths from the source node 0 to all other nodes in the graph. The output is an array with the shortest path length to each node. A path length is the sum of the weights of the edges in the path.
|
60
|
+
|
61
|
+
## Bellman-Ford Algorithm: Handling Negative Weights
|
62
|
+
|
63
|
+
The Bellman-Ford algorithm can handle graphs with negative edge weights and detect negative cycles, making it suitable for more complex scenarios than Dijkstra's algorithm.
|
64
|
+
|
65
|
+
```python
|
66
|
+
from edsger.path import BellmanFord
|
67
|
+
|
68
|
+
# Create a graph with negative weights
|
69
|
+
edges_negative = pd.DataFrame({
|
70
|
+
'tail': [0, 0, 1, 1, 2, 3],
|
71
|
+
'head': [1, 2, 2, 3, 3, 4],
|
72
|
+
'weight': [1, 4, -2, 5, 1, 3] # Note the negative weight
|
73
|
+
})
|
74
|
+
edges_negative
|
75
|
+
```
|
76
|
+
|
77
|
+
| | tail | head | weight |
|
78
|
+
|---:|-------:|-------:|---------:|
|
79
|
+
| 0 | 0 | 1 | 1.0 |
|
80
|
+
| 1 | 0 | 2 | 4.0 |
|
81
|
+
| 2 | 1 | 2 | -2.0 |
|
82
|
+
| 3 | 1 | 3 | 5.0 |
|
83
|
+
| 4 | 2 | 3 | 1.0 |
|
84
|
+
| 5 | 3 | 4 | 3.0 |
|
85
|
+
|
86
|
+
```python
|
87
|
+
# Initialize and run Bellman-Ford
|
88
|
+
bf = BellmanFord(edges_negative)
|
89
|
+
shortest_paths = bf.run(vertex_idx=0)
|
90
|
+
print("Shortest paths:", shortest_paths)
|
91
|
+
```
|
92
|
+
|
93
|
+
Shortest paths: [ 0. 1. -1. 0. 3.]
|
94
|
+
|
95
|
+
The Bellman-Ford algorithm finds the optimal path even with negative weights. In this example, the shortest path from node 0 to node 2 has length -1 (going 0→1→2 with weights 1 + (-2) = -1), which is shorter than the direct path 0→2 with weight 4.
|
96
|
+
|
97
|
+
### Negative Cycle Detection
|
98
|
+
|
99
|
+
Bellman-Ford can also detect negative cycles, which indicate that no shortest path exists:
|
100
|
+
|
101
|
+
```python
|
102
|
+
# Create a graph with a negative cycle
|
103
|
+
edges_cycle = pd.DataFrame({
|
104
|
+
'tail': [0, 1, 2],
|
105
|
+
'head': [1, 2, 0],
|
106
|
+
'weight': [1, -2, -1] # Cycle 0→1→2→0 has total weight -2
|
107
|
+
})
|
108
|
+
|
109
|
+
bf_cycle = BellmanFord(edges_cycle)
|
110
|
+
try:
|
111
|
+
bf_cycle.run(vertex_idx=0)
|
112
|
+
except ValueError as e:
|
113
|
+
print("Error:", e)
|
114
|
+
```
|
115
|
+
|
116
|
+
Error: Negative cycle detected in the graph
|
117
|
+
|
118
|
+
## Breadth-First Search: Unweighted Directed Graphs
|
119
|
+
|
120
|
+
The BFS (Breadth-First Search) algorithm finds shortest paths in directed graphs where edge weights are ignored (or all edges are treated as having equal weight). It's particularly efficient for finding paths based on the minimum number of hops/edges rather than weighted distances.
|
121
|
+
|
122
|
+
```python
|
123
|
+
from edsger.path import BFS
|
124
|
+
|
125
|
+
# Create an unweighted directed graph
|
126
|
+
edges_unweighted = pd.DataFrame({
|
127
|
+
'tail': [0, 0, 1, 2, 2, 3],
|
128
|
+
'head': [1, 2, 3, 3, 4, 4]
|
129
|
+
})
|
130
|
+
edges_unweighted
|
131
|
+
```
|
132
|
+
|
133
|
+
| | tail | head |
|
134
|
+
|---:|-------:|-------:|
|
135
|
+
| 0 | 0 | 1 |
|
136
|
+
| 1 | 0 | 2 |
|
137
|
+
| 2 | 1 | 3 |
|
138
|
+
| 3 | 2 | 3 |
|
139
|
+
| 4 | 2 | 4 |
|
140
|
+
| 5 | 3 | 4 |
|
141
|
+
|
142
|
+
```python
|
143
|
+
# Initialize BFS
|
144
|
+
bfs = BFS(edges_unweighted)
|
145
|
+
|
146
|
+
# Run BFS from vertex 0 with path tracking
|
147
|
+
predecessors = bfs.run(vertex_idx=0, path_tracking=True)
|
148
|
+
print("Predecessors:", predecessors)
|
149
|
+
|
150
|
+
# Extract the path to vertex 4
|
151
|
+
path = bfs.get_path(4)
|
152
|
+
print("Path from 0 to 4:", path)
|
153
|
+
```
|
154
|
+
|
155
|
+
Predecessors: [-9999 0 0 1 2]
|
156
|
+
Path from 0 to 4: [4 2 0]
|
157
|
+
|
158
|
+
The BFS algorithm is ideal for directed graphs when:
|
159
|
+
- All edges should be treated equally (ignoring edge weights)
|
160
|
+
- You need to find paths with the minimum number of edges/hops
|
161
|
+
- You want the fastest path-finding algorithm for unweighted directed graphs (O(V + E) time complexity)
|
162
|
+
|
163
|
+
Note: The predecessor value -9999 indicates either the start vertex or an unreachable vertex. In the path output, vertices are listed from target to source.
|
164
|
+
|
165
|
+
## Installation
|
166
|
+
|
167
|
+
### Standard Installation
|
168
|
+
|
169
|
+
```bash
|
170
|
+
pip install edsger
|
171
|
+
```
|
172
|
+
|
173
|
+
### Development Installation
|
174
|
+
|
175
|
+
For development work, clone the repository and install in development mode:
|
176
|
+
|
177
|
+
```bash
|
178
|
+
git clone https://github.com/aetperf/Edsger.git
|
179
|
+
cd Edsger
|
180
|
+
pip install -r requirements-dev.txt
|
181
|
+
pip install -e .
|
182
|
+
```
|
183
|
+
|
184
|
+
## Development
|
185
|
+
|
186
|
+
This project uses several development tools to ensure code quality:
|
187
|
+
|
188
|
+
### Type Checking
|
189
|
+
|
190
|
+
We use [Pyright](https://github.com/microsoft/pyright) for static type checking:
|
191
|
+
|
192
|
+
```bash
|
193
|
+
# Run type checking
|
194
|
+
make typecheck
|
195
|
+
|
196
|
+
# Or directly with pyright
|
197
|
+
pyright
|
198
|
+
```
|
199
|
+
|
200
|
+
For more details on type checking configuration and gradual typing strategy, see [TYPING.md](TYPING.md).
|
201
|
+
|
202
|
+
### Running Tests
|
203
|
+
|
204
|
+
```bash
|
205
|
+
# Run all tests
|
206
|
+
make test
|
207
|
+
|
208
|
+
# Run with coverage
|
209
|
+
make coverage
|
210
|
+
```
|
211
|
+
|
212
|
+
### Code Formatting and Linting
|
213
|
+
|
214
|
+
```bash
|
215
|
+
# Format code with black
|
216
|
+
make format
|
217
|
+
|
218
|
+
# Check code style
|
219
|
+
make lint
|
220
|
+
```
|
221
|
+
|
222
|
+
### Pre-commit Hooks
|
223
|
+
|
224
|
+
This project uses pre-commit hooks to maintain code quality. The hooks behave differently based on the branch:
|
225
|
+
|
226
|
+
- **Protected branches (main, release*)**: All hooks run including pyright type checking
|
227
|
+
- **Feature branches**: Only formatting hooks run (black, cython-lint) for faster commits
|
228
|
+
- Run `make typecheck` or `pre-commit run --all-files` to manually check types before merging
|
229
|
+
|
230
|
+
```bash
|
231
|
+
# Install pre-commit hooks
|
232
|
+
pre-commit install
|
233
|
+
|
234
|
+
# Run all hooks manually
|
235
|
+
pre-commit run --all-files
|
236
|
+
|
237
|
+
# Skip specific hooks if needed
|
238
|
+
SKIP=pyright git commit -m "your message"
|
239
|
+
```
|
240
|
+
|
241
|
+
### Available Make Commands
|
242
|
+
|
243
|
+
```bash
|
244
|
+
make help # Show all available commands
|
245
|
+
```
|
246
|
+
|
247
|
+
## Why Use Edsger?
|
248
|
+
|
249
|
+
Edsger is designed to be **dataframe-friendly**, providing seamless integration with pandas workflows for graph algorithms. Also it is rather efficient on Linux. Our benchmarks on the USA road network (23.9M vertices, 57.7M edges) demonstrate nice performance:
|
250
|
+
|
251
|
+
<img src="https://raw.githubusercontent.com/aetperf/edsger/release/docs/source/assets/dijkstra_benchmark_comparison.png" alt="Dijkstra Performance Comparison" width="700">
|
252
|
+
|
253
|
+
## Contributing
|
254
|
+
|
255
|
+
We welcome contributions to the Edsger library. If you have any suggestions, bug reports, or feature requests, please open an issue on our [GitHub repository](https://github.com/aetperf/Edsger).
|
256
|
+
|
257
|
+
## License
|
258
|
+
|
259
|
+
Edsger is licensed under the MIT License. See the LICENSE file for more details.
|
260
|
+
|
261
|
+
## Contact
|
262
|
+
|
263
|
+
For any questions or inquiries, please contact me at [francois.pacull@architecture-performance.fr](mailto:francois.pacull@architecture-performance.fr).
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# Clean build script for Edsger Cython project
|
4
|
+
# Use this when making significant changes to ensure clean rebuild
|
5
|
+
|
6
|
+
echo "Cleaning build artifacts..."
|
7
|
+
|
8
|
+
# Remove compiled Cython C files
|
9
|
+
find src/ -name "*.c" -delete
|
10
|
+
echo "✓ Removed generated C files"
|
11
|
+
|
12
|
+
# Remove compiled shared libraries
|
13
|
+
find src/ -name "*.so" -delete
|
14
|
+
echo "✓ Removed shared libraries"
|
15
|
+
|
16
|
+
# Remove build directory
|
17
|
+
rm -rf build/
|
18
|
+
echo "✓ Removed build directory"
|
19
|
+
|
20
|
+
# Remove Python cache
|
21
|
+
find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null
|
22
|
+
find . -name "*.pyc" -delete 2>/dev/null
|
23
|
+
echo "✓ Removed Python cache"
|
24
|
+
|
25
|
+
# Remove Cython HTML annotation files (if any)
|
26
|
+
find . -name "*.html" -path "*/build/*" -delete 2>/dev/null
|
27
|
+
|
28
|
+
echo "Clean complete! Ready for fresh build."
|
29
|
+
echo "Run: python setup.py build_ext --inplace"
|