1

I am making a program to find an mst in a given graph (custom data type). I tested it with a tuple that seemed to work, but when I gave it a different tuple, it says:

Graph:
(1, 2, 1) (1, 3, 0) (2, 4, 0) Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
    at Graph$xGraph.edges(Graph.java:18)
    at Graph.main(Graph.java:102)

The graph I'm giving it has 4 different vertices so I don't understand the bounds issue.

Here is where I'm setting the bounds of the graph in main:

public static void main(String[] args) {
        int v = 4;
        xGraph graph = new xGraph(v);
        System.out.println("Graph:");
        graph.edges(1,2,1);
        graph.edges(1,3,0);
        graph.edges(2,4,0);
        graph.edges(3,4,10);
        graph.primMST();
    }

And here is the rest of the code:

public class Graph{
    

    static class xGraph{
        public int v;
        public int m[][];

        public xGraph(int vert) {
            this.v = vert;
            m = new int[vert][vert];
        }

        public void edges(int start, int end, int weight) {   
            System.out.print("(" + start + ", " + end + ", " + weight + ") ");
            m[start][end]=weight;            
            m[end][start] = weight;
        }

        
        int min(boolean [] mst, int [] weights){
            int minweights = Integer.MAX_VALUE;
            int vert = -1;
            
            for (int i = 0; i <v ; i++) {
                if(mst[i]==false && minweights>weights[i]){
                    minweights = weights[i];
                    vert = i;
                }
            }
            
            return vert;
            
        }

        public class minSpanTree{
            
            int parent;
            
            int weight;
            
        }

        public void primMST(){
            boolean[] mst = new boolean[v];
            minSpanTree[] minSpanTree = new minSpanTree[v];
            int [] weights = new int[v];

            for (int i = 0; i <v ; i++) {
                weights[i] = Integer.MAX_VALUE;
                minSpanTree[i] = new minSpanTree();
            }

            
            weights[0] = 0;
            minSpanTree[0] = new minSpanTree();
            minSpanTree[0].parent = -1;

            for (int i = 0; i <v ; i++) {

               
                int vert = min(mst, weights);

                mst[vert] = true;

                for (int j = 0; j <v ; j++) {
                    
                    if(m[vert][j]>0){
                        if(mst[j]==false && m[vert][j]<weights[j]){                            
                            weights[j] = m[vert][j];
                            minSpanTree[j].parent = vert;
                            minSpanTree[j].weight = weights[j];
                        }
                    }
                }
            }                             
            printMST(minSpanTree);
        }

        public void printMST(minSpanTree[] minSpanTree){
            int total_min_weight = 0;
            System.out.println();
            System.out.println("Minimum Spanning Tree: ");
            for (int i = 1; i <v ; i++) {
                System.out.println("Edge: " + i + " - " + minSpanTree[i].parent +
                        " weights: " + minSpanTree[i].weight);
                total_min_weight += minSpanTree[i].weight;
            }
            System.out.println("Total minimum weights: " + total_min_weight);
        }
    
    }
    
    public static void main(String[] args) {
        int v = 4;
        xGraph graph = new xGraph(v);
        System.out.println("Graph:");
        graph.edges(1,2,1);
        graph.edges(1,3,0);
        graph.edges(2,4,0);
        graph.edges(3,4,10);
        graph.primMST();
    }
    
}

edit For more information. The program runs with a six vertices graph.

public static void main(String[] args) {
        int v = 6;
        xGraph graph = new xGraph(v);
        System.out.println("Graph:");
        graph.edges(0, 1, 2);
        graph.edges(0, 2, 4);
        graph.edges(1, 2, 2);
        graph.edges(1, 3, 1);
        graph.edges(2, 3, 6);
        graph.edges(3, 4, 7);
        graph.edges(4, 5, 3);
        graph.primMST();
    }

I'm just confused because how can it work with variables that seem equal to one another in purpose?

  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – maloomeister Apr 09 '21 at 06:31
  • 2
    You create a 2D array with size `4` for both dimensions. So your array indexes go from `0-3`, as array indexes start at `0`. Im method `edges` you however try to access the array with index `4`, which causes the exception. – maloomeister Apr 09 '21 at 06:32
  • I changed the the size v to 3 but it didn't seem to fix it. Is it because it's 2d? – linda baldwell Apr 09 '21 at 14:34
  • @maloomeister I think I'm confused because I'm using one single variable to set the bounds for a 2D array. I can't seem to figure out what it should be. – linda baldwell Apr 09 '21 at 16:47

1 Answers1

0

It is because the first method started the vertices at zero while when I changed it to a 4 node graph, I started from 1.