圖拓撲排序演算法

來源:互聯網
上載者:User

標籤:ret   for   col   result   res   while   append   not   IV   

 1 # 有向非循環圖 2 def sortedTopology(graph): 3     inDict = {}                     # 儲存節點:節點入度 4     zeroInQueue = Queue()           # 儲存入度為零的節點 5     for node in graph.nodes:        # 遍曆圖所有的節點 6         inDict[node] = node.come    # 將節點與節點入度存入字典中 7         if node.come == 0:          # 若存在節點入度為零則將節點放入隊列中 8             zeroInQueue.put(node) 9     result = []10     while not zeroInQueue.empty():11         cur = zeroInQueue.get()12         result.append(cur)13         for next in cur.nexts:                  # 若當前節點鄰接節點存在14             inDict[next] = inDict[next] - 1     # 在字典中將鄰接節點入度減一再放回15             if inDict[next] == 0:16                 zeroInQueue.put(next)17     return result

 

圖拓撲排序演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.