Question

What is the type PriorityVertex as seen here? I am also writing a getMinSpanningTree method.

Prim's Algorithm

Was it helpful?

Solution

The PriorityVertex from this example is just a class that the asker created. It is a simple implementation of a vertex in a graph.

This class implements the Comparable interface. The reason for that is that PriorityQueue must compare its elements. This can be achieved in two ways:

  • The elements must either implement the Comparable interface
  • or a Comparator has to be passed to the PriorityQueue, that tells the queue how to compare the elements.

So the PriorityVertex class could roughly look as follows:

class PriorityVertex implements Comparable<PriorityVertex>
{
    private float priority;

    // Setters, getters ... whatever the vertex needs
    ...

    // Implementation of the Comparable interface:
    @Override 
    public int compareTo(PriorityVertex other)
    {
        return Float.compare(this.priority, other.priority);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top