|
1 |
| -from _typeshed import Incomplete, SupportsGetItem |
2 |
| -from collections.abc import Callable, Generator |
| 1 | +from collections.abc import Callable, Collection, Generator |
3 | 2 | from typing import Any
|
| 3 | +from typing_extensions import TypeAlias |
4 | 4 |
|
5 | 5 | from networkx.classes.graph import Graph, _Node
|
6 | 6 | from networkx.utils.backends import _dispatchable
|
@@ -33,156 +33,114 @@ __all__ = [
|
33 | 33 | "johnson",
|
34 | 34 | ]
|
35 | 35 |
|
| 36 | +_WeightFunc: TypeAlias = Callable[ |
| 37 | + [_Node, _Node, dict[str, Any]], # Any: type of edge data cannot be known statically |
| 38 | + float | None, # the weight or None to indicate a hidden edge |
| 39 | +] |
| 40 | + |
36 | 41 | @_dispatchable
|
37 | 42 | def dijkstra_path(
|
38 |
| - G: Graph[_Node], |
39 |
| - source: _Node, |
40 |
| - target: _Node, |
41 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
42 |
| -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... |
| 43 | + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 44 | +) -> list[_Node]: ... |
43 | 45 | @_dispatchable
|
44 | 46 | def dijkstra_path_length(
|
45 |
| - G: Graph[_Node], |
46 |
| - source: _Node, |
47 |
| - target: _Node, |
48 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
49 |
| -): ... |
| 47 | + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 48 | +) -> float: ... |
50 | 49 | @_dispatchable
|
51 | 50 | def single_source_dijkstra_path(
|
52 |
| - G: Graph[_Node], |
53 |
| - source: _Node, |
54 |
| - cutoff: float | None = None, |
55 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
56 |
| -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... |
| 51 | + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 52 | +) -> dict[_Node, list[_Node]]: ... |
57 | 53 | @_dispatchable
|
58 | 54 | def single_source_dijkstra_path_length(
|
59 |
| - G: Graph[_Node], |
60 |
| - source: _Node, |
61 |
| - cutoff: float | None = None, |
62 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
63 |
| -) -> dict[Incomplete, Incomplete]: ... |
| 55 | + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 56 | +) -> dict[_Node, float]: ... |
64 | 57 | @_dispatchable
|
65 | 58 | def single_source_dijkstra(
|
66 | 59 | G: Graph[_Node],
|
67 | 60 | source: _Node,
|
68 | 61 | target: _Node | None = None,
|
69 | 62 | cutoff: float | None = None,
|
70 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
71 |
| -) -> tuple[Incomplete, Incomplete]: ... |
| 63 | + weight: str | _WeightFunc[_Node] | None = "weight", |
| 64 | +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target |
72 | 65 | @_dispatchable
|
73 | 66 | def multi_source_dijkstra_path(
|
74 |
| - G: Graph[_Node], |
75 |
| - sources, |
76 |
| - cutoff: float | None = None, |
77 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
78 |
| -) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ... |
| 67 | + G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 68 | +) -> dict[_Node, list[_Node]]: ... |
79 | 69 | @_dispatchable
|
80 | 70 | def multi_source_dijkstra_path_length(
|
81 |
| - G: Graph[_Node], |
82 |
| - sources, |
83 |
| - cutoff: float | None = None, |
84 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
85 |
| -) -> dict[Incomplete, Incomplete]: ... |
| 71 | + G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 72 | +) -> dict[_Node, float]: ... |
86 | 73 | @_dispatchable
|
87 | 74 | def multi_source_dijkstra(
|
88 | 75 | G: Graph[_Node],
|
89 |
| - sources, |
| 76 | + sources: Collection[_Node], |
90 | 77 | target: _Node | None = None,
|
91 | 78 | cutoff: float | None = None,
|
92 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
93 |
| -) -> tuple[Incomplete, Incomplete]: ... |
| 79 | + weight: str | _WeightFunc[_Node] | None = "weight", |
| 80 | +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target |
94 | 81 | @_dispatchable
|
95 | 82 | def dijkstra_predecessor_and_distance(
|
96 |
| - G: Graph[_Node], |
97 |
| - source: _Node, |
98 |
| - cutoff: float | None = None, |
99 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
100 |
| -) -> tuple[dict[Incomplete, list[Incomplete]], dict[Incomplete, Incomplete]]: ... |
| 83 | + G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 84 | +) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ... |
101 | 85 | @_dispatchable
|
102 | 86 | def all_pairs_dijkstra(
|
103 |
| - G: Graph[_Node], |
104 |
| - cutoff: float | None = None, |
105 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
106 |
| -) -> Generator[Incomplete, None, None]: ... |
| 87 | + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 88 | +) -> Generator[tuple[_Node, tuple[dict[_Node, float], dict[_Node, list[_Node]]]]]: ... |
107 | 89 | @_dispatchable
|
108 | 90 | def all_pairs_dijkstra_path_length(
|
109 |
| - G: Graph[_Node], |
110 |
| - cutoff: float | None = None, |
111 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
112 |
| -) -> Generator[Incomplete, None, None]: ... |
| 91 | + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 92 | +) -> Generator[tuple[_Node, dict[_Node, float]]]: ... |
113 | 93 | @_dispatchable
|
114 | 94 | def all_pairs_dijkstra_path(
|
115 |
| - G: Graph[_Node], |
116 |
| - cutoff: float | None = None, |
117 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
118 |
| -) -> Generator[tuple[Incomplete, Incomplete], None, None]: ... |
| 95 | + G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 96 | +) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ... |
119 | 97 | @_dispatchable
|
120 | 98 | def bellman_ford_predecessor_and_distance(
|
121 | 99 | G: Graph[_Node],
|
122 | 100 | source: _Node,
|
123 | 101 | target: _Node | None = None,
|
124 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
| 102 | + weight: str | _WeightFunc[_Node] | None = "weight", |
125 | 103 | heuristic: bool = False,
|
126 |
| -) -> tuple[Incomplete, Incomplete]: ... |
| 104 | +) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ... |
127 | 105 | @_dispatchable
|
128 | 106 | def bellman_ford_path(
|
129 |
| - G: Graph[_Node], |
130 |
| - source: _Node, |
131 |
| - target: _Node, |
132 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
133 |
| -) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ... |
| 107 | + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 108 | +) -> list[_Node]: ... |
134 | 109 | @_dispatchable
|
135 | 110 | def bellman_ford_path_length(
|
136 |
| - G: Graph[_Node], |
137 |
| - source: _Node, |
138 |
| - target: _Node, |
139 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
140 |
| -): ... |
| 111 | + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 112 | +) -> float: ... |
141 | 113 | @_dispatchable
|
142 | 114 | def single_source_bellman_ford_path(
|
143 |
| - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
144 |
| -) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ... |
| 115 | + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 116 | +) -> dict[_Node, list[_Node]]: ... |
145 | 117 | @_dispatchable
|
146 | 118 | def single_source_bellman_ford_path_length(
|
147 |
| - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
148 |
| -) -> dict[Incomplete, int]: ... |
| 119 | + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 120 | +) -> dict[_Node, float]: ... |
149 | 121 | @_dispatchable
|
150 | 122 | def single_source_bellman_ford(
|
151 |
| - G: Graph[_Node], |
152 |
| - source: _Node, |
153 |
| - target: _Node | None = None, |
154 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
155 |
| -): ... |
| 123 | + G: Graph[_Node], source: _Node, target: _Node | None = None, weight: str | _WeightFunc[_Node] | None = "weight" |
| 124 | +) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target |
156 | 125 | @_dispatchable
|
157 | 126 | def all_pairs_bellman_ford_path_length(
|
158 |
| - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
159 |
| -) -> Generator[Incomplete, None, None]: ... |
| 127 | + G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight" |
| 128 | +) -> Generator[tuple[_Node, dict[_Node, float]]]: ... |
160 | 129 | @_dispatchable
|
161 | 130 | def all_pairs_bellman_ford_path(
|
162 |
| - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
163 |
| -) -> Generator[tuple[Incomplete, Incomplete], None, None]: ... |
| 131 | + G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight" |
| 132 | +) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ... |
164 | 133 | @_dispatchable
|
165 | 134 | def goldberg_radzik(
|
166 |
| - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
167 |
| -) -> tuple[dict[Incomplete, None], dict[Incomplete, int | float]]: ... |
| 135 | + G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 136 | +) -> tuple[dict[_Node, _Node | None], dict[_Node, float]]: ... |
168 | 137 | @_dispatchable
|
169 |
| -def negative_edge_cycle( |
170 |
| - G: Graph[_Node], |
171 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
172 |
| - heuristic: bool = True, |
173 |
| -): ... |
| 138 | +def negative_edge_cycle(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight", heuristic: bool = True) -> bool: ... |
174 | 139 | @_dispatchable
|
175 |
| -def find_negative_cycle( |
176 |
| - G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
177 |
| -): ... |
| 140 | +def find_negative_cycle(G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight") -> list[_Node]: ... |
178 | 141 | @_dispatchable
|
179 | 142 | def bidirectional_dijkstra(
|
180 |
| - G: Graph[_Node], |
181 |
| - source: _Node, |
182 |
| - target: _Node, |
183 |
| - weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight", |
184 |
| -): ... |
| 143 | + G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight" |
| 144 | +) -> tuple[float, list[_Node]]: ... |
185 | 145 | @_dispatchable
|
186 |
| -def johnson( |
187 |
| - G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight" |
188 |
| -) -> dict[Any, dict[Any, list[Any]]]: ... |
| 146 | +def johnson(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight") -> dict[_Node, dict[_Node, list[_Node]]]: ... |
0 commit comments