首页 > 代码库 > PYTHON实现DFS算法
PYTHON实现DFS算法
1 class Vertice: 2 def __init__(self,index): 3 self.no = index 4 self.color = 0 # 0:white 1 gray 2 black 5 self.vt = 0 6 def getNextV(self): 7 return self.nextV 8 def setNextV(self,*nextVertice): 9 self.nextV = nextVertice 10 def setColor(self,color): 11 self.color = color 12 def getColor(self): 13 return self.color 14 def incVt(self): 15 self.vt += 1 16 def getVt(self): 17 return self.vt 18 19 v1 = Vertice(1) 20 v2 = Vertice(2) 21 v3 = Vertice(3) 22 v4 = Vertice(4) 23 v5 = Vertice(5) 24 25 v1.setNextV(v2,v5) 26 v2.setNextV(v1,v5,v4,v3) 27 v3.setNextV(v2,v4) 28 v4.setNextV(v2,v3,v5) 29 v5.setNextV(v1,v2,v4) 30 31 v100 = Vertice(100) 32 v100.setNextV(v1,v2,v3,v4,v5) 33 34 def DFS_Visit(v): 35 v.setColor(1) 36 v.incVt() 37 for it in v.getNextV(): 38 if it.getColor() == 0: 39 DFS_Visit(it) 40 v.setColor(2) 41 42 DFS_Visit(v100) 43 44 for it in v100.getNextV(): 45 print it.getVt() 46
PYTHON实现DFS算法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。