简体   繁体   English

无向图BFS算法中的索引错误

[英]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 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.我正在无向图中尝试 bfs 算法,如果有人对这个概念有任何了解,它会显示分段错误错误,请回复。

Segmentation Faults happen in programs (such as the JVM) due to memory errors.由于内存错误,在程序(例如 JVM)中会发生分段错误。 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.要么 JVM 有一个错误,导致它在启动以使用那么多缓冲区空间时尝试使用计算机上错误的内存部分,或者它尝试分配 256M 的内存,并且在此过程中,它使用了更多空间比电脑给的。 Make sure you update your JVM.确保更新 JVM。 Please let me know if you still face the issue.如果您仍然遇到此问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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