python - dfs
Posted darkchii
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python - dfs相关的知识,希望对你有一定的参考价值。
解决方案:
class Gra(object): def __init__(self): self.order = [] self.neighboor = {} self.visit = {} def _dfs_visit_(self, u): if u in self.neighboor: for v in self.neighboor[u]: print(v) if v not in self.visit: self.visit[v] = True self._dfs_visit_(v) self.visit[u] = True def add_node(self, node): k, v = node self.neighboor[k] = v def dfs(self): for u in self.neighboor.keys(): if u not in self.visit: print(u) self._dfs_visit_(u) g = Gra() g.add_node((‘A‘, [‘B‘, ‘C‘])) g.add_node((‘C‘, [‘F‘])) g.add_node((‘B‘, [‘D‘, ‘E‘])) g.add_node((‘F‘, [‘G‘, ‘H‘])) g.dfs()
以上是关于python - dfs的主要内容,如果未能解决你的问题,请参考以下文章