Kosaraju算法(发现强连通分图算法)

        看论文的时候,看到Kosaraju算法。Kosaraju是一个强连通分图的发现算法,如有代码中有详细的注释,所以就不赘述了。直接上码(使用webgraph库实现,在我之前的文章中,对webgraph有相关的介绍):

 
 
package cn.edu.dlut.wisdom;
 
 
import it.unimi.dsi.fastutil.ints.*;
import it.unimi.dsi.fastutil.objects.*;
import it.unimi.dsi.webgraph.*;
import java.util.Comparator;
 
 
/**
 * @author You Wang
 * Kosaraju算法,用来发现强连通分量
 * 算法过程如下:
 * G:有向图;S:空栈
 * while S中不包含G中所有顶点
 *     任选一个不在S中的节点v
 *     从v开始进行深度优先搜素,每次将访问到的节点u压入S中
 * 反转G中所有的边
 * while S非空
 *     从S中弹出结点v
 *     从v开始进行深度优先搜索,所有访问到的结点构成强连通分图,记录下来
 *     从G和S中删除这些结点
 */
public class Kosaraju {
    private ImmutableGraph graph;
    private ImmutableGraph igraph;
    private ImmutableGraph workGraph;
    private ObjectAVLTreeSet<IntAVLTreeSet> sccs;
    private IntArrayList stack;
    private boolean[] visited;
    private int numNodes;
 
 
    public Kosaraju(ImmutableGraph graph) {
        this.graph = graph;
        igraph = Transform.transpose(graph);
        numNodes = graph.numNodes();
    }
 
 
    public Kosaraju(ImmutableGraph graph, ImmutableGraph igraph) {
        this.graph = graph;
        this.igraph = igraph;
        numNodes = graph.numNodes();
    }
 
 
    public ObjectAVLTreeSet<IntAVLTreeSet> compute() {
        stack = new IntArrayList();
        visited = new boolean[numNodes];
        workGraph = graph;
        // dfs the graph, adding nodes to the stack
        for(int i = 0; i < numNodes; i++)
            if(!stack.contains(i))
                dfs(i, true);
        workGraph = igraph;
        Comparator cmp = new Comparator() {
 
 
            public int compare(Object o1, Object o2) {
                if(o1 instanceof IntAVLTreeSet && o2 instanceof IntAVLTreeSet) {
                    IntAVLTreeSet s1 = (IntAVLTreeSet)o1;
                    IntAVLTreeSet s2 = (IntAVLTreeSet)o2;
                    if (s1.size() != s2.size())
                        return s1.size() - s2.size();
                    else
                    {
                        int[] a1 = s1.toIntArray();
                        int[] a2 = s2.toIntArray();
                        for (int i = 0; i < a1.length; i++)
                            if (a1[i] != a2[i])
                                return a1[i] - a2[i];
                        return 0;
                    }
                }
                else
                    throw new IllegalArgumentException("The argument must be an IntAVLTreeSet");
            }
        };
        sccs = new ObjectAVLTreeSet<IntAVLTreeSet>(cmp);
        while(!stack.isEmpty()) {
            IntAVLTreeSet component = new IntAVLTreeSet();
            int v = stack.popInt();
            component.add(v);
            dfs(v, false);
            // any components we visited are strongly connected
            // remove them from the starck and add them to the component
            IntIterator it = stack.iterator();
            while(it.hasNext()) {
                int n = it.nextInt();
                if(!visited[n]) {
                    component.add(n);
                    it.remove();
                }
            }
            if(component.size() != 0)
                sccs.add(component);
        }
        return sccs;
    }
 
 
    private void dfs(int node, boolean forward) {
        visited[node] = forward;
        if(workGraph.outdegree(node) == 0) {
            if(forward)
                stack.push(node);
            return;
        }
        for(int n : workGraph.successorArray(node))
            if(visited[n] != forward)
                dfs(n, forward);
        if(forward)
            stack.push(node);
    }
}
 
 

 

        其中的set排序,个人感觉不尽完美,不知道各位有何高见,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.现给你一个函数式y=f(x)和N(N在1到8间取值),对于所有可能x(x在0到2N-1间取值)输出对应的结果y(y在0到2N-1间取值)。 输入只有一行,给你一个函数式,式子中只含有字母,数字,‘+’、‘-’、‘*’、‘/’、‘(’、‘)’,表达式长度不超过 100,式子中的数均不为负且为0到2N-1间的int型整数,保证数据合法,输入的值以及结果都为0到2N-1间的int型整数。 ‘+’表示两正整数相加,结果再模2N ‘-’表示两正整数相减的绝对值,结果再模2N ‘*’表示两正整数相乘,结果再模2N ‘/’表示两正整数相除,结果再模2N ‘(’、‘)’括号里面的运算优先级更高 2.根据1.的结果判断其输入输出构成的有向图的连通性(不需画出有向图,只需判断连通性,)。 输入输出构成的有向图有2N个顶点,如果有b=f(a)表明从顶点a到顶点b有有向路径。 连通(Strongly Connected)是指一个有向图(Directed Graph)中任意两点v1、v2间存在v1到v2的路径(path)及v2到v1的路径。 在有向图中, 如果任何两顶点之间均有有向路径, 则称该有向图是连通图。 现有的判定方法有从某个结点出发后借助对有向图的正向和逆向深度优先搜索来进行的,有通过求其可达矩阵来判定,有将有向图中形成回路的结点进行收缩的方法来判断等。从降低时间复杂度和空间复杂度考虑,找出结点较多时能快速判断出一个有向图是否为连通的方法,并编程实现。 示例: 输入N: 2 输入表达式:y=(x+1)*3 输出 x :0 1 2 3 y :3 2 1 0 输出 该函数的有向图不是连通图 注释:由于N=2,x取值0到3 。 当x=0时,y=(((0+1)%4)*3) %4=3 当x=1时,y=(((1+1)%4)*3) %4=2 当x=2时,y=(((2+1)%4)*3) %4=1 当x=3时,y=(((3+1)%4)*3) %4=0 得到对应的有向图为 输入的是M维函数,输出相应结果 示例: 输入N: 2 输入M:2 输入表达式:w=(x+y+1)*3 v=(x +1)*3+y 输出 (x,y) :(0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) (w,v) : (3,3) (2,0) (1,1) (0,2) (2,2) (1,3) (0,0) (3,1) (1,1) (0,2) (3,3) (2,0) (0,0) (3,1) (2,2) (1,3) 输出 该函数的有向图不是连通

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值