线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:索引 5 超出长度 5 的范围[BFS] [重复]

问题描述 投票:0回答:1
import java.util.*;
class Traversals
{
    public static  class Queue
    {
        int front=-1;
        int size;
        int rear=-1;
        int queue[];
       public Queue(int size)
       {
        this.size=size;
        this.queue=new int[size];
       }

       public boolean isfull()
       {
        return front==0 && rear==size-1;
       }

       public boolean isEmpty()
       {
        return front==-1;
       }

       public void enqueue(int item)
       {
        if(isfull())
        {
            return;
        }
        if(front==-1)
        {
            front++;
        }
        rear++;
        queue[rear]=item;
       }
       public int deque()
       {
        int item;
        if(isEmpty())
        {
            return -1;
        }
        else
        item=queue[front];
        if(front>=rear)
        {
            front=rear=-1;
        }
        else
        {
            front++;
        }
        return item;
        }
    }
    
   
    
    
    public static void bfs(int graph[][],int source,int n)
    {
        Queue queue=new Queue(n);
        boolean visited[]= new boolean[n];
        visited[source]=false;
        queue.enqueue(source);
        System.out.print(source);
        while(!queue.isEmpty())
        {
            int u=queue.deque();
            visited[u]=true;
            for(int v=0;v<n;v++)
            {
                if(graph[u][v]!=0 && visited[v]==false)
                {
                queue.enqueue(v);
                visited[v]=true;
                System.out.print(" "+v);
                }
            }
            
        }
    }


   
    public static void printgraph(int graph[][],int n)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(graph[i][j]!=0)
                {
                    System.out.println("Source : "+ i +", Destination : "+j);
                }
            }
        }
    }
    public static void main(String args[])
    {
        int n,ed;
        System.out.print("Enter the number of nodes: ");
        Scanner sc= new Scanner(System.in);
        n=sc.nextInt();
        System.out.print("Enter the number of edges: ");
        ed=sc.nextInt();
        int graph[][]=new int[n][n];
        int src,dest,x;
        for(int i=0;i<ed;i++)
        {
            System.out.print("Enter the source node for edge " + i + " : ");
            src=sc.nextInt();
            System.out.print("Enter the destination node for edge " + i + " : ");
            dest=sc.nextInt();
            graph[src][dest]=1;
        }
        printgraph(graph,n);
        System.out.println("Enter the node for starting bfs operation: ");
        x=sc.nextInt();
        bfs(graph,x,n);
    }
}

我正在尝试实现 bfs 算法,但遇到了这个错误。谁能帮我解决这个问题。此外, printgraph 函数也倾向于仅打印与 n-1 个顶点相关的边。谁能帮我解释一下这个概念吗

java breadth-first-search
1个回答
0
投票

您没有正确初始化

visited
数组,您必须在更改其值之前初始化所有节点:

boolean visited[] = new boolean[n];

for (int i = 0; i < n; i++) {
    visited[i] = false; // Initialize nodes
}

visited[source] = true;
© www.soinside.com 2019 - 2024. All rights reserved.