Skip to content

Commit 01cb5ba

Browse files
authored
networkx: improve the shortest_paths.weighted module (#14508)
1 parent 10dba69 commit 01cb5ba

File tree

1 file changed

+55
-97
lines changed
  • stubs/networkx/networkx/algorithms/shortest_paths

1 file changed

+55
-97
lines changed
Lines changed: 55 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from _typeshed import Incomplete, SupportsGetItem
2-
from collections.abc import Callable, Generator
1+
from collections.abc import Callable, Collection, Generator
32
from typing import Any
3+
from typing_extensions import TypeAlias
44

55
from networkx.classes.graph import Graph, _Node
66
from networkx.utils.backends import _dispatchable
@@ -33,156 +33,114 @@ __all__ = [
3333
"johnson",
3434
]
3535

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+
3641
@_dispatchable
3742
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]: ...
4345
@_dispatchable
4446
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: ...
5049
@_dispatchable
5150
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]]: ...
5753
@_dispatchable
5854
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]: ...
6457
@_dispatchable
6558
def single_source_dijkstra(
6659
G: Graph[_Node],
6760
source: _Node,
6861
target: _Node | None = None,
6962
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
7265
@_dispatchable
7366
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]]: ...
7969
@_dispatchable
8070
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]: ...
8673
@_dispatchable
8774
def multi_source_dijkstra(
8875
G: Graph[_Node],
89-
sources,
76+
sources: Collection[_Node],
9077
target: _Node | None = None,
9178
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
9481
@_dispatchable
9582
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]]: ...
10185
@_dispatchable
10286
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]]]]]: ...
10789
@_dispatchable
10890
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]]]: ...
11393
@_dispatchable
11494
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]]]]: ...
11997
@_dispatchable
12098
def bellman_ford_predecessor_and_distance(
12199
G: Graph[_Node],
122100
source: _Node,
123101
target: _Node | None = None,
124-
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
102+
weight: str | _WeightFunc[_Node] | None = "weight",
125103
heuristic: bool = False,
126-
) -> tuple[Incomplete, Incomplete]: ...
104+
) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ...
127105
@_dispatchable
128106
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]: ...
134109
@_dispatchable
135110
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: ...
141113
@_dispatchable
142114
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]]: ...
145117
@_dispatchable
146118
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]: ...
149121
@_dispatchable
150122
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
156125
@_dispatchable
157126
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]]]: ...
160129
@_dispatchable
161130
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]]]]: ...
164133
@_dispatchable
165134
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]]: ...
168137
@_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: ...
174139
@_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]: ...
178141
@_dispatchable
179142
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]]: ...
185145
@_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

Comments
 (0)