在处理非常大的输入时,深度优先搜索(DFS)实现中出现了栈溢出错误。

huangapple go评论141阅读模式
英文:

Getting StackOverflowError in DFS implementation for very large input

问题

以下是翻译好的部分:

在使用Java在HackerEarth上练习DFS问题时,其中一个测试用例在检查时导致NZEC错误,显示StackOverFlow错误。我检查了很多次,除了一个导致错误的测试用例外,所有的测试用例都通过了。这不仅仅是针对特定问题,而是在许多问题(DFS问题)中,总有一个测试用例在使用Java解决时总是导致NZEC错误。

我不知道是因为我的邻接表实现还是其他原因。下面是其中一个问题。

问题https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/feasible-relations/

我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;

public class TestClass {

    static long mod = 1000000007;
    static String[] temp;
    static  int[] node;
    static  int[] vis;
    public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main (String[] args) throws IOException {

        int testCases= Integer.parseInt(br.readLine().split(" ")[0]);

        while (testCases > 0) {
            temp = br.readLine().split(" ");
            int N = Integer.parseInt(temp[0]);
            int K = Integer.parseInt(temp[1]);

            HashMap<Integer, ArrayList<Integer>> forEqual = new HashMap<>();
            ArrayList<int[]> edgeList = new ArrayList<>();

            for (int i = 0; i < K; i++) {
                temp = br.readLine().split(" ");
                int u = Integer.parseInt(temp[0]);
                int v = Integer.parseInt(temp[2]);
                if (temp[1].equals("=")) {
                    forEqual.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
                    forEqual.computeIfAbsent(v, k -> new ArrayList<>()).add(u);
                } else {
                    edgeList.add(new int[]{u, v});
                }
            }

            boolean[] vis = new boolean[1000005];
            node = new int[1000005];
            int c = 0;
            for (int i : forEqual.keySet()) {
                if (!vis[i]) {
                    c++;
                    DFS(i, forEqual, c, vis);
                }
            }
            boolean flag = true;

            for(int[] i : edgeList){
                if(node[i[0]] == node[i[1]]){
                    flag = false;
                    break;
                }
            }
            System.out.println(flag ? "YES" : "NO");
            testCases--;
        }
    }

    private static void DFS(int i, HashMap<Integer, ArrayList<Integer>> forEqual, int c, boolean[] vis) {
        vis[i] = true;
        node[i] = c;
        for (int v : forEqual.get(i)) {
            if (!vis[v]) {
                DFS(v, forEqual, c, vis);
            }
        }
    }

}

错误日志

英文:

While practising DFS problems on HackerEarth using Java, one of the test cases results in NZEC error when checking it out, it displays StackOverFlow error. I checked it many of the times but all of the test cases pass except one which results in an error, and this is not for that specific problem but for many of the problems(DFS problems) one test cases is always there out of 10 or 20 which always result in NZEC when solving with java.

I don't whether it is due to my Adjacency list implementation or something else. Here is the problem one of the problem.

Problem : https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/feasible-relations/

My code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
public class TestClass {
static long mod = 1000000007;
static String[] temp;
static  int[] node;
static  int[] vis;
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {
int testCases= Integer.parseInt(br.readLine().split(&quot; &quot;)[0]);
while (testCases &gt; 0) {
temp = br.readLine().split(&quot; &quot;);
int N = Integer.parseInt(temp[0]);
int K = Integer.parseInt(temp[1]);
HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; forEqual = new HashMap&lt;&gt;();
ArrayList&lt;int[]&gt; edgeList = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; K; i++) {
temp = br.readLine().split(&quot; &quot;);
int u = Integer.parseInt(temp[0]);
int v = Integer.parseInt(temp[2]);
if (temp[1].equals(&quot;=&quot;)) {
forEqual.computeIfAbsent(u, k -&gt; new ArrayList&lt;&gt;()).add(v);
forEqual.computeIfAbsent(v, k -&gt; new ArrayList&lt;&gt;()).add(u);
} else {
edgeList.add(new int[]{u, v});
}
}
boolean[] vis = new boolean[1000005];
node = new int[1000005];
int c = 0;
for (int i : forEqual.keySet()) {
if (!vis[i]) {
c++;
DFS(i, forEqual, c, vis);
}
}
boolean flag = true;
for(int[] i : edgeList){
if(node[i[0]] == node[i[1]]){
flag = false;
break;
}
}
System.out.println(flag ? &quot;YES&quot; : &quot;NO&quot;);
testCases--;
}
}
private static void DFS(int i, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; forEqual, int c, boolean[] vis) {
vis[i] = true;
node[i] = c;
for (int v : forEqual.get(i)) {
if (!vis[v]) {
DFS(v, forEqual, c, vis);
}
}
}
}

Error Log

答案1

得分: 2

没有人回答我的问题,这个问题不是来自实时竞赛,我花了一个星期的时间来找到问题的解决方案。
StackOverflow的问题是由HackerEarth IDE和类似的在线Java IDE上堆栈调用次数限制引起的,仅允许 100,000 次调用,为了避免这种情况,创建一个线程对象并手动传递堆栈大小作为参数,这将增加堆栈大小。

class Solution{
     public static void main (String[] args) throws IOException {
          new Thread(null, new TestClass(), "", 1 << 26).start();
     }
     public void run() {
         // 无论你想做什么
     }
}
英文:

No one answer to my question, this question is not from the live contest, it takes me a week to figure out the solution for my problem.
The problem of StackOverFlow is due to the limitation of the number of stack calls on HackerEarth IDE and similar online IDE for Java which is 100,000 calls are only allowed, to avoid such, create a thread object and pass stack size as a parameter manually, which will increase the stack size.

class Solution{
public static void main (String[] args) throws IOException {
new Thread(null, new TestClass(), &quot;&quot;, 1 &lt;&lt; 26).start();
}
public void run() {
// DO WHATEVER YOU WANT 
}
}

huangapple
  • 本文由 发表于 2020年8月18日 20:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定