PYTHON實現DFS演算法

來源:互聯網
上載者:User

標籤:index   ext   white   etc   style   init   visit   getc   code   

 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 = nextVertice10   def setColor(self,color):11     self.color = color12   def getColor(self):13     return self.color14   def incVt(self):15     self.vt += 116   def getVt(self):17     return self.vt18 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.