码迷,mamicode.com
首页 > 编程语言 > 详细

python - dfs

时间:2020-05-10 12:47:05      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:init   not   elf   visit   object   highlight   sel   add   order   

  解决方案:

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

标签:init   not   elf   visit   object   highlight   sel   add   order   

原文地址:https://www.cnblogs.com/darkchii/p/12862589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!