edsger 0.1.4__cp311-cp311-macosx_11_0_arm64.whl → 0.1.5__cp311-cp311-macosx_11_0_arm64.whl
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/_version.py +1 -1
- edsger/bellman_ford.c +34907 -0
- edsger/bellman_ford.cpython-311-darwin.so +0 -0
- edsger/bellman_ford.pyx +544 -0
- edsger/commons.c +246 -243
- edsger/commons.cpython-311-darwin.so +0 -0
- edsger/dijkstra.c +277 -262
- edsger/dijkstra.cpython-311-darwin.so +0 -0
- edsger/path.py +622 -23
- edsger/path_tracking.c +259 -244
- edsger/path_tracking.cpython-311-darwin.so +0 -0
- edsger/pq_4ary_dec_0b.c +267 -252
- edsger/pq_4ary_dec_0b.cpython-311-darwin.so +0 -0
- edsger/spiess_florian.c +276 -261
- edsger/spiess_florian.cpython-311-darwin.so +0 -0
- edsger/star.c +259 -244
- edsger/star.cpython-311-darwin.so +0 -0
- edsger/utils.py +68 -4
- {edsger-0.1.4.dist-info → edsger-0.1.5.dist-info}/METADATA +59 -2
- edsger-0.1.5.dist-info/RECORD +36 -0
- edsger-0.1.4.dist-info/RECORD +0 -33
- {edsger-0.1.4.dist-info → edsger-0.1.5.dist-info}/WHEEL +0 -0
- {edsger-0.1.4.dist-info → edsger-0.1.5.dist-info}/licenses/AUTHORS.rst +0 -0
- {edsger-0.1.4.dist-info → edsger-0.1.5.dist-info}/licenses/LICENSE +0 -0
- {edsger-0.1.4.dist-info → edsger-0.1.5.dist-info}/top_level.txt +0 -0
Binary file
|
edsger/utils.py
CHANGED
@@ -6,7 +6,15 @@ import numpy as np
|
|
6
6
|
import pandas as pd
|
7
7
|
|
8
8
|
|
9
|
-
def generate_random_network(
|
9
|
+
def generate_random_network(
|
10
|
+
n_edges=100,
|
11
|
+
n_verts=20,
|
12
|
+
seed=124,
|
13
|
+
sort=True,
|
14
|
+
allow_negative_weights=False,
|
15
|
+
negative_weight_ratio=0.3,
|
16
|
+
weight_range=(0.1, 1.0),
|
17
|
+
):
|
10
18
|
"""
|
11
19
|
Generate a random network with a specified number of edges and vertices.
|
12
20
|
|
@@ -20,6 +28,14 @@ def generate_random_network(n_edges=100, n_verts=20, seed=124, sort=True):
|
|
20
28
|
The seed for the random number generator. Default is 124.
|
21
29
|
sort : bool, optional
|
22
30
|
Whether to sort the edges by tail and head vertices. Default is True.
|
31
|
+
allow_negative_weights : bool, optional
|
32
|
+
Whether to allow negative edge weights. Default is False (positive weights only).
|
33
|
+
negative_weight_ratio : float, optional
|
34
|
+
Proportion of edges that should have negative weights when allow_negative_weights=True.
|
35
|
+
Must be between 0.0 and 1.0. Default is 0.3 (30% negative).
|
36
|
+
weight_range : tuple of float, optional
|
37
|
+
Range of absolute values for weights as (min, max). Default is (0.1, 1.0).
|
38
|
+
When allow_negative_weights=True, negative weights will be in range (-max, -min).
|
23
39
|
|
24
40
|
Returns
|
25
41
|
-------
|
@@ -28,6 +44,8 @@ def generate_random_network(n_edges=100, n_verts=20, seed=124, sort=True):
|
|
28
44
|
|
29
45
|
Examples
|
30
46
|
--------
|
47
|
+
Generate a graph with positive weights only (default):
|
48
|
+
|
31
49
|
>>> generate_random_network(n_edges=5, n_verts=3, seed=42)
|
32
50
|
tail head weight
|
33
51
|
0 0 2 0.975622
|
@@ -36,20 +54,66 @@ def generate_random_network(n_edges=100, n_verts=20, seed=124, sort=True):
|
|
36
54
|
3 1 2 0.786064
|
37
55
|
4 2 0 0.761140
|
38
56
|
|
57
|
+
Generate a graph with mixed positive and negative weights:
|
58
|
+
|
59
|
+
>>> generate_random_network(n_edges=5, n_verts=3, seed=42,
|
60
|
+
... allow_negative_weights=True, negative_weight_ratio=0.4)
|
61
|
+
tail head weight
|
62
|
+
0 0 2 0.975622
|
63
|
+
1 1 0 -0.128114
|
64
|
+
2 1 0 0.450386
|
65
|
+
3 1 2 -0.786064
|
66
|
+
4 2 0 0.761140
|
39
67
|
|
40
68
|
Notes
|
41
69
|
-----
|
42
70
|
The 'tail' and 'head' columns represent the source and destination vertices of each edge,
|
43
|
-
respectively. The 'weight' column represents the weight of each edge
|
44
|
-
|
71
|
+
respectively. The 'weight' column represents the weight of each edge.
|
72
|
+
|
73
|
+
When allow_negative_weights=False (default), weights are random floats between
|
74
|
+
weight_range[0] and weight_range[1].
|
75
|
+
|
76
|
+
When allow_negative_weights=True, approximately negative_weight_ratio proportion of edges
|
77
|
+
will have negative weights, useful for testing algorithms like Bellman-Ford that support
|
78
|
+
negative edge weights.
|
45
79
|
|
46
80
|
If `sort` is True, the DataFrame is sorted by the 'tail' and 'head' columns and the index
|
47
81
|
is reset.
|
48
82
|
"""
|
83
|
+
# Validate parameters
|
84
|
+
if not 0.0 <= negative_weight_ratio <= 1.0:
|
85
|
+
raise ValueError("negative_weight_ratio must be between 0.0 and 1.0")
|
86
|
+
if (
|
87
|
+
len(weight_range) != 2
|
88
|
+
or weight_range[0] <= 0
|
89
|
+
or weight_range[1] <= weight_range[0]
|
90
|
+
):
|
91
|
+
raise ValueError("weight_range must be (min, max) with 0 < min < max")
|
92
|
+
|
49
93
|
rng = np.random.default_rng(seed=seed)
|
50
94
|
tail = rng.integers(low=0, high=n_verts, size=n_edges)
|
51
95
|
head = rng.integers(low=0, high=n_verts, size=n_edges)
|
52
|
-
|
96
|
+
|
97
|
+
# Generate weights
|
98
|
+
if allow_negative_weights:
|
99
|
+
# Generate weights in the specified range
|
100
|
+
weight = rng.uniform(low=weight_range[0], high=weight_range[1], size=n_edges)
|
101
|
+
|
102
|
+
# Randomly select edges to have negative weights
|
103
|
+
n_negative = int(n_edges * negative_weight_ratio)
|
104
|
+
if n_negative > 0:
|
105
|
+
negative_indices = rng.choice(n_edges, size=n_negative, replace=False)
|
106
|
+
weight[negative_indices] *= -1
|
107
|
+
else:
|
108
|
+
# Original behavior: positive weights in range [weight_range[0], weight_range[1]]
|
109
|
+
if weight_range == (0.1, 1.0):
|
110
|
+
# Keep backward compatibility for default case
|
111
|
+
weight = rng.random(size=n_edges)
|
112
|
+
else:
|
113
|
+
weight = rng.uniform(
|
114
|
+
low=weight_range[0], high=weight_range[1], size=n_edges
|
115
|
+
)
|
116
|
+
|
53
117
|
edges = pd.DataFrame(data={"tail": tail, "head": head, "weight": weight})
|
54
118
|
if sort:
|
55
119
|
edges.sort_values(by=["tail", "head"], inplace=True)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: edsger
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.5
|
4
4
|
Summary: Graph algorithms in Cython.
|
5
5
|
Author-email: François Pacull <francois.pacull@architecture-performance.fr>
|
6
6
|
Maintainer-email: François Pacull <francois.pacull@architecture-performance.fr>
|
@@ -54,7 +54,7 @@ Dynamic: license-file
|
|
54
54
|
|
55
55
|
*Graph algorithms in Cython*
|
56
56
|
|
57
|
-
Welcome to our Python library for graph algorithms.
|
57
|
+
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.
|
58
58
|
|
59
59
|
Documentation : [https://edsger.readthedocs.io/en/latest/](https://edsger.readthedocs.io/en/latest/)
|
60
60
|
|
@@ -98,6 +98,63 @@ print("Shortest paths:", shortest_paths)
|
|
98
98
|
|
99
99
|
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.
|
100
100
|
|
101
|
+
## Bellman-Ford Algorithm: Handling Negative Weights
|
102
|
+
|
103
|
+
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.
|
104
|
+
|
105
|
+
```python
|
106
|
+
from edsger.path import BellmanFord
|
107
|
+
|
108
|
+
# Create a graph with negative weights
|
109
|
+
edges_negative = pd.DataFrame({
|
110
|
+
'tail': [0, 0, 1, 1, 2, 3],
|
111
|
+
'head': [1, 2, 2, 3, 3, 4],
|
112
|
+
'weight': [1, 4, -2, 5, 1, 3] # Note the negative weight
|
113
|
+
})
|
114
|
+
edges_negative
|
115
|
+
```
|
116
|
+
|
117
|
+
| | tail | head | weight |
|
118
|
+
|---:|-------:|-------:|---------:|
|
119
|
+
| 0 | 0 | 1 | 1.0 |
|
120
|
+
| 1 | 0 | 2 | 4.0 |
|
121
|
+
| 2 | 1 | 2 | -2.0 |
|
122
|
+
| 3 | 1 | 3 | 5.0 |
|
123
|
+
| 4 | 2 | 3 | 1.0 |
|
124
|
+
| 5 | 3 | 4 | 3.0 |
|
125
|
+
|
126
|
+
```python
|
127
|
+
# Initialize and run Bellman-Ford
|
128
|
+
bf = BellmanFord(edges_negative)
|
129
|
+
shortest_paths = bf.run(vertex_idx=0)
|
130
|
+
print("Shortest paths:", shortest_paths)
|
131
|
+
```
|
132
|
+
|
133
|
+
Shortest paths: [ 0. 1. -1. 0. 3.]
|
134
|
+
|
135
|
+
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.
|
136
|
+
|
137
|
+
### Negative Cycle Detection
|
138
|
+
|
139
|
+
Bellman-Ford can also detect negative cycles, which indicate that no shortest path exists:
|
140
|
+
|
141
|
+
```python
|
142
|
+
# Create a graph with a negative cycle
|
143
|
+
edges_cycle = pd.DataFrame({
|
144
|
+
'tail': [0, 1, 2],
|
145
|
+
'head': [1, 2, 0],
|
146
|
+
'weight': [1, -2, -1] # Cycle 0→1→2→0 has total weight -2
|
147
|
+
})
|
148
|
+
|
149
|
+
bf_cycle = BellmanFord(edges_cycle)
|
150
|
+
try:
|
151
|
+
bf_cycle.run(vertex_idx=0)
|
152
|
+
except ValueError as e:
|
153
|
+
print("Error:", e)
|
154
|
+
```
|
155
|
+
|
156
|
+
Error: Negative cycle detected in the graph
|
157
|
+
|
101
158
|
## Installation
|
102
159
|
|
103
160
|
### Standard Installation
|
@@ -0,0 +1,36 @@
|
|
1
|
+
edsger-0.1.5.dist-info/RECORD,,
|
2
|
+
edsger-0.1.5.dist-info/WHEEL,sha256=qxQkdhERtGxJzqnVOBnucx1aUmU2n3HmuzYdln_LyOw,109
|
3
|
+
edsger-0.1.5.dist-info/top_level.txt,sha256=QvhzFORJIIot6GzSnDrtGa9KQt9iifCbOC5ULlzY5dg,7
|
4
|
+
edsger-0.1.5.dist-info/METADATA,sha256=LF7X-0Qgi0OuVqY_I_uNsKr5xfCnPHYbOJ-AuPXN9jY,7111
|
5
|
+
edsger-0.1.5.dist-info/licenses/LICENSE,sha256=eNjfz5CInLrVdczJbhazCKtb8-0qB0UaXZ3bXN0zio0,1111
|
6
|
+
edsger-0.1.5.dist-info/licenses/AUTHORS.rst,sha256=9lqpqjiC4XukK7jdxXwKJJrddqwCV2DjpYTqhpP6znA,105
|
7
|
+
edsger/spiess_florian.c,sha256=bAxIe7SjZi3TDAdwT5py4LMiz_S14NOm_z62l-ZP8ts,1356425
|
8
|
+
edsger/dijkstra.c,sha256=KKT9sI0KGYnvDyEhP_Wr0j2GoMPSVfCWEQypIkBQP6c,1658053
|
9
|
+
edsger/bellman_ford.pyx,sha256=swabcGIjYIeFBud5VdlPAQENv3_AXULnSNLXaQSpE3U,16884
|
10
|
+
edsger/_version.py,sha256=Nmswip0IUvJenHIhdfSyTYurDcwWTvOQ8mPDREtwE1o,21
|
11
|
+
edsger/bellman_ford.c,sha256=S10CgCkQO7hv9o2Ie_rfe7Cl2fytw_CtL7B-Zro66CA,1374207
|
12
|
+
edsger/spiess_florian.cpython-311-darwin.so,sha256=kc0blCEvg75DAovaJnV5p7lEdQUhu7P83IKeYCgFka8,276280
|
13
|
+
edsger/commons.c,sha256=li8n04xPc7bKullEhjVHTxS6s_WN8jCncKeWb3hJEjI,343126
|
14
|
+
edsger/star.c,sha256=VHQUKaeAxlXGRY5NZt0Xyl6gUNZetjk598JX6MKuj98,1319723
|
15
|
+
edsger/path_tracking.cpython-311-darwin.so,sha256=Uhy-5yTJjbP-xERsiqbizdRCfS0ZsfaFmgp6CAGU0XY,191152
|
16
|
+
edsger/commons.pyx,sha256=rPWrq1zfN_IuTgLgvvQe8ma7bU1e3V8lWbBEFH5Vaw8,805
|
17
|
+
edsger/pq_4ary_dec_0b.pxd,sha256=MbQuP1y1t6_2fJRkpqRPV2lmatwfqOxjsrb1GLfEudA,1065
|
18
|
+
edsger/__init__.py,sha256=CoqO_GHq5NC94S2JrYjf6dVGOKnxJ2TRBDOjikyscOY,40
|
19
|
+
edsger/prefetch_compat.h,sha256=6HfoyHI0dQE_MDsYHjUiX77Hg_z8eH6kRwN_5RAhbYo,806
|
20
|
+
edsger/path_tracking.c,sha256=LESlwOk6leVuhrMy0CNN01Hgk_Bghi7G0oRBM8zuZrk,1144589
|
21
|
+
edsger/spiess_florian.pyx,sha256=9CrcMyzwSXaC_EdSl1uiD9yhFH6ACtooZOMfYw6vhvA,9566
|
22
|
+
edsger/pq_4ary_dec_0b.c,sha256=jNibkKCmxgC8Q_Y-lerC-SbqjIoDH7J6YgY6ohPJdLE,1367901
|
23
|
+
edsger/dijkstra.cpython-311-darwin.so,sha256=8cixMXjmFRYZtMAqQ1Bn-aMDQsCQOeyTMKf6C9A1HFc,366992
|
24
|
+
edsger/networks.py,sha256=o_dKC6fcxsqWr3fE0m5sUQkpZO-0B2x-w51l_1fyzAc,10316
|
25
|
+
edsger/commons.pxd,sha256=P9-n7ChbSIMSBRRY_lY3gBwXYUSvqJG-S_6e0dZTYU8,439
|
26
|
+
edsger/.gitignore,sha256=tWvEr3sBkY2ODntRp4_IUz3cidH-xu1efiOQK9DfK04,12
|
27
|
+
edsger/utils.py,sha256=C38-cgHUPf5XEjT7359vu5xO8hl-fYAZsYL_mcp8Gts,4601
|
28
|
+
edsger/pq_4ary_dec_0b.pyx,sha256=fRJCT79GsCocZgpostfn7t5nkNQALK0A93OJK4fxJR0,17796
|
29
|
+
edsger/commons.cpython-311-darwin.so,sha256=ERBQGGHVxdBdlO2TWSLHql-S6SZKtHPaXowj2aikGF4,58016
|
30
|
+
edsger/path_tracking.pyx,sha256=uO06fzL8V5KeI3w5FPVJJ13ZtdFGTol6oUjVX4Jjxxs,1807
|
31
|
+
edsger/star.cpython-311-darwin.so,sha256=ucRFvAIuEsinmmr5nFY2hbbZDazqU9Pl0PNKcwdL0G4,250648
|
32
|
+
edsger/path.py,sha256=MJTK4QJKfJnX0FjRC8HnDm0RDOKcoAOdY1a6mQq5LpU,57316
|
33
|
+
edsger/dijkstra.pyx,sha256=R8DM9LlHXUOZ38t7qINaB1t_QBP_aeWBOqIHHVN9W10,36513
|
34
|
+
edsger/star.pyx,sha256=MVS4kylmMAXOHm8liTfTMLzrr0jhA3cRU3-KCfVjNNM,9246
|
35
|
+
edsger/bellman_ford.cpython-311-darwin.so,sha256=8tTxOMqamB43XPboGfMU3j6ZyIiCs2wwyA155D5Qntw,250816
|
36
|
+
edsger/pq_4ary_dec_0b.cpython-311-darwin.so,sha256=C-1t3STU2-O-Fo5ArssE7ZR4TI6J4s4WqzhgTeEEzkI,214440
|
edsger-0.1.4.dist-info/RECORD
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
edsger-0.1.4.dist-info/RECORD,,
|
2
|
-
edsger-0.1.4.dist-info/WHEEL,sha256=qxQkdhERtGxJzqnVOBnucx1aUmU2n3HmuzYdln_LyOw,109
|
3
|
-
edsger-0.1.4.dist-info/top_level.txt,sha256=QvhzFORJIIot6GzSnDrtGa9KQt9iifCbOC5ULlzY5dg,7
|
4
|
-
edsger-0.1.4.dist-info/METADATA,sha256=W7VnBFB3jCX77cLnj1naJ-1BGzYatHsQ2DGoBk6Gbkk,5369
|
5
|
-
edsger-0.1.4.dist-info/licenses/LICENSE,sha256=eNjfz5CInLrVdczJbhazCKtb8-0qB0UaXZ3bXN0zio0,1111
|
6
|
-
edsger-0.1.4.dist-info/licenses/AUTHORS.rst,sha256=9lqpqjiC4XukK7jdxXwKJJrddqwCV2DjpYTqhpP6znA,105
|
7
|
-
edsger/spiess_florian.c,sha256=rkz11lkANig6lSuQ6pFBWtMdOXuM2uOYvx7VmwRQJ6w,1355775
|
8
|
-
edsger/dijkstra.c,sha256=_yF4vvqlSo00GQK1mTkqZFn0VmtILVECpBXOO3DcoT0,1657403
|
9
|
-
edsger/_version.py,sha256=JMD28FXYHc_TM03visyUSd3UA9FZAaJMRStnfZoq50Y,21
|
10
|
-
edsger/spiess_florian.cpython-311-darwin.so,sha256=Jh7BUGAa1vcLq6gqRxD1V_Y7SIA89KiZU9k2tLxfZkU,276312
|
11
|
-
edsger/commons.c,sha256=tdBzW_gCuLRoKR8kpvLLA25kKzA3e-_2xc8Nq-nPXo0,343009
|
12
|
-
edsger/star.c,sha256=i92m8J2fiypXVKRuWo3uPvLZfns5YynagtrYlLPewis,1319073
|
13
|
-
edsger/path_tracking.cpython-311-darwin.so,sha256=1rEt-_0dfUHeDT4rPXdn5O019WaclPrfaiotJWaZcgs,191200
|
14
|
-
edsger/commons.pyx,sha256=rPWrq1zfN_IuTgLgvvQe8ma7bU1e3V8lWbBEFH5Vaw8,805
|
15
|
-
edsger/pq_4ary_dec_0b.pxd,sha256=MbQuP1y1t6_2fJRkpqRPV2lmatwfqOxjsrb1GLfEudA,1065
|
16
|
-
edsger/__init__.py,sha256=CoqO_GHq5NC94S2JrYjf6dVGOKnxJ2TRBDOjikyscOY,40
|
17
|
-
edsger/prefetch_compat.h,sha256=6HfoyHI0dQE_MDsYHjUiX77Hg_z8eH6kRwN_5RAhbYo,806
|
18
|
-
edsger/path_tracking.c,sha256=CfibJn9D2rhfHKJsYJuGBETRhNNS4mAlQ0llS6mJGB0,1143939
|
19
|
-
edsger/spiess_florian.pyx,sha256=9CrcMyzwSXaC_EdSl1uiD9yhFH6ACtooZOMfYw6vhvA,9566
|
20
|
-
edsger/pq_4ary_dec_0b.c,sha256=NKUN0BMgirhv5zCyLjyuMhq7HaxkvI6mjBitxPFcuBU,1367251
|
21
|
-
edsger/dijkstra.cpython-311-darwin.so,sha256=sU7ItPPr393zN_E5y0uSCQgUfJSnyKmvU7sal0GUQ1M,367040
|
22
|
-
edsger/networks.py,sha256=o_dKC6fcxsqWr3fE0m5sUQkpZO-0B2x-w51l_1fyzAc,10316
|
23
|
-
edsger/commons.pxd,sha256=P9-n7ChbSIMSBRRY_lY3gBwXYUSvqJG-S_6e0dZTYU8,439
|
24
|
-
edsger/.gitignore,sha256=tWvEr3sBkY2ODntRp4_IUz3cidH-xu1efiOQK9DfK04,12
|
25
|
-
edsger/utils.py,sha256=igl2xDpkjTuQ5fhDdynQ0ekMSva52BstrTsZaTtE2RI,2034
|
26
|
-
edsger/pq_4ary_dec_0b.pyx,sha256=fRJCT79GsCocZgpostfn7t5nkNQALK0A93OJK4fxJR0,17796
|
27
|
-
edsger/commons.cpython-311-darwin.so,sha256=fGR441o1wyvlV1uOOcWLVOyP91J7D0pxWF3mbT7Ncko,58016
|
28
|
-
edsger/path_tracking.pyx,sha256=uO06fzL8V5KeI3w5FPVJJ13ZtdFGTol6oUjVX4Jjxxs,1807
|
29
|
-
edsger/star.cpython-311-darwin.so,sha256=xe-xzBtEBWmLN00fk-JdVcyBpY5FrywQ8I3bbVguyGc,250696
|
30
|
-
edsger/path.py,sha256=4hwoXiaepp_twwEQMmlygFsYy1HcxtZmR1HSvNStUC4,34874
|
31
|
-
edsger/dijkstra.pyx,sha256=R8DM9LlHXUOZ38t7qINaB1t_QBP_aeWBOqIHHVN9W10,36513
|
32
|
-
edsger/star.pyx,sha256=MVS4kylmMAXOHm8liTfTMLzrr0jhA3cRU3-KCfVjNNM,9246
|
33
|
-
edsger/pq_4ary_dec_0b.cpython-311-darwin.so,sha256=OS3RHv-FKNQQmfFzdg4Khf-wfBbAC1WtHMGZTNUc5yY,214488
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|