import enum
import collections
class BuildGraphic:
def __init__(self):
self.G = dict()
self.G['r'] = ['s', 'v']
self.G['s'] = ['r', 'w']
self.G['v'] = ['r']
self.G['w'] = ['s', 't', 'x']
self.G['x'] = ['u', 't', 'y', 'w']
self.G['t'] = ['u', 'w', 'x']
self.G['u'] = ['t', 'x', 'y']
self.G['y'] = ['x', 'u']
def get(self):
return self.G
class BFS:
Color = enum.Enum('Color', 'WHITE GRAY BLACK')
Nan = 1 << 32
class VInfo:
def __init__(self):
self.color = BFS.Color.WHITE
self.parent = None
self.distance = BFS.Nan
def __repr__(self):
return '(%s, %s, %d)' % (self.color, self.parent, self.distance)
def __init__(self, G):
self.G = G
self.VerToVInfo = { key : BFS.VInfo() for key in self.G }
def __call__(self, *args, **kwargs):
Q = collections.deque()
for s in args:
self.VerToVInfo[s].color = BFS.Color.GRAY
self.VerToVInfo[s].distance = 0
Q.append(s)
while Q:
u = Q.popleft()
for v in self.G[u]:
if self.VerToVInfo[v].color == BFS.Color.WHITE:
self.VerToVInfo[v].color = BFS.Color.GRAY
self.VerToVInfo[v].parent = u
self.VerToVInfo[v].distance = self.VerToVInfo[u].distance + 1
Q.append(v)
self.VerToVInfo[u].color = BFS.Color.BLACK
return self
def get_vinfo(self):
return self.VerToVInfo
if __name__ == '__main__':
G = BuildGraphic()
print(BFS(G.get())('s').get_vinfo())
BFS
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- hiho 1251题目链接(C题) 题目大意 给出两个均由1..6组成的长度相等的字符串,每次你可以进行两种操作。...