简体   繁体   English

Dijkstra算法中使用的优先级队列的比较器类实现?

[英]Comparator class implementation for priority queue used in Dijkstra's Algorithm?

I'm trying to implement Dijsktra's Algorithm from CLRS - Introduction to Algorithms book,however, i'm having trouble about implementing a priority queue with Comparator interface. 我正在尝试从CLRS-算法入门一书中实现Dijsktra的算法,但是,在使用Comparator接口实现优先级队列时遇到了麻烦。 This is my Vertex class as you can see; 如您所见,这是我的Vertex类;

public class Vertex {

    public boolean explored;
    public int vertexID;
    public LinkedList<Vertex> adjacencyList;
    public LinkedList<Edge> edgeSet;
    public int shortestDistance;
    public Vertex predecessor;

    public Vertex(int vertexID){

        this.vertexID = vertexID;
        this.explored = false;
        this.adjacencyList = new LinkedList<>();
        this.edgeSet = new LinkedList<>();
        this.shortestDistance = Integer.MAX_VALUE;
        this.predecessor = null;
    }
}

So initially shortestDistance attribute is declared to Integer.MAX_VALUE . 所以最初shortestDistance属性被声明为Integer.MAX_VALUE Furthermore, you can see the class which implements from Comparator, is used for priority queue. 此外,您可以看到从Comparator实现的类,该类用于优先级队列。

public class WeightComparator implements Comparator<Vertex> {

    @Override
    public int compare(Vertex o1, Vertex o2) {

        return Math.min(o1.shortestDistance, o2.shortestDistance);
    }
}

I'm sure that the whole implementation doesn't have any logical errors due to my some tests,however, in some tests it fails. 我确信由于我的一些测试,整个实现不会有任何逻辑错误,但是,在某些测试中,它会失败。 I create a reference to my queue with this statement 我用此语句创建对队列的引用

PriorityQueue<Vertex> queue = new PriorityQueue<>(N, weightComparator);

where N is the size of the queue. 其中N是队列的大小。 So please correct me if i'm wrong about the way how it works. 因此,如果我对它的工作方式有误,请纠正我。 When you poll an item from it, it will remove the item which has least priority ?. 当您从中poll项目时,它将删除优先级最低的项目。 Hope it had been clear to understand my problem, and i will appreciate a lot if anyone can help about this. 希望能清楚地理解我的问题,如果有人可以提供帮助,我将非常感谢。 So thanks anyway 所以还是谢谢

Math.min gives you the smaller of two values. Math.min为您提供两个值中的较小者。 Returning that as a compare value is wrong. 将其作为比较值返回是错误的。

A compare return value of <0 means the first value is smaller than the second, but if you return Math.Min(5, 3) , you will get 3, which is >0, which means that the comparator will tell it that 3 > 5. Which is wrong. compare返回值<0表示第一个值小于第二个值,但是如果返回Math.Min(5, 3) ,则将获得3,即> 0,这意味着比较器将告诉它3 > 5.错了。

What you are looking for is: 您正在寻找的是:

public int compare(Vertex o1, Vertex o2) {
    return Integer.compare(o1.shortestDistance, o2.shortestDistance);
}

Unless shortestDistance can be negative, your comparator can never return a negative number. 除非shortestDistance可以为负,否则您的比较器永远不会返回负数。 It is therefore not a correct implementation. 因此,这不是正确的实现。

Conceptually, a comparator for primitives should return a subtraction: 从概念上讲,基元的比较器应返回减法:

return o1.shortestDistance-o2.shortestDistance;

or the other way around if you want descending. 或者如果想下降则反之。 But you need to beware of overflow issues. 但是您需要提防溢出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM