@@ -15,8 +15,8 @@ module.exports = () => ({
15
15
{
16
16
language : "py" ,
17
17
desc : `
18
-
19
- 比如一个图是这样的:
18
+ ${ t ( "Locale.codeTemplate.graph.item1_desc1" ) }
19
+
20
20
21
21
\`\`\`
22
22
E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F
@@ -25,7 +25,8 @@ E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F
25
25
-------- 2 ---------> G ------- 1 ------
26
26
\`\`\`
27
27
28
- 我们使用邻接矩阵来构造:
28
+ ${ t ( "Locale.codeTemplate.graph.item1_desc2" ) }
29
+
29
30
30
31
\`\`\`py
31
32
G = {
@@ -47,6 +48,7 @@ import heapq
47
48
48
49
def dijkstra(graph, start, end):
49
50
# 堆里的数据都是 (cost, i) 的二元祖,其含义是“从 start 走到 i 的距离是 cost”。
51
+ # The data in the heap consists of tuples (cost, i), where it signifies "the distance from start to i is cost".
50
52
heap = [(0, start)]
51
53
visited = set()
52
54
while heap:
@@ -79,18 +81,24 @@ def dijkstra(graph, start, end):
79
81
language : "py" ,
80
82
text : `
81
83
# graph 是邻接矩阵,n 是顶点个数
84
+ # The graph is represented as an adjacency matrix, where n represents the number of vertices.
85
+
82
86
# graph 形如: graph[u][v] = w
87
+ # graph is like: graph[u][v] = w
83
88
def floyd_warshall(graph, n):
84
89
dist = [[float("inf") for _ in range(n)] for _ in range(n)]
85
90
86
91
for i in range(n):
87
92
for j in range(n):
88
93
dist[i][j] = graph[i][j]
89
94
95
+ # 将顶点k与所有其他顶点(i, j)进行比较
90
96
# check vertex k against all other vertices (i, j)
91
97
for k in range(n):
98
+ # 循环遍历图数组的行
92
99
# looping through rows of graph array
93
100
for i in range(n):
101
+ # 循环遍历图数组的列
94
102
# looping through columns of graph array
95
103
for j in range(n):
96
104
if (
@@ -116,7 +124,9 @@ def floyd_warshall(graph, n):
116
124
{
117
125
language : "py" ,
118
126
text : `
127
+ # 如果不存在,返回-1
119
128
# return -1 for not exsit
129
+ # 如果存在,返回 dis map,dis[v]表示从点s到点v的最小花费
120
130
# else return dis map where dis[v] means for point s the least cost to point v
121
131
def bell_man(edges, s):
122
132
dis = defaultdict(lambda: math.inf)
@@ -245,7 +255,8 @@ def PrimsAlgorithm(l): # noqa: E741
245
255
set_position(positions[start], temp)
246
256
247
257
top_to_bottom(heap, m, size, positions)
248
-
258
+
259
+ # 如果最小堆中任意节点的值减小,则更新函数
249
260
# Update function if value of any node in min-heap decreases
250
261
def bottom_to_top(val, index, heap, position):
251
262
temp = position[index]
@@ -283,10 +294,16 @@ def PrimsAlgorithm(l): # noqa: E741
283
294
return temp
284
295
285
296
visited = [0 for i in range(len(l))]
286
- Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex
297
+ # 所选顶点的邻近树顶点
298
+ # Neighboring Tree Vertex of selected vertex
299
+ Nbr_TV = [-1 for i in range(len(l))]
300
+ # 部分树的探索顶点到邻近顶点的最小距离
287
301
# Minimum Distance of explored vertex with neighboring vertex of partial tree
302
+ # 以图表形式呈现
288
303
# formed in graph
289
- Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex
304
+ # 堆顶点到相邻顶点的距离
305
+ # Heap of Distance of vertices from their neighboring vertex
306
+ Distance_TV = []
290
307
Positions = []
291
308
292
309
for x in range(len(l)):
@@ -339,8 +356,9 @@ if __name__ == "__main__": # pragma: no cover
339
356
text : `
340
357
def topologicalSort(graph):
341
358
"""
359
+ Kahn算法是使用广度优先搜索(BFS)来找到有向无环图(Directed Acyclic Graph)的拓扑排序的算法。
342
360
Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph
343
- using BFS
361
+ using BFS.
344
362
"""
345
363
indegree = [0] * len(graph)
346
364
queue = collections.deque([])
@@ -369,7 +387,7 @@ def topologicalSort(graph):
369
387
else:
370
388
print(topo)
371
389
372
-
390
+ # 图的邻接表
373
391
# Adjacency List of Graph
374
392
graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []}
375
393
topologicalSort(graph)
0 commit comments