import enum
import collections
class BuildGraphic:
def __init__(self):
self.G = dict()
self.G['u'] = ['v', 'x']
self.G['x'] = ['v']
self.G['v'] = ['y']
self.G['y'] = ['x']
self.G['w'] = ['y', 'z']
self.G['z'] = ['z']
def get(self):
return self.G
class DFS:
Color = enum.Enum('Color', 'WHITE GRAY BLACK')
class VInfo:
def __init__(self):
self.color = DFS.Color.WHITE
self.parent = None
self.d = int()
self.f = int()
def __repr__(self):
return '(%s, %s, %d, %d)' % (self.color, self.parent, self.d, self.f)
def __init__(self, G):
self.G = G
self.time = 0
self.VerToVInfo = { key : DFS.VInfo() for key in self.G }
def __call__(self, *args, **kwargs):
for s in self.G:
self.visit(s)
return self
def visit(self, u):
if self.VerToVInfo[u].color != DFS.Color.WHITE:
return
self.VerToVInfo[u].color = DFS.Color.GRAY
self.time += 1
self.VerToVInfo[u].d = self.time
for v in self.G[u]:
self.VerToVInfo[v].parent = u
self.visit(v)
self.time += 1
self.VerToVInfo[u].f = self.time
self.VerToVInfo[u].color = DFS.Color.BLACK
def get_vinfo(self):
return self.VerToVInfo
if __name__ == '__main__':
G = BuildGraphic()
print(DFS(G.get())().get_vinfo())
DFS
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Neil Zhu,简书ID Not_GOD,University AI 创始人 & Chief Scientist...
- 译自《Breadth First Traversal or BFS for a Graph》 和 《Depth F...
- 好几个月前发过这篇程序员漫画出本决定!我来售卖本子预订金~ 现在本子准备正式售卖并开始可以补尾款了。(尾款链接在文...