线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1

时间:2016-05-06 00:55:11

标签: java

我正在尝试计算保存在文本文件中的加权连接图的MST。但是,我得到一个例外:ArrayIndexOutOfBounds: 1

我知道我遇到了问题而我正在尝试访问无效的数组索引。我似乎无法找到改变的地点和内容。我在这个网站和其他网站上搜索了一些类似的问题,但无济于事。 如果有人能够指出我出错的地方,我们将不胜感激。

class Heap
{
    private int[] h;       // heap array
    private int[] hPos;    // hPos[h[k]] == k
    private int[] dist;    // dist[v] = priority of v

    private int N;         // heap size

    // The heap constructor gets passed from the Graph:
    //    1. maximum heap size
    //    2. reference to the dist[] array
    //    3. reference to the hPos[] array
    public Heap(int maxSize, int[] _dist, int[] _hPos) 
    {
        N = 0;
        h = new int[maxSize + 1];
        dist = _dist;
        hPos = _hPos;
    }

    public boolean isEmpty() 
    {
        return N == 0;
    }

    public void siftUp( int k) 
    {
        int v = h[k];

        h[0] = 0; //puts dummy node at top of the heap
        dist[0] = Integer.MIN_VALUE;


        while (dist[v] < dist[h[k / 2]])
        {
            h[k] = h[k / 2];
            hPos[h[k]] = k;
            k = k / 2;
        }
        h[k] = v;
        hPos[v] = k;
    }

    public void siftDown(int k)
    {
        int v, j;
        v = h[k];
        j = 2 * k;
        while (j <= N)
        {
            //Ensures there is something in the Heap, then finds the larger branch
            if (j + 1 <= N && dist[h[j]] > dist[h[j + 1]])
            {
                j++;
            }

            //comparing the position with the node being moved
            if (dist[h[j]] >= dist[v])
            {
                break;
            }

            h[k] = h[j];
            k = j;
            j = k * 2;
        }

        h[k] = v;
        hPos[v] = k;
    }


    public void insert( int x) 
    {
        h[++N] = x;
        siftUp( N);
    }


    public int remove() 
    {   
        int v = h[1];
        hPos[v] = 0; // v is no longer in heap
        h[N+1] = 0;  // put null node into empty spot

        h[1] = h[N--];
        siftDown(1);

        return v;
    }

}

class Graph {

    class Node {
        public int vert;
        public int wgt;
        public int data;
        public Node next;

    }

    // V = number of vertices
    // E = number of edges
    // adj[] is the adjacency lists array
    private int V, E;
    private Node[] adj;
    private Node z;
    private int[] mst;

    // used for traversing graph
    private int[] visited;
    private int id;


    // default constructor
    public Graph(String graphFile)  throws IOException
    {
        int u, v;
        int e, wgt;
        Node t;


        FileReader fr = new FileReader(graphFile);
        BufferedReader reader = new BufferedReader(fr);

        String splits = " +";  // multiple whitespace as delimiter
        String line = reader.readLine();        
        String[] parts = line.split(splits);
        System.out.println("Parts[] = " + parts[0] + " " + parts[1]);

        V = Integer.parseInt(parts[0]);
        E = Integer.parseInt(parts[1]);

        // create sentinel node
        z = new Node(); 
        z.next = z;

        // create adjacency lists, initialised to sentinel node z       
        adj = new Node[V+1];        
        for(v = 1; v <= V; ++v)
        {
            adj[v] = z;               
        }

       // read the edges
        System.out.println("Reading edges from text file");
        for(e = 1; e <= E; ++e)
        {
            line = reader.readLine();
            parts = line.split(splits);
            u = Integer.parseInt(parts[0]);
            v = Integer.parseInt(parts[1]); 
            wgt = Integer.parseInt(parts[2]);

           System.out.printf("Edge %1$s--(%2$s)--%3$s" + "\r\n", toChar(u), wgt, toChar(v));    

            // write code to put edge into adjacency matrix     
            t = new Node();
            t.data = wgt;
            t.vert = u;
            t.next = adj[v];

            adj[v] = t;

            t = new Node();
            t.data = wgt;
            t.vert = v;
            t.next = adj[u];

            adj[u] = t;
        }          
    }

    // convert vertex into char for pretty printing
    private char toChar(int u)
    {  
        return (char)(u + 64);
    }

    // method to display the graph representation
    public void display() {
        int v;
        Node n;

        for(v=1; v<=V; ++v){
            System.out.print("\nadj[" + toChar(v) + "] ->" );
            for(n = adj[v]; n != z; n = n.next) 
                System.out.print(" |" + toChar(n.vert) + " | " + n.wgt + "| ->");    
        }
        System.out.println("");
    }



    public void MST_Prim(int s)
    {
        int v, u;
        int wgt, wgt_sum = 0;
        int[]  dist, parent, hPos;
        Node t;

        //the distance from node to node
        dist = new int[V + 1];

        //the parent node
        parent = new int[V + 1];

        //current heap position
        hPos = new int[V + 1];


        // initialising parent and position to zero, and dist to the max value
        for (v = 1; v <= V; v++)
        {
            dist[v] = Integer.MAX_VALUE;
            parent[v] = 0;
            hPos[v] = 0;
        }

        Heap heap = new Heap(V + 1, dist, hPos);
        heap.insert(s);

        dist[s] = 0;
        for(v = 0; v<dist[v]; v++)
        {
            dist[v] = dist[v];
            parent[v] = 0;
            hPos[v] = 0;
        }

        dist[s] = 0;

        Heap pq =  new Heap(V, dist, hPos);
        pq.insert(s);

        while (!heap.isEmpty())
        {

            v = heap.remove();


           System.out.printf("\nAdding edge {0}--({1})--{2}", toChar(parent[v]), dist[v], toChar(v));


            //calculates the sum of the weights
            wgt_sum += dist[v];

            //prevents duplicates
            dist[v] = -dist[v];

            for (t = adj[v]; t != z; t = t.next)
            {

                if (t.data < dist[t.vert])
                {
                    dist[t.vert] = t.data;
                    parent[t.vert] = v;

                    //If the vertex is empty, insert next vertex
                    if (hPos[t.vert] == 0)
                    {
                        heap.insert(t.vert);
                    }
                    else //Else call sift up
                    {
                        heap.siftUp(hPos[t.vert]);
                    }
                }
            }
        }

            System.out.print("\n\nWeight of MST = " + wgt_sum + "\n");
            mst = parent;     
        }





    public void showMST()
    {
            System.out.println("Minimum Spanning tree parent array is:");
            for(int v = 1; v <= V; ++v)
            {
                System.out.println(toChar(v) + " -> " + toChar(mst[v]));
            }
            System.out.println("");
    }

}

public class PrimLists {
    public static void main(String[] args) throws IOException
    {

        int s = 2;
        String fname = "wGraph3.txt";               

        Graph g = new Graph(fname);

        g.display();


    }

} 

似乎在这两条线上发生例外,但我看不出它们有什么问题。

System.out.println("Parts[] = " + parts[0] + " " + parts[1]);
Graph g = new Graph(fname);

文件wGraph3.txt的内容如下

7   11
1   2   7
1   4   5
2   3   8
2   4   9
2   5   7
3   5   5
4   5   15
4   6   6
5   6   8
5   7   9
6   7   11

这种格式对我来说似乎很好,我一直在这方面工作,我只是没有看到问题。

0 个答案:

没有答案