mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@ -10,10 +10,10 @@ from modules import *
|
||||
|
||||
class GraphAdjList:
|
||||
""" 基于邻接表实现的无向图类 """
|
||||
def __init__(self, edges: List[List[Vertex]]) -> None:
|
||||
def __init__(self, edges: list[list[Vertex]]) -> None:
|
||||
""" 构造方法 """
|
||||
# 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
||||
self.adj_list: Dict = {}
|
||||
self.adj_list = dict[Vertex, Vertex]()
|
||||
# 添加所有顶点和边
|
||||
for edge in edges:
|
||||
self.add_vertex(edge[0])
|
||||
|
||||
@ -11,14 +11,14 @@ from modules import *
|
||||
class GraphAdjMat:
|
||||
""" 基于邻接矩阵实现的无向图类 """
|
||||
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
vertices: List[int] = []
|
||||
vertices: list[int] = []
|
||||
# 邻接矩阵,行列索引对应“顶点索引”
|
||||
adj_mat: List[List[int]] = []
|
||||
adj_mat: list[list[int]] = []
|
||||
|
||||
def __init__(self, vertices: List[int], edges: List[List[int]]) -> None:
|
||||
def __init__(self, vertices: list[int], edges: list[list[int]]) -> None:
|
||||
""" 构造方法 """
|
||||
self.vertices: List[int] = []
|
||||
self.adj_mat: List[List[int]] = []
|
||||
self.vertices: list[int] = []
|
||||
self.adj_mat: list[list[int]] = []
|
||||
# 添加顶点
|
||||
for val in vertices:
|
||||
self.add_vertex(val)
|
||||
@ -85,8 +85,8 @@ class GraphAdjMat:
|
||||
if __name__ == "__main__":
|
||||
""" 初始化无向图 """
|
||||
# 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
vertices: List[int] = [1, 3, 2, 5, 4]
|
||||
edges: List[List[int]] = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
||||
vertices: list[int] = [1, 3, 2, 5, 4]
|
||||
edges: list[list[int]] = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
||||
graph = GraphAdjMat(vertices, edges)
|
||||
print("\n初始化后,图为")
|
||||
graph.print()
|
||||
|
||||
@ -7,17 +7,18 @@ Author: Krahets (krahets@163.com)
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
from collections import deque
|
||||
from graph_adjacency_list import GraphAdjList
|
||||
|
||||
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> List[Vertex]:
|
||||
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
""" 广度优先遍历 BFS """
|
||||
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
# 顶点遍历序列
|
||||
res = []
|
||||
# 哈希表,用于记录已被访问过的顶点
|
||||
visited = set([start_vet])
|
||||
visited = set[Vertex]([start_vet])
|
||||
# 队列用于实现 BFS
|
||||
que = collections.deque([start_vet])
|
||||
que = deque[Vertex]([start_vet])
|
||||
# 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while len(que) > 0:
|
||||
vet = que.popleft() # 队首顶点出队
|
||||
|
||||
@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
from graph_adjacency_list import GraphAdjList
|
||||
|
||||
def dfs(graph: GraphAdjList, visited: Set[Vertex], res: List[Vertex], vet: Vertex):
|
||||
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):
|
||||
""" 深度优先遍历 DFS 辅助函数 """
|
||||
res.append(vet) # 记录访问顶点
|
||||
visited.add(vet) # 标记该顶点已被访问
|
||||
@ -21,12 +21,12 @@ def dfs(graph: GraphAdjList, visited: Set[Vertex], res: List[Vertex], vet: Verte
|
||||
dfs(graph, visited, res, adjVet)
|
||||
|
||||
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> List[Vertex]:
|
||||
def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
""" 深度优先遍历 DFS """
|
||||
# 顶点遍历序列
|
||||
res = []
|
||||
# 哈希表,用于记录已被访问过的顶点
|
||||
visited = set()
|
||||
visited = set[Vertex]()
|
||||
dfs(graph, visited, res, start_vet)
|
||||
return res
|
||||
|
||||
|
||||
Reference in New Issue
Block a user