簡體   English   中英

如何轉換DFS

[英]how to transform dfs

我有深度優先搜索算法,其偽代碼如下:

   DFS(Vertex v)
    mark v visited
    make an empty Stack S
    push all vertices adjacent to v onto S
    while S is not empty do
        Vertex w is pop off S
        for all Vertex u adjacent to w do
            if u is not visited then
                mark u visited
                push u onto S

現在,我希望將上述dfs算法轉換為廣度優先搜索。 我正在用C ++實現程序。 我不知道該怎么做。

編輯:我知道bfs的偽代碼。 我正在尋找的是如何將dfs的上述偽代碼轉換為bfs。

BFS(Vertex v)
    mark v visited
    make an empty Queue Q
    Enqueue all vertices adjacent to v onto Q
    while Q is not empty do
        Vertex w is dequeued from Q
        for all Vertex u adjacent to w do
            if u is not visited then
                mark u visited
                enqueue u into Q

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM