python 短进程优先算法_黄哥Python:图深度优先算法(dfs)
深度優先搜索算法(英語:Depth-First-Search,DFS)是一種用于遍歷或搜索樹或圖的算法。沿著樹的深度遍歷樹的節點,盡可能深的搜索樹的分支。當節點v的所在邊都己被探尋過,搜索將回溯到發現節點v的那條邊的起始節點。這一過程一直進行到已發現從源節點可達的所有節點為止。如果還存在未被發現的節點,則選擇其中一個作為源節點并重復以上過程,整個進程反復進行直到所有節點都被訪問為止。屬于盲目搜索。
深度優先搜索是圖論中的經典算法,利用深度優先搜索算法可以產生目標圖的相應拓撲排序表,利用拓撲排序表可以方便的解決很多相關的圖論問題,如最大路徑問題等等。因發明“深度優先搜索算法”,約翰·霍普克洛夫特與羅伯特·塔揚在1986年共同獲得計算機領域的最高獎:圖靈獎。
偽代碼(Pseudocode):
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
A recursive implementation of DFS:
procedure DFS(G, v) is label v as discovered for all directed edges from v to w that are in G.adjacentEdges(v) do if vertex w is not labeled as discovered then recursively call DFS(G, w)The order in which the vertices are discovered by this algorithm is called the lexicographic order.
A non-recursive implementation of DFS with worst-case space complexity 。
procedure DFS-iterative(G, v) is let S be a stack S.push(v) while S is not empty do v = S.pop() if v is not labeled as discovered then label v as discovered for all edges from v to w in G.adjacentEdges(v) do S.push(w)黃哥所寫的代碼,迭代實現。
遞歸實現。
請看西瓜視頻播放地址。
總結
以上是生活随笔為你收集整理的python 短进程优先算法_黄哥Python:图深度优先算法(dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: java mysql 占位符_在Java
- 下一篇: stm32 iic接口 进入busy_S
