简体   繁体   English

JS中的优先级队列

[英]priority queue in JS

I am trying to use a priority Queue for my own data type which is a 3x3 array. 我正在尝试为我自己的数据类型(3x3数组)使用优先级队列。 The actual code and data type is more complex so I boiled it down to the essentials. 实际的代码和数据类型更为复杂,因此我将其归结为要点。 Note that the priority queue works well for an integer data type (see output at the end). 请注意,优先级队列对于整数数据类型效果很好(请参阅最后的输出)。 Everything is self explanatory. 一切都是自我解释。 Assume the metric function returns a positive integer for a given 3X3 array. 假设度量函数为给定的3X3数组返回一个正整数。 I am confused why the dequeueing from the heap does not return the min valued object (or max valued in case I got the comparator backwards but I got the middle value for both). 我很困惑,为什么从堆中出队不会返回最小值对象(或最大值,以防万一我把比较器向后但都得到中间值)。 For the data type of integer the priority Queue seems to work correctly as the output shows. 对于整数数据类型,优先级队列似乎可以正常工作,如输出所示。

    var r = require('js-priority-queue');



    metric = function (A) {
    N =  A.length;
    if (A[0][1] == 0) return 123;
    if (A[0][1] == 5) return 124;
    if (A[0][1] == 1) return 122;
    if (A[0][1] == 6) return 122;
    return 0;
}


    mComparator = function (m1, m2) {
      ret = metric(m2) - metric(m1);
      return ret;
   }

   mHeap = new r(mComparator);

   nHeap = new r(function (a,b) {
      return b - a;
   })

   A = [[5, 0, 1], [7, 6, 3], [2, 4, 8]];
   B = [[5, 6, 1], [7, 0, 3], [2, 4, 8]];

   C = [[5, 1, 0], [7, 6, 3], [2, 4, 8]];
   D = [[0, 5, 1], [7, 6, 3], [2, 4, 8]];

   console.log("metric(A) -> %d", metric(A));
   console.log("metric(B) -> %d", metric(B));
   console.log("metric(C) -> %d", metric(C));
   console.log("metric(D) -> %d", metric(D));

   mHeap.queue(A);
   mHeap.queue(B);
   mHeap.queue(C);
   mHeap.queue(D);

   X = mHeap.dequeue();
   console.log(X);

   X = mHeap.dequeue();
   console.log(X);

   X = mHeap.dequeue();
   console.log(X);

   X = mHeap.dequeue();
   console.log(X);

   nHeap.queue(123);
   nHeap.queue(124);
   nHeap.queue(122);
   nHeap.queue(122);

   y = nHeap.dequeue();
   console.log(y);


#Output 
metric(A) -> 123
metric(B) -> 122
metric(C) -> 122
metric(D) -> 124
[ [ 5, 0, 1 ], [ 7, 6, 3 ], [ 2, 4, 8 ] ]
[ [ 0, 5, 1 ], [ 7, 6, 3 ], [ 2, 4, 8 ] ]
[ [ 5, 1, 0 ], [ 7, 6, 3 ], [ 2, 4, 8 ] ]
[ [ 5, 6, 1 ], [ 7, 0, 3 ], [ 2, 4, 8 ] ]
122

If you are using the adamhooper priority queue, then your problem is you are not supplying the comparator correctly. 如果使用的是adamhooper优先级队列,那么问题是您没有正确提供比较器。 Change your constructor invocations to: 将构造函数调用更改为:

mHeap = new r({"comparator": mComparator});

nHeap = new r({"comparator": function (a,b) {
    return b - a;
}})

And you should start getting expected results. 您应该开始获得预期的结果。 Note that this gives you max-heaps . 请注意,这为您提供了max-heaps Since you wanted min-heaps, you should also reverse your comparison order: 由于您需要最小堆,因此您还应该反转比较顺序:

mComparator = function (m1, m2) {
    ret = metric(m1) - metric(m2);
    return ret;
}

mHeap = new r({"comparator": mComparator});

nHeap = new r({"comparator": function (a,b) {
    return a - b;
}})

Example code for correctly supplying a comparator is given on the github project's front page, but I see how it could be easy to miss. github项目的首页给出了正确提供比较器的示例代码,但是我发现它很容易遗漏。

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

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