You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-1Lines changed: 58 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@
13
13
14
14
*Graph algorithms in Cython*
15
15
16
-
Welcome to our Python library for graph algorithms. So far, the library only includes Dijkstra's algorithm but we should add a range of 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.
16
+
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.
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.
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.
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.
95
+
96
+
### Negative Cycle Detection
97
+
98
+
Bellman-Ford can also detect negative cycles, which indicate that no shortest path exists:
99
+
100
+
```python
101
+
# Create a graph with a negative cycle
102
+
edges_cycle = pd.DataFrame({
103
+
'tail': [0, 1, 2],
104
+
'head': [1, 2, 0],
105
+
'weight': [1, -2, -1] # Cycle 0→1→2→0 has total weight -2
Welcome to the Edsger documentation! Edsger is a Python library for efficient graph algorithms implemented in Cython. The library currently focuses on shortest path algorithms, with Dijkstra's algorithm fully implemented and additional algorithms planned for future releases.
9
+
Welcome to the Edsger documentation! Edsger is a Python library for efficient graph algorithms implemented in Cython. The library focuses on shortest path algorithms, featuring so far both Dijkstra's algorithm for positive-weight graphs and Bellman-Ford algorithm for graphs with negative weights and cycle detection.
10
10
11
11
## Why Use Edsger?
12
12
@@ -18,7 +18,7 @@ Edsger is designed to be **dataframe-friendly**, providing seamless integration
18
18
19
19
```python
20
20
import pandas as pd
21
-
from edsger.path import Dijkstra
21
+
from edsger.path import Dijkstra, BellmanFord
22
22
23
23
# Your graph data is already in a DataFrame
24
24
edges = pd.DataFrame({
@@ -27,19 +27,21 @@ edges = pd.DataFrame({
27
27
'weight': [1.0, 2.0, 1.5, 1.0]
28
28
})
29
29
30
-
#No conversion needed - use directly!
31
-
dijkstra = Dijkstra(edges, orientation="out")
30
+
#Use Dijkstra for positive weights (faster)
31
+
dijkstra = Dijkstra(edges)
32
32
distances = dijkstra.run(vertex_idx=0)
33
-
distances
33
+
34
+
# Use Bellman-Ford for negative weights or cycle detection
35
+
bf = BellmanFord(edges)
36
+
distances = bf.run(vertex_idx=0)
34
37
```
35
-
array([0., 1., 2., 3.])
36
38
37
39
## Key Features
38
40
39
41
-**Native pandas DataFrame support** - No graph object conversion required
40
-
-**High performance** - Cython implementation with aggressive optimizations
41
-
-**Memory efficient** - Optimized for large-scale real-world datasets
42
-
-**Easy integration** with NumPy and pandas workflows
42
+
-**High performance** - Cython implementation
43
+
-**Memory efficient** - Optimized for real-world datasets
44
+
-**Easy integration** with NumPy and Pandas workflows
43
45
-**Production ready** - Comprehensive testing across Python 3.9-3.13
0 commit comments