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?

1
  • PriorityQueue<Vertex>. 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. Commented Apr 7, 2018 at 17:01

1 Answer 1

0

Try this instead:

static class Vertex implements Comparable<Vertex> {
Sign up to request clarification or add additional context in comments.

2 Comments

...and implement the required method, of course.
Which is a little bit down in the code: public int compareTo(Vertex other) {

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.