0

I have implemented Dijkstra's Algorithm using PriorityQueue. However on running the code, I get the following exception :

Exception in thread "main" java.lang.ClassCastException: Dijkstra$Vertex cannot be cast to java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(Unknown Source)
    at java.util.PriorityQueue.siftUp(Unknown Source)
    at java.util.PriorityQueue.offer(Unknown Source)
    at java.util.PriorityQueue.add(Unknown Source)
    at Dijkstra.dijkstra(Dijkstra.java:55)
    at Dijkstra.main(Dijkstra.java:89)

The code used is :

import java.util.HashSet;
import java.util.PriorityQueue;


public class Dijkstra {
    static class Vertex{
        private int vertexid;
        private Double distance;

        public Vertex(int vertexid, Double distance) {
            this.vertexid = vertexid;
            this.distance = distance;
        }

        public int getVertexid() {
            return vertexid;
        }

        public Double getDistance() {
            return distance;
        }

        public int compareTo(Vertex other) {
            return this.getDistance().compareTo(other.getDistance());
        }

        public boolean equals(Object o) {
            if (o instanceof Vertex) {
                Vertex v = (Vertex) o;
                return vertexid == v.vertexid && distance == v.distance;
            }
            return false;
        }
    }

    public static void dijkstra(double g[][], int n, int m, int source) {
        // g is the adjacency matrix
        // n is the number of nodes
        // m is the number of edges

        // initialize shortest path

        double d[] = new double[n];

        d[source] = 0;
        for (int i = 0; i < n; i++) {
            d[i] = Double.POSITIVE_INFINITY;
        }

        HashSet<Integer> s = new HashSet<Integer>();
        PriorityQueue<Vertex> q = new PriorityQueue<Vertex>();

        // initialize q
        for (int i = 0; i < n; i++) {
            q.add(new Vertex(i, d[i]));
        }

        Vertex u;

        while (!q.isEmpty()) {
            u = q.remove();
            System.out.println(u.getVertexid() + "\t" + u.getDistance());
            s.add(u.getVertexid());

            for (int i = 0; i < n; i++) {
                if (i != u.getVertexid() && g[u.getVertexid()][i] != Double.POSITIVE_INFINITY) {
                    if (u.getDistance().doubleValue() + g[u.getVertexid()][i] < d[i]) {
                        q.remove(new Vertex(i, d[i]));
                        d[i] = u.getDistance().doubleValue() + g[u.getVertexid()][i];
                        q.add(new Vertex(i, d[i]));
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        double graph[][] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
                {4, 0, 8, 0, 0, 0, 0, 11, 0},
                {0, 8, 0, 7, 0, 4, 0, 0, 2},
                {0, 0, 7, 0, 9, 14, 0, 0, 0},
                {0, 0, 0, 9, 0, 10, 0, 0, 0},
                {0, 0, 4, 14, 10, 0, 2, 0, 0},
                {0, 0, 0, 0, 0, 2, 0, 1, 6},
                {8, 11, 0, 0, 0, 0, 1, 0, 7},
                {0, 0, 2, 0, 0, 0, 6, 7, 0}
               };

        Dijkstra.dijkstra(graph, 8, 14, 0);
    }
}

The Vertex class is used to create a structure that basically stores the vertex and its label distance. The priority queue will work on objects of this class, and will use the distance value as the ordering value for remove operations. How to rectify the exception?

kauray
  • 681
  • 1
  • 10
  • 26
  • `PriorityQueue`. You have said that you want `Vertex` instances to be sorted in sorted order. You have then neither provided a `Comparator` nor implemented `Comparable` in `Vertex`. – Boris the Spider Apr 07 '18 at 17:01

1 Answers1

0

Try this instead:

static class Vertex implements Comparable<Vertex> {
SeverityOne
  • 2,062
  • 7
  • 23