Skip to content

networkx: improve the shortest_paths.weighted module #14508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 55 additions & 97 deletions stubs/networkx/networkx/algorithms/shortest_paths/weighted.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from _typeshed import Incomplete, SupportsGetItem
from collections.abc import Callable, Generator
from collections.abc import Callable, Collection, Generator
from typing import Any
from typing_extensions import TypeAlias

from networkx.classes.graph import Graph, _Node
from networkx.utils.backends import _dispatchable
Expand Down Expand Up @@ -33,156 +33,114 @@ __all__ = [
"johnson",
]

_WeightFunc: TypeAlias = Callable[
[_Node, _Node, dict[str, Any]], # Any: type of edge data cannot be known statically
float | None, # the weight or None to indicate a hidden edge
]

@_dispatchable
def dijkstra_path(
G: Graph[_Node],
source: _Node,
target: _Node,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ...
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> list[_Node]: ...
@_dispatchable
def dijkstra_path_length(
G: Graph[_Node],
source: _Node,
target: _Node,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
): ...
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> float: ...
@_dispatchable
def single_source_dijkstra_path(
G: Graph[_Node],
source: _Node,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ...
G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, list[_Node]]: ...
@_dispatchable
def single_source_dijkstra_path_length(
G: Graph[_Node],
source: _Node,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> dict[Incomplete, Incomplete]: ...
G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, float]: ...
@_dispatchable
def single_source_dijkstra(
G: Graph[_Node],
source: _Node,
target: _Node | None = None,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> tuple[Incomplete, Incomplete]: ...
weight: str | _WeightFunc[_Node] | None = "weight",
) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target
@_dispatchable
def multi_source_dijkstra_path(
G: Graph[_Node],
sources,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> dict[Incomplete, list[Incomplete]] | list[Incomplete]: ...
G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, list[_Node]]: ...
@_dispatchable
def multi_source_dijkstra_path_length(
G: Graph[_Node],
sources,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> dict[Incomplete, Incomplete]: ...
G: Graph[_Node], sources: Collection[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, float]: ...
@_dispatchable
def multi_source_dijkstra(
G: Graph[_Node],
sources,
sources: Collection[_Node],
target: _Node | None = None,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> tuple[Incomplete, Incomplete]: ...
weight: str | _WeightFunc[_Node] | None = "weight",
) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target
@_dispatchable
def dijkstra_predecessor_and_distance(
G: Graph[_Node],
source: _Node,
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> tuple[dict[Incomplete, list[Incomplete]], dict[Incomplete, Incomplete]]: ...
G: Graph[_Node], source: _Node, cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ...
@_dispatchable
def all_pairs_dijkstra(
G: Graph[_Node],
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> Generator[Incomplete, None, None]: ...
G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> Generator[tuple[_Node, tuple[dict[_Node, float], dict[_Node, list[_Node]]]]]: ...
@_dispatchable
def all_pairs_dijkstra_path_length(
G: Graph[_Node],
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> Generator[Incomplete, None, None]: ...
G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> Generator[tuple[_Node, dict[_Node, float]]]: ...
@_dispatchable
def all_pairs_dijkstra_path(
G: Graph[_Node],
cutoff: float | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> Generator[tuple[Incomplete, Incomplete], None, None]: ...
G: Graph[_Node], cutoff: float | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ...
@_dispatchable
def bellman_ford_predecessor_and_distance(
G: Graph[_Node],
source: _Node,
target: _Node | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
weight: str | _WeightFunc[_Node] | None = "weight",
heuristic: bool = False,
) -> tuple[Incomplete, Incomplete]: ...
) -> tuple[dict[_Node, list[_Node]], dict[_Node, float]]: ...
@_dispatchable
def bellman_ford_path(
G: Graph[_Node],
source: _Node,
target: _Node,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ...
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> list[_Node]: ...
@_dispatchable
def bellman_ford_path_length(
G: Graph[_Node],
source: _Node,
target: _Node,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
): ...
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> float: ...
@_dispatchable
def single_source_bellman_ford_path(
G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> list[Incomplete] | dict[Incomplete, list[Incomplete]]: ...
G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, list[_Node]]: ...
@_dispatchable
def single_source_bellman_ford_path_length(
G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> dict[Incomplete, int]: ...
G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> dict[_Node, float]: ...
@_dispatchable
def single_source_bellman_ford(
G: Graph[_Node],
source: _Node,
target: _Node | None = None,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
): ...
G: Graph[_Node], source: _Node, target: _Node | None = None, weight: str | _WeightFunc[_Node] | None = "weight"
) -> tuple[dict[_Node, float], dict[_Node, list[_Node]]] | tuple[float, list[_Node]]: ... # TODO: overload on target
@_dispatchable
def all_pairs_bellman_ford_path_length(
G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> Generator[Incomplete, None, None]: ...
G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight"
) -> Generator[tuple[_Node, dict[_Node, float]]]: ...
@_dispatchable
def all_pairs_bellman_ford_path(
G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> Generator[tuple[Incomplete, Incomplete], None, None]: ...
G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight"
) -> Generator[tuple[_Node, dict[_Node, list[_Node]]]]: ...
@_dispatchable
def goldberg_radzik(
G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> tuple[dict[Incomplete, None], dict[Incomplete, int | float]]: ...
G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> tuple[dict[_Node, _Node | None], dict[_Node, float]]: ...
@_dispatchable
def negative_edge_cycle(
G: Graph[_Node],
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
heuristic: bool = True,
): ...
def negative_edge_cycle(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight", heuristic: bool = True) -> bool: ...
@_dispatchable
def find_negative_cycle(
G: Graph[_Node], source: _Node, weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
): ...
def find_negative_cycle(G: Graph[_Node], source: _Node, weight: str | _WeightFunc[_Node] | None = "weight") -> list[_Node]: ...
@_dispatchable
def bidirectional_dijkstra(
G: Graph[_Node],
source: _Node,
target: _Node,
weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight",
): ...
G: Graph[_Node], source: _Node, target: _Node, weight: str | _WeightFunc[_Node] | None = "weight"
) -> tuple[float, list[_Node]]: ...
@_dispatchable
def johnson(
G: Graph[_Node], weight: str | Callable[[Any, Any, SupportsGetItem[str, Any]], float | None] | None = "weight"
) -> dict[Any, dict[Any, list[Any]]]: ...
def johnson(G: Graph[_Node], weight: str | _WeightFunc[_Node] | None = "weight") -> dict[_Node, dict[_Node, list[_Node]]]: ...
Loading