mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
from __future__ import annotations (#2464)
* from __future__ import annotations
* fixup! from __future__ import annotations
* fixup! from __future__ import annotations
* fixup! Format Python code with psf/black push
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Dict, List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def printDist(dist, V):
|
||||
@@ -7,7 +7,7 @@ def printDist(dist, V):
|
||||
print("\t".join(f"{i}\t{d}" for i, d in enumerate(distances)))
|
||||
|
||||
|
||||
def BellmanFord(graph: List[Dict[str, int]], V: int, E: int, src: int) -> int:
|
||||
def BellmanFord(graph: list[dict[str, int]], V: int, E: int, src: int) -> int:
|
||||
"""
|
||||
Returns shortest paths from a vertex src to all
|
||||
other vertices.
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
https://en.wikipedia.org/wiki/Bidirectional_search
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from math import sqrt
|
||||
from typing import List, Tuple
|
||||
|
||||
# 1 for manhattan, 0 for euclidean
|
||||
HEURISTIC = 0
|
||||
@@ -89,7 +90,7 @@ class AStar:
|
||||
|
||||
self.reached = False
|
||||
|
||||
def search(self) -> List[Tuple[int]]:
|
||||
def search(self) -> list[tuple[int]]:
|
||||
while self.open_nodes:
|
||||
# Open Nodes are sorted using __lt__
|
||||
self.open_nodes.sort()
|
||||
@@ -120,7 +121,7 @@ class AStar:
|
||||
if not (self.reached):
|
||||
return [(self.start.pos)]
|
||||
|
||||
def get_successors(self, parent: Node) -> List[Node]:
|
||||
def get_successors(self, parent: Node) -> list[Node]:
|
||||
"""
|
||||
Returns a list of successors (both in the grid and free spaces)
|
||||
"""
|
||||
@@ -146,7 +147,7 @@ class AStar:
|
||||
)
|
||||
return successors
|
||||
|
||||
def retrace_path(self, node: Node) -> List[Tuple[int]]:
|
||||
def retrace_path(self, node: Node) -> list[tuple[int]]:
|
||||
"""
|
||||
Retrace the path from parents to parents until start node
|
||||
"""
|
||||
@@ -177,7 +178,7 @@ class BidirectionalAStar:
|
||||
self.bwd_astar = AStar(goal, start)
|
||||
self.reached = False
|
||||
|
||||
def search(self) -> List[Tuple[int]]:
|
||||
def search(self) -> list[tuple[int]]:
|
||||
while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes:
|
||||
self.fwd_astar.open_nodes.sort()
|
||||
self.bwd_astar.open_nodes.sort()
|
||||
@@ -224,7 +225,7 @@ class BidirectionalAStar:
|
||||
|
||||
def retrace_bidirectional_path(
|
||||
self, fwd_node: Node, bwd_node: Node
|
||||
) -> List[Tuple[int]]:
|
||||
) -> list[tuple[int]]:
|
||||
fwd_path = self.fwd_astar.retrace_path(fwd_node)
|
||||
bwd_path = self.bwd_astar.retrace_path(bwd_node)
|
||||
bwd_path.pop()
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
https://en.wikipedia.org/wiki/Bidirectional_search
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import List, Tuple
|
||||
|
||||
grid = [
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
@@ -51,7 +52,7 @@ class BreadthFirstSearch:
|
||||
self.node_queue = [self.start]
|
||||
self.reached = False
|
||||
|
||||
def search(self) -> List[Tuple[int]]:
|
||||
def search(self) -> list[tuple[int]]:
|
||||
while self.node_queue:
|
||||
current_node = self.node_queue.pop(0)
|
||||
|
||||
@@ -67,7 +68,7 @@ class BreadthFirstSearch:
|
||||
if not (self.reached):
|
||||
return [(self.start.pos)]
|
||||
|
||||
def get_successors(self, parent: Node) -> List[Node]:
|
||||
def get_successors(self, parent: Node) -> list[Node]:
|
||||
"""
|
||||
Returns a list of successors (both in the grid and free spaces)
|
||||
"""
|
||||
@@ -86,7 +87,7 @@ class BreadthFirstSearch:
|
||||
)
|
||||
return successors
|
||||
|
||||
def retrace_path(self, node: Node) -> List[Tuple[int]]:
|
||||
def retrace_path(self, node: Node) -> list[tuple[int]]:
|
||||
"""
|
||||
Retrace the path from parents to parents until start node
|
||||
"""
|
||||
@@ -118,7 +119,7 @@ class BidirectionalBreadthFirstSearch:
|
||||
self.bwd_bfs = BreadthFirstSearch(goal, start)
|
||||
self.reached = False
|
||||
|
||||
def search(self) -> List[Tuple[int]]:
|
||||
def search(self) -> list[tuple[int]]:
|
||||
while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue:
|
||||
current_fwd_node = self.fwd_bfs.node_queue.pop(0)
|
||||
current_bwd_node = self.bwd_bfs.node_queue.pop(0)
|
||||
@@ -146,7 +147,7 @@ class BidirectionalBreadthFirstSearch:
|
||||
|
||||
def retrace_bidirectional_path(
|
||||
self, fwd_node: Node, bwd_node: Node
|
||||
) -> List[Tuple[int]]:
|
||||
) -> list[tuple[int]]:
|
||||
fwd_path = self.fwd_bfs.retrace_path(fwd_node)
|
||||
bwd_path = self.bwd_bfs.retrace_path(bwd_node)
|
||||
bwd_path.pop()
|
||||
|
||||
@@ -12,7 +12,7 @@ while Q is non-empty:
|
||||
mark w as explored
|
||||
add w to Q (at the end)
|
||||
"""
|
||||
from typing import Dict, Set
|
||||
from __future__ import annotations
|
||||
|
||||
G = {
|
||||
"A": ["B", "C"],
|
||||
@@ -24,7 +24,7 @@ G = {
|
||||
}
|
||||
|
||||
|
||||
def breadth_first_search(graph: Dict, start: str) -> Set[str]:
|
||||
def breadth_first_search(graph: dict, start: str) -> set[str]:
|
||||
"""
|
||||
>>> ''.join(sorted(breadth_first_search(G, 'A')))
|
||||
'ABCDEF'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Breath First Search (BFS) can be used when finding the shortest path
|
||||
from a given source node to a target node in an unweighted graph.
|
||||
"""
|
||||
from typing import Dict
|
||||
from __future__ import annotations
|
||||
|
||||
graph = {
|
||||
"A": ["B", "C", "E"],
|
||||
@@ -15,7 +15,7 @@ graph = {
|
||||
|
||||
|
||||
class Graph:
|
||||
def __init__(self, graph: Dict[str, str], source_vertex: str) -> None:
|
||||
def __init__(self, graph: dict[str, str], source_vertex: str) -> None:
|
||||
"""Graph is implemented as dictionary of adjacency lists. Also,
|
||||
Source vertex have to be defined upon initialization.
|
||||
"""
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""Non recursive implementation of a DFS algorithm."""
|
||||
|
||||
from typing import Dict, Set
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def depth_first_search(graph: Dict, start: str) -> Set[int]:
|
||||
def depth_first_search(graph: dict, start: str) -> set[int]:
|
||||
"""Depth First Search on Graph
|
||||
:param graph: directed graph in dictionary format
|
||||
:param vertex: starting vertex as a string
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def stable_matching(donor_pref: List[int], recipient_pref: List[int]) -> List[int]:
|
||||
def stable_matching(donor_pref: list[int], recipient_pref: list[int]) -> list[int]:
|
||||
"""
|
||||
Finds the stable match in any bipartite graph, i.e a pairing where no 2 objects
|
||||
prefer each other over their partner. The function accepts the preferences of
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
https://en.wikipedia.org/wiki/Best-first_search#Greedy_BFS
|
||||
"""
|
||||
|
||||
from typing import List, Tuple
|
||||
from __future__ import annotations
|
||||
|
||||
grid = [
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
@@ -81,7 +81,7 @@ class GreedyBestFirst:
|
||||
|
||||
self.reached = False
|
||||
|
||||
def search(self) -> List[Tuple[int]]:
|
||||
def search(self) -> list[tuple[int]]:
|
||||
"""
|
||||
Search for the path,
|
||||
if a path is not found, only the starting position is returned
|
||||
@@ -116,7 +116,7 @@ class GreedyBestFirst:
|
||||
if not (self.reached):
|
||||
return [self.start.pos]
|
||||
|
||||
def get_successors(self, parent: Node) -> List[Node]:
|
||||
def get_successors(self, parent: Node) -> list[Node]:
|
||||
"""
|
||||
Returns a list of successors (both in the grid and free spaces)
|
||||
"""
|
||||
@@ -143,7 +143,7 @@ class GreedyBestFirst:
|
||||
)
|
||||
return successors
|
||||
|
||||
def retrace_path(self, node: Node) -> List[Tuple[int]]:
|
||||
def retrace_path(self, node: Node) -> list[tuple[int]]:
|
||||
"""
|
||||
Retrace the path from parents to parents until start node
|
||||
"""
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
An implementation of Karger's Algorithm for partitioning a graph.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from typing import Dict, List, Set, Tuple
|
||||
|
||||
# Adjacency list representation of this graph:
|
||||
# https://en.wikipedia.org/wiki/File:Single_run_of_Karger%E2%80%99s_Mincut_algorithm.svg
|
||||
@@ -21,7 +22,7 @@ TEST_GRAPH = {
|
||||
}
|
||||
|
||||
|
||||
def partition_graph(graph: Dict[str, List[str]]) -> Set[Tuple[str, str]]:
|
||||
def partition_graph(graph: dict[str, list[str]]) -> set[tuple[str, str]]:
|
||||
"""
|
||||
Partitions a graph using Karger's Algorithm. Implemented from
|
||||
pseudocode found here:
|
||||
@@ -60,9 +61,7 @@ def partition_graph(graph: Dict[str, List[str]]) -> Set[Tuple[str, str]]:
|
||||
for neighbor in uv_neighbors:
|
||||
graph_copy[neighbor].append(uv)
|
||||
|
||||
contracted_nodes[uv] = {
|
||||
node for node in contracted_nodes[u].union(contracted_nodes[v])
|
||||
}
|
||||
contracted_nodes[uv] = set(contracted_nodes[u].union(contracted_nodes[v]))
|
||||
|
||||
# Remove nodes u and v.
|
||||
del graph_copy[u]
|
||||
|
||||
@@ -100,7 +100,7 @@ def prim_heap(graph: list, root: Vertex) -> Iterator[tuple]:
|
||||
u.pi = None
|
||||
root.key = 0
|
||||
|
||||
h = [v for v in graph]
|
||||
h = list(graph)
|
||||
hq.heapify(h)
|
||||
|
||||
while h:
|
||||
|
||||
Reference in New Issue
Block a user