-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdfs.py
56 lines (54 loc) · 1.04 KB
/
dfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
第3章里面的DFS就是个概念模型,没有输出= =用不来的= =
因此这部分按照《Python Algorithms》介绍的方法来。
顺带一提,《Python Algorithms》是本好书= =
约定:图的存储方法
这里的图采用邻接表的方法存储。有两种可行的方式:
1. 边无权重:
采用集合存储该点可到达的点集
2. 边有权重:
采用字典(点:边的权重)存储该点可到达的点集
例:
a, b, c, d, e, f, g, h = range(8)
G = [
set([b, c, f]),
set([e]),
set([d]),
set([a, h]),
set([f, g, h]),
set([b, g]),
set(),
set([g])
]
"""
def rec_dfs(G, s, S=None):
if S is None:
S = set()
S.add(s)
for u in G[s]:
if u in S:
continue
rec_dfs(G, u, S)
def iter_dfs(G, s):
S, Q = set(), []
Q.append(s)
while Q:
u = Q.pop()
if u in S:
continue
S.add(u)
Q.extend(G[u])
yield u
def dfs(G, s, pre, post, S=None, t=0):
if S is None:
S = set()
pre[s] = t
t += 1
S.add(s)
for u in G[s]:
if u in S:
continue
t = dfs(G, u, pre, post, S, t)
post[s] = t
t += 1
return t