For faster navigation, this Iframe is preloading the Wikiwand page for 科萨拉朱算法.

科萨拉朱算法

科萨拉朱算法(英語:Kosaraju's algorithm),也被称为科萨拉朱—夏尔算法,是一个在线性时间内寻找一个有向图中的强连通分量的算法。阿尔佛雷德·艾侯约翰·霍普克洛夫特杰弗瑞·乌尔曼相信该算法来自S·拉奥·科萨拉朱英语S. Rao Kosaraju于1978年撰写的一篇未发表论文之中[1]米卡·夏尔英语Micha Sharir也独立发现了该算法并于1981年将其发表[2]。该算法巧妙地利用了一个定理:「一个图的反向图和原图具有一样的强连通分量」。

简介

[编辑]

该算法主要用于枚举图中每一个强连通分量内的所有顶点。该算法可由以下四部分组成[3]

  1. 对有向图取逆,得到的反向图
  2. 利用深度优先搜索求出的逆后排序
  3. 按照上述逆后排序的序列进行深度优先搜索
  4. 同一个深度优先搜索递归子程序中访问的所有顶点都在同一个强连通分量内

Java代码实现

[编辑]
public class KosarajuAlgorithm {
    private boolean[] marked;
    private int[] id;
    private int count=-1;
    private Stack<Integer> reversePostOrder;
    public KosarajuAlgorithm(Digraph G){
        //G.V()返回有向图G的边数
        marked=new boolean[G.V()];
        id=new int[G.V()];
        //G.reverse()返回的为G的反向图
        Digraph G_reverse=G.reverse();
        //本遍循环是将G的反向图的逆后序排列存储在reversePostOrder中
        for(int i=0;i<G_reverse.V();i++){
            if(!marked[i]){
                dfs(G_reverse,i);
            }
        }
        count=0;
        //按照G的反向图的逆后排序进行深度优先搜索
        for(int i:reversePostOrder){
            if(!marked[i]){
                dfs(G,i);
                count++;
            }
        }
    }
    //深度优先搜索
    public void dfs(Digraph G,int v){
        marked[v]=true;
        id[v]=count;
        for(int i:G.adj(v)){
            if(!marked[i]){
                dfs(G,i);
            }
        }
        reversePostOrder.push(v);
    }
}

复杂度

[编辑]

当图是使用邻接表形式组建的,科萨拉朱算法需要对整张图进行了两次的完整的访问,每次访问与顶点数和边数之和成正比,所以可以在線性时间内访问完成。该算法在实际操作中要比Tarjan算法基于路径的强连通分量算法英语Path-based strong component algorithm要慢,这两种算法都只需要对图进行一次完整的访问。

当图是使用邻接矩阵形式组建的,算法的时间复杂度为

参考

[编辑]
  1. ^ Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman. Data Structures and Algorithms. Addison-Wesley. 1983 [2016-02-03]. ISBN 978-0201000238. ,p222--p230
  2. ^ Micha, Sharir. A strong-connectivity algorithm and its applications in data flow analysis. Computers & Mathematics with Applications. 1981, (7): 67–72 [2016-02-03]. (原始内容存档于2019-04-13). 
  3. ^ Robert Sedgewick, Kevin Wayne. 算法. 北京: 人民邮电出版社. 2012年10月 [2016-02-03]. ISBN 978-7-115-29380-0. ,p379--p380

文献及链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
科萨拉朱算法
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?