Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-递归Tarjan';超出了s算法的时间限制_Java_Recursion_Graph_Tarjans Algorithm - Fatal编程技术网

Java-递归Tarjan';超出了s算法的时间限制

Java-递归Tarjan';超出了s算法的时间限制,java,recursion,graph,tarjans-algorithm,Java,Recursion,Graph,Tarjans Algorithm,我用Tarjan的算法在无向图中找到临界连接。但是,当输入有100000个顶点时,它有时会抛出一个TLE。我到处找,找不出原因 我试图实现Tarjan算法的迭代版本,但没能实现。 代码是否有进一步的改进 测试用例链接: import java.util.*; 七星三级{ //访问顶点的顺序 私人内部文件[]ord; //此顶点可以达到的顶点的最低索引 私人机构【】低; //跟踪当前订单; 私人整数计数; //结果 私有列表结果; //图表 私有映射图; 参观私人住宅;; 公共静态void mai

我用Tarjan的算法在无向图中找到临界连接。但是,当输入有100000个顶点时,它有时会抛出一个TLE。我到处找,找不出原因

我试图实现Tarjan算法的迭代版本,但没能实现。 代码是否有进一步的改进

测试用例链接:

import java.util.*;
七星三级{
//访问顶点的顺序
私人内部文件[]ord;
//此顶点可以达到的顶点的最低索引
私人机构【】低;
//跟踪当前订单;
私人整数计数;
//结果
私有列表结果;
//图表
私有映射图;
参观私人住宅;;
公共静态void main(字符串[]args){
SevenSixThree s=新的SevenSixThree();
列表=新的ArrayList();
列表l1=新的ArrayList();
l1.添加(0);
l1.加入(1);
添加(新阵列列表(l1));
列表l2=新的ArrayList();
l2.添加(0);
l2.添加(2);
添加(新ArrayList(l2));
列表l3=新的ArrayList();
l3.加入第(1)款;
l3.加入(2);
添加(新ArrayList(l3));
列表l4=新的ArrayList();
l4.加入第(1)款;
l4.加入(3);
添加(新阵列列表(l4));
List res=s.临界连接(4,List);
系统输出打印(res.toArray().toString());
}
公共列表关键连接(int n,列表连接){
//求一个图的桥。
//首先,用map构建图形
HashMap graph=新的HashMap();
对于(int i=0;iord[v]){
列表桥=新的ArrayList();
桥。添加(v);
桥。添加(w);
结果:添加(桥接);
}
}否则{
如果(w!=父级){
低[v]=数学最小值(低[v],低[w]);
}
}
}
}

对于性能改进,代码是良好的,并且被法官接受?对于性能改进,代码是良好的,并且被法官接受?
import java.util.*;

class SevenSixThree {


    // the order of visiting vertices
    private int[] ord;

    // the lowest index of the vertex that this vertex can reach
    private int[] low;

    // keep track of the current order;
    private int count;

    // result
    private List<List<Integer>> result;

    // graph
    private Map<Integer, List<Integer>> graph;

    private boolean[] visited;

    public static void main(String[] args) {
        SevenSixThree s = new SevenSixThree();
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> l1 = new ArrayList<>();
        l1.add(0);
        l1.add(1);
        list.add(new ArrayList<>(l1));

        List<Integer> l2 = new ArrayList<>();
        l2.add(0);
        l2.add(2);
        list.add(new ArrayList<>(l2));

        List<Integer> l3 = new ArrayList<>();
        l3.add(1);
        l3.add(2);
        list.add(new ArrayList<>(l3));

        List<Integer> l4 = new ArrayList<>();
        l4.add(1);
        l4.add(3);
        list.add(new ArrayList<>(l4));

        List<List<Integer>> res = s.criticalConnections(4, list);
        System.out.print(res.toArray().toString());
    }


    public  List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
        // find bridge of a graph.

        // first, build the graph with map
        HashMap<Integer, List<Integer>> graph = new HashMap<>();
        for (int i = 0; i < n; i++) {
            graph.put(i, new ArrayList<>());
        }


        for (List<Integer> connection : connections) {
            int v = connection.get(0);
            int w = connection.get(1);

            graph.get(v).add(w);
            graph.get(w).add(v);
        }

        ord = new int[n];
        low = new int[n];
        visited = new boolean[n];
        Arrays.fill(visited, false);
        result = new ArrayList<>();

        dfs(0, -1, graph);


        return result;
    }

    private void dfs(int v, int parent, HashMap<Integer, List<Integer>> graph) {
         // visit this vertex
         visited[v] = true;
         ord[v] = count++;
         low[v] = ord[v];

         List<Integer> adjs = graph.get(v);
         for (int w : adjs) {
             if (!visited[w]) {
                 dfs(w, v, graph);
                 low[v] = Math.min(low[w], low[v]);
                 if (low[w] > ord[v]) {
                     List<Integer> bridge = new ArrayList<>();
                     bridge.add(v);
                     bridge.add(w);
                     result.add(bridge);
                 }
             } else {
                 if (w != parent) {
                     low[v] = Math.min(low[v], low[w]);
                 }
             }
         }
     }