javapathshortest

Why am I getting an ArrayIndexOutOfBoundsException in this particular code?


I have something similar to this in Dijkstras algorithm but I’m getting no error in that. I’ve tried substituting different values for the integer max and other various things but nothing works. I’ve also searched the this site and others but found nothing that could help. Also if it makes a difference my graph class is in a class to itself. Any help would be appreciated. I updated my question... question was answered. But I did reformat just in case anyone else wanted to take a look.

          public static void main(String[] args)
              {

                  final static int V=7;
                  static final int E=13;

                 Graph graph=new Graph(V,E);

                graph.edge[0].src = 1; 
                graph.edge[0].dest = 2; 
                graph.edge[0].weight = 5; 

                graph.edge[1].src = 1; 
                graph.edge[1].dest = 3; 
                graph.edge[1].weight = 8; 

                graph.edge[2].src = 1; 
                graph.edge[2].dest = 5; 
                graph.edge[2].weight = 7; 

                graph.edge[3].src = 1; 
                graph.edge[3].dest = 6; 
                graph.edge[3].weight = 10; 

                graph.edge[4].src = 2; 
                graph.edge[4].dest = 3; 
                graph.edge[4].weight = -2; 

                graph.edge[5].src = 2; 
                graph.edge[5].dest = 5; 
                graph.edge[5].weight = -2; 

                graph.edge[6].src = 3; 
                graph.edge[6].dest = 4; 
                graph.edge[6].weight = 6; 

                graph.edge[7].src = 5; 
                graph.edge[7].dest = 4; 
                graph.edge[7].weight = 4; 

                graph.edge[8].src = 5; 
                graph.edge[8].dest = 6; 
                graph.edge[8].weight = 2;

                graph.edge[9].src = 5; 
                graph.edge[9].dest = 7; 
                graph.edge[9].weight = 7;

                graph.edge[10].src = 6; 
                graph.edge[10].dest = 7; 
                graph.edge[10].weight= -1; 

                graph.edge[11].src = 7; 
                graph.edge[11].dest = 3; 
                graph.edge[11].weight = 4;

                graph.edge[12].src = 7; 
                graph.edge[12].dest = 4; 
                graph.edge[12].weight = 5;




         Graph.BellmanFord(graph,0);

      }



          public class Graph 
       {


          public class Edge { 
          int src, dest, weight; 
           Edge() { 
          src = dest = weight = 0; 
        } 
       }; 

       int V, E; 
       Edge edge[]; 

       Graph(int v, int e) 
        { 
        V = v; 
        E = e; 
        edge = new Edge[e]; 
       for (int i=0; i<e; ++i) 
           edge[i] = new Edge(); 
      }



      static void bellmanford(Graph graph , int src )
       {

         int V = graph.V, E = graph.E; 
         int dist[]=new int[V];

          for (int i=0; i<V; ++i) 
          dist[i] = Integer.MAX_VALUE; 
          dist[src] = 0; 



        for (int i=1; i<V; ++i) 
       { 
       for (int j=0; j<E; ++j) 
       { 
         int u = graph.edge[j].src; 
         int v = graph.edge[j].dest; 
         int weight = graph.edge[j].weight; 
         if (dist[u]!=Integer.MAX_VALUE && // I’m getting the error        
         here.
               dist[u]+weight<dist[v]) 
                dist[v]=dist[u]+weight; 
        } 
        } 

        for (int j=0; j<E; ++j) 
        { 
       int u = graph.edge[j].src; 
       int v = graph.edge[j].dest; 
       int weight = graph.edge[j].weight; 
       if (dist[u]!= Integer.MAX_VALUE && 
       dist[u]+weight < dist[v]) 
       System.out.println("Graph contains negative weight cycle"); 


        }

         printdistb(dist,V);
       }


     static void printdistb(int dist[], int V) 
     { 
    System.out.println("Vertex   Distance from Source"); 
    for (int i = 0; i< V; ++i) 
    System.out.println(i+"            "+dist[i]); 
     } 

Solution

  • You're declaring the array dist[] with the length of V. And then you're using graph.edge[j].src as an index for the dist[] array. This is why you are getting an ArrayIndexOutOfBoundsException. In short, this means that the src value is bigger than V.

    The fix

    Increase the length of dist[] by 1.

    Inside static void bellmanford(){...} change from

    int dist[] = new int[V];

    to

    int dist[] = new int[V+1];

    This did solve the exception issue for me. But there seem to be more logical issues with the algorithm (The actual problem may be hidden deeper somewhere else.). But at least the program is executing now so you can debug it. Good luck.