简体   繁体   中英

Indexing error in BFS algorithm of undirected graph

class Solution {
 // Function to return Breadth First Traversal of given graph.
 public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj)     
 {
    ArrayList<Integer> result = new ArrayList<>();
    
    Queue<Integer> q = new LinkedList<>();
    q.add(0);
    
    boolean[] visited = new boolean[V];
    visited[0] = true;
    
    while(!q.isEmpty()) {
        int v = q.poll();
        result.add(v);
        
        ArrayList<Integer> adjList = adj.get(v);
        for(int i : adjList) {
            if(!visited[i]) {
                visited[i] = true;
                q.add(i);    
            }
        }
    }
    
    return result;
 }      
}

Error: [错误图片 1

I am attempting bfs algorithm in undirected graph and it is showing error of segmentation fault if anyone have any knowledge regarding the concept please reply.

Segmentation Faults happen in programs (such as the JVM) due to memory errors. Either the JVM has a bug in it that makes it try to use the wrong section of memory on the computer when its cranked up to use that much buffer space, or it tries to allocate 256M of memory and in the process, it uses more space than the computer gave it. Make sure you update your JVM. Please let me know if you still face the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM