python - dfs

2021-01-25 07:12

阅读:477

标签: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


评论


亲,登录后才可以留言!