• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            O(1) 的小樂

            Job Hunting

            公告

            記錄我的生活和工作。。。
            <2011年11月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統計

            • 隨筆 - 182
            • 文章 - 1
            • 評論 - 41
            • 引用 - 0

            留言簿(10)

            隨筆分類(70)

            隨筆檔案(182)

            文章檔案(1)

            如影隨形

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            有向圖強連通分量 Kosaraju算法

               It makes use of the fact that the transpose graph (the same graph with the direction of every edge reversed) has exactly the same strongly connected components as the original graph.

               它利用了有向圖的這樣一個性質,一個圖和他的transpose graph(邊全部反向)具有相同的強連通分量!

            算法偽代碼

            Kosaraju's algorithm is simple and works as follows:

            • Let G be a directed graph and S be an empty stack.
            • While S does not contain all vertices:
              • Choose an arbitrary vertex v not in S. Perform a depth-first search starting at v. Each time that depth-first search finishes expanding a vertex u, push u onto S.
            • Reverse the directions of all arcs to obtain the transpose graph.
            • While S is nonempty:
              • Pop the top vertex v from S. Perform a depth-first search starting at v. The set of visited vertices will give the strongly connected component containing v; record this and remove all these vertices from the graph G and the stack S. Equivalently,breadth-first search (BFS) can be used instead of depth-first search.

             

             

            需要注意的是這里的第一遍BFS搜索的時候的入隊序列,Each time that depth-first search finishes expanding a vertex u, push u onto S.只有當擴展結束了此節點之后,此節點才會被push onto S.

            算法思路:

            1, 后序遍歷原圖,對每個訪問到的節點標記時間戳。

            2, 按照時間戳的降序遍歷反向圖,得到的每個連通塊就是一個強連通分量。

            證明是很簡單的:

            假設以上算法從u訪問到了v,那么說明反向圖有一條從u到v的邊,也就說明了原圖中有一條從v到u的邊,又因為u的標號是大于v的,那么,u一定在v之前訪問到(否則v的標號將大于u),并且是從u訪問到v了的(v到u也有一條路徑,否則就會從v訪問到u)。

             

            QQ截圖未命名

            如果應用我們第一個Tarjan算法的例子的話,第一遍DFS 得到的次序是 6 4 2 5 3 1

             

            代碼

            #include "cstdlib"
            #include "cctype"
            #include "cstring"
            #include "cstdio"
            #include "cmath"
            #include "algorithm"
            #include "vector"
            #include "string"
            #include "iostream"
            #include "sstream"
            #include "set"
            #include "queue"
            #include "stack"
            #include "fstream"
            #include "strstream"
            using namespace std;
            #define M 2000
            bool vis[M];                 //遍歷數組
            int post[M];                 //時間戳對應的點
            int timestamp;               //時間戳
            int ComponetNumber=0;        //有向圖強連通分量個數
            vector <int> Edge[M];        //鄰接表表示
            vector <int> Opp[M];         //原圖的反圖
            vector <int> Component[M];   //獲得強連通分量結果

            void dfs(int u) {             //第一個dfs確定時間戳
                vis[u] = true;
                for(int i=0;i<Edge[u].size();i++) {
                    if(vis[ Edge[u][i]])    continue;
                    //cout<<Edge[u][i]<<endl;
                    dfs(Edge[u][i]);
                }
                //cout<<"timestamp    "<<timestamp<<"       "<<u<<endl;   
                post[timestamp++] = u;
            }

            void dfs2(int u) {      //第二個反邊dfs確定連通塊
                vis[u] = true;
                Component[ComponetNumber].push_back(u);
                for(int i=0;i<Opp[u].size();i++)
                {
                    int v = Opp[u][i];
                    if(vis[v])  continue;
                    dfs2(v);
                }
            }

            void Kosaraju(int n) {
                memset(vis,0,sizeof(vis));
                timestamp = 0;
                for(int i=0;i<n;i++) {
                    if(vis[i])    continue;
                    dfs(i);
                }
                memset(vis,0,sizeof(vis));
                ComponetNumber++;
                for(int i=n-1;i>=0;i--) {//按時間戳從大到小搜
                    if(vis[post[i]])    continue;
                    Component[ComponetNumber].clear();
                    dfs2(post[i]);
                    ComponetNumber++;
                }
                ComponetNumber--;      //最后我們把塊加了1。。所以要減掉
            }
            int main()
            {
                Edge[0].push_back(1);Edge[0].push_back(2);
                Edge[1].push_back(3);
                Edge[2].push_back(3);Edge[2].push_back(4);
                Edge[3].push_back(0);Edge[3].push_back(5);
                Edge[4].push_back(5);

                Opp[0].push_back(3);
                Opp[1].push_back(0);
                Opp[2].push_back(0);
                Opp[3].push_back(1);Opp[3].push_back(2);
                Opp[4].push_back(2);
                Opp[5].push_back(3);Opp[6].push_back(4);
                int  N=6;
                Kosaraju(N);
                cout<<"ComponetNumber is "<<ComponetNumber<<endl;
                for(int i=0;i<N;i++)
                {
                    for(int j=0;j<Component[i].size();j++)
                        cout<<Component[i][j];
                    cout<<endl;
                }
                return 0;
            }

             

             

                此算法的時間復雜度當然也是 O(M+N)(M條邊,N個點)與Tarjan算法相似。。但是在系數上不如Tarjan算法!在實際的測試中,Tarjan算法的運行效率也比Kosaraju算法高30%左右。 

                當然Kosaraju算法額外花費的時間,也不是白費的,它獲得了圖的一個拓撲性質哦!!

                如果我們把求出來的每個強連通分量收縮成一個點,并且用求出每個強連通分量的順序來標記收縮后的節點,那么這個順序其 實就是強連通分量收縮成點后形成的有向無環圖的拓撲序列。為什么呢?首先,應該明確搜索后的圖一定是有向無環圖呢?廢話,如果還有環,那么環上的頂點對應 的所有原來圖上的頂點構成一個強連通分量,而不是構成環上那么多點對應的獨自的強連通分量了。然后就是為什么是拓撲序列,我們在改進分析的時候,不是先選 的樹不會連通到其他樹上(對于反圖GT來說),也就是后選的樹沒有連通到先選的樹,也即先出現的強連通分量收縮的點只能指向后出現的強連通分量收縮的點。那么拓撲序列不是理所當然的嗎?這就是Kosaraju算法的一個隱藏性質。

             

            Reference :

            http://www.notonlysuccess.com/?p=181

            推薦一下啊!終于算是搞的差不多了。。下面就是做一些練習,然后鞏固提高一下!接下來剩下的最后一個算法了:Gabow 算法

            posted on 2010-09-26 22:49 Sosi 閱讀(1860) 評論(1)  編輯 收藏 引用

            評論

            # re: 有向圖強連通分量 Kosaraju算法 2013-04-29 19:13 ygqwan

            樓主的第一次post數組是不是存錯了呢
              回復  更多評論    
            統計系統
            久久夜色精品国产欧美乱| 久久99热国产这有精品| 久久精品国产亚洲av麻豆图片| 合区精品久久久中文字幕一区| 人妻无码中文久久久久专区| 国产精品久久久福利| 久久中文字幕视频、最近更新| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲伊人久久成综合人影院| 久久综合九色综合网站| 久久精品国产第一区二区| 久久精品国产亚洲AV电影| 久久久久久国产a免费观看不卡| 久久精品国产亚洲AV电影| 久久综合亚洲色HEZYO社区| 国产毛片久久久久久国产毛片| 久久久久久久久久久久中文字幕 | 久久青青草原国产精品免费| 一级做a爰片久久毛片毛片| 99久久精品免费观看国产| 久久这里只有精品18| 久久久久久国产精品美女| 久久久久久青草大香综合精品| 国产精品久久久久久| 久久夜色精品国产噜噜亚洲AV| 久久精品人妻中文系列| 怡红院日本一道日本久久 | 狠狠精品久久久无码中文字幕| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 香蕉久久av一区二区三区| 亚洲精品成人久久久| 亚洲国产成人精品女人久久久| 久久亚洲2019中文字幕| 欧美粉嫩小泬久久久久久久| 久久国产成人精品国产成人亚洲| 777久久精品一区二区三区无码| 久久99精品国产99久久| 麻豆精品久久精品色综合| 成人a毛片久久免费播放| 国产高清美女一级a毛片久久w| 精品水蜜桃久久久久久久|