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()
相关推荐
鱼天翱 2019-06-28
heray0 2019-06-28
csmnjk 2019-06-27
WindChaser 2019-06-25
lawrencesgj 2018-07-23
lisa0 2014-12-06
GMCWXH 2014-11-09
IvanXing 2013-10-17
ououlal 2013-10-21
MrTitan 2012-05-05
liuyubing0 2013-01-17
snowman 2013-01-17
YLIMHHMILY 2012-10-21
wudi 2012-03-11
陈先森 2012-02-12
ynkgyangxw 2012-01-29
PHP100 2019-03-28