簡體   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;
 }      
}

錯誤: [錯誤圖片 1

我正在無向圖中嘗試 bfs 算法,如果有人對這個概念有任何了解,它會顯示分段錯誤錯誤,請回復。

由於內存錯誤,在程序(例如 JVM)中會發生分段錯誤。 要么 JVM 有一個錯誤,導致它在啟動以使用那么多緩沖區空間時嘗試使用計算機上錯誤的內存部分,或者它嘗試分配 256M 的內存,並且在此過程中,它使用了更多空間比電腦給的。 確保更新 JVM。 如果您仍然遇到此問題,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM