G = {'U':['V', 'X'],
'V':['Y'],
'X':['V'],
'Y':['X'],
'W':['Y', 'Z'],
'Z':['Z']
}
def DFS(G):
for u in G.keys():
G[u].append('white')
global time
time = 0
for u in G:
if 'white' in G[u]:
DFS_VISIT(G, u)
return G
def DFS_VISIT(G, u):
global time
global ud, uf
time = time + 1
ud = time
G[u].append(ud)
for i in range (len(G[u]) - 2, 0, -1):
if 'white' in G[u]:
G[u][i] = 'gray'
else:
continue
for v in G[u]:
if v != 'gray' and type(v) != int:
if 'white' in G[v]:
G[u].append(v)
DFS_VISIT(G, v)
else:
continue
time = time + 1
uf = time
G[u].append(uf)
for i in range (len(G[u]) - 4, 0, -1):
if 'gray' in G[u]:
G[u][i] = 'black'
print(DFS(G))
这个代码做的不是很完美,有一个bug 就是需要每个顶点都需要有边,之后我会改进
{'U': ['V', 'X', 'black', 1, 'V', 8], 'V': ['Y', 'black', 2, 'Y', 7], 'X': ['V', 'gray', 4, 5], 'Y': ['X', 'black', 3, 'X', 6], 'W': ['Y', 'Z', 'black', 9, 'Z', 12], 'Z': ['Z', 'gray', 10, 11]}
这是输出结果