如何在matlab中实现优先级队列

如何在matlab中实现优先级队列,matlab,Matlab,matlab中是否有提供min priorityqueue功能的库 import java.util.PriorityQueue; import java.util.*; public class MyQueue { Comparator<Double> c; PriorityQueue<Double> PQ; public MyQueue() { c = new Comparator<Double>(){ pu

matlab中是否有提供min priorityqueue功能的库

import java.util.PriorityQueue;
import java.util.*;

public class MyQueue {
  Comparator<Double> c;
  PriorityQueue<Double> PQ;

  public MyQueue() {
    c = new Comparator<Double>(){
            public int compare(Double o1, Double o2){
              if(o2 > o1) {
                return -1;
              } else if(o1 > o2) {
                return 1;
              } else {
                return 0;
              }
            }
        };
    PQ = new PriorityQueue<Double>(1000,c);
  }

  public void addElement(double d) {
    PQ.add(d);
  }

  public double removeElement() {
    return(PQ.remove());
  }
}
import java.util.PriorityQueue;
导入java.util.*;
公共类MyQueue{
比较器c;
优先队列PQ;
公共MyQueue(){
c=新的比较器(){
公共整数比较(双o1,双o2){
如果(o2>o1){
返回-1;
}如果(o1>o2),则为else{
返回1;
}否则{
返回0;
}
}
};
PQ=新的优先队列(1000,c);
}
公共无效补遗(双d){
PQ.增加(d);
}
公开双重删除(){
返回(PQ.remove());
}
}

我已经用java实现了这个优先队列。我可以从matlab中调用它。但是,我需要将每个成本与一个索引关联起来。我的意思是,我需要存储的不仅仅是节点的成本,还有它的索引。我怎样才能做到这一点。我需要从matlab传递索引

您可以使用Java的默认
PriorityQueue
如下所示:

>> q=java.util.PriorityQueue;
>> q.add({value,index});
这是从Java开始提供的≥ 1.5版本,自7.0.4(R14)以来的所有MATLAB版本中都预先绑定了该版本

否则,您可以使用中的一个,您必须编译它


它也有一个块,但我怀疑这就是你想要的。

下面是一个完全用matlab编写的优先级队列的调整大小数组实现。您可以附加/耦合任何类型的数据/索引以及优先级值。此外,您还可以在创建最小优先级队列和最大优先级队列时,通过传递给构造函数的布尔参数在它们之间切换行为

classdef PriorityQueue < handle

    properties (SetAccess = private)               
       numElements; 
       priorityList;
       valueList;
       flagMaxPriorityQueue;
    end

    methods (Access = public)       

        function obj = PriorityQueue( flagMaxPriorityQueue )

            if ~exist( 'flagMaxPriorityQueue', 'var' )
                flagMaxPriorityQueue = true;
            else
                if ~(isscalar(flagMaxPriorityQueue) && islogical(flagMaxPriorityQueue))
                    error( 'ERROR: invalid flagMaxPriorityQueue argument' );
                end
            end

            obj.flagMaxPriorityQueue = flagMaxPriorityQueue;
            obj.numElements = 0;
            obj.priorityList = {};
            obj.valueList = {};

        end

        function insert(obj, priority, value)     

            % increase the size of the array if full
            if obj.numElements > 0 && obj.numElements + 1 > numel( obj.priorityList )                                

                % double the size of the array and copy stuff
                obj.priorityList = cat(1, obj.priorityList, cell(obj.numElements, 1));
                obj.valueList = cat(1, obj.valueList, cell(obj.numElements, 1));

            end

            obj.numElements = obj.numElements + 1;

            obj.priorityList{ obj.numElements } = priority;
            obj.valueList{ obj.numElements } = value;

            obj.swim(obj.numElements);

        end

        function [priority, value] = pop( obj )

            if obj.isEmpty()
                error( 'called pop() on an empty priority queue' );
            end          

            priority = obj.priorityList{1};
            value = obj.valueList{1};

            obj.exch(1, obj.numElements);            
            obj.numElements = obj.numElements - 1;            
            obj.sink(1);

            obj.priorityList{ obj.numElements + 1 } = [];
            obj.valueList{ obj.numElements + 1 } = [];

            % halve the size of the arrays if they get one-quarter full
            if obj.numElements > 0 && obj.numElements == floor( numel( obj.priorityList ) / 4 )                

                obj.priorityList( 2 * obj.numElements + 1 : end ) = [];
                obj.valueList( 2 * obj.numElements + 1 : end ) = [];

            end

        end

        function [flagEmpty] = isEmpty( obj )        

            flagEmpty = (obj.numElements == 0);

        end

        function [qSize] = size( obj )

            qSize = obj.numElements;

        end

        function [priority, value] = peek( obj )

            if obj.isEmpty()
                error( 'requested max() of an empty priority queue' );
            end          

            priority = obj.priorityList{1};
            value = obj.valueList{1};

        end

    end    

    methods (Access = private)

        function swim(obj, elPos)

            while elPos > 1 && obj.compare(floor(elPos / 2), elPos)

                obj.exch(floor(elPos / 2), elPos);
                elPos = floor(elPos / 2);

            end

        end

        function sink(obj, elPos)

            while 2 * elPos <= obj.numElements

                j = 2 * elPos;

                if j < obj.numElements && obj.compare(j, j+1)
                    j = j + 1;
                end

                if ~obj.compare(elPos, j)
                    break;
                end

                obj.exch(elPos, j);
                elPos = j;

            end

        end

        function [blnCmpResult] = compare(obj, e1, e2)

            if obj.flagMaxPriorityQueue
                blnCmpResult = (obj.priorityList{e1} < obj.priorityList{e2});
            else
                blnCmpResult = (obj.priorityList{e1} > obj.priorityList{e2});
            end            

        end

        function exch(obj, e1, e2 )

            temp = obj.priorityList{e1};
            obj.priorityList{e1} = obj.priorityList{e2};
            obj.priorityList{e2} = temp;            

            temp = obj.valueList{e1};
            obj.valueList{e1} = obj.valueList{e2};
            obj.valueList{e2} = temp;            

        end

    end

end % classdef
classdef PriorityQueue0&&obj.numElements+1>numel(obj.priorityList)
%将数组的大小增加一倍并复制内容
obj.priorityList=cat(1,obj.priorityList,cell(obj.numElements,1));
obj.valueList=cat(1,obj.valueList,单元格(obj.numElements,1));
结束
obj.numElements=obj.numElements+1;
obj.priorityList{obj.numElements}=优先级;
obj.valueList{obj.numElements}=值;
对象游泳(对象Numelents);
结束
函数[优先级,值]=pop(obj)
如果对象为空()
错误('在空优先级队列上调用了pop());
结束
优先级=对象优先级列表{1};
value=obj.valueList{1};
实物交换(1,实物余额);
obj.numElements=obj.numElements-1;
目标水槽(1);
obj.priorityList{obj.numElements+1}=[];
对象值列表{obj.numElements+1}=[];
%如果阵列已满四分之一,则将其大小减半
如果obj.numElements>0&&obj.numElements==楼层(numel(obj.priorityList)/4)
对象优先级列表(2*obj.numElements+1:end)=[];
对象价值表(2*obj.numElements+1:end)=[];
结束
结束
函数[flagmpty]=isEmpty(obj)
鞭毛=(obj.numelents==0);
结束
函数[qSize]=大小(obj)
qSize=对象数量;
结束
函数[优先级,值]=peek(obj)
如果对象为空()
错误('requested max()为空优先级队列');
结束
优先级=对象优先级列表{1};
value=obj.valueList{1};
结束
结束
方法(访问=私有)
功能游泳(obj、elPos)
当elPos>1和对象比较时(楼层(elPos/2),elPos)
obj.exch(楼层(elPos/2),elPos);
elPos=楼层(elPos/2);
结束
结束
功能接收器(obj、elPos)
而2*elPos obj.priorityList{e2});
结束
结束
功能exch(obj、e1、e2)
temp=obj.priorityList{e1};
obj.priorityList{e1}=obj.priorityList{e2};
obj.priorityList{e2}=temp;
temp=对象值列表{e1};
obj.valueList{e1}=obj.valueList{e2};
对象值列表{e2}=temp;
结束
结束
结束%classdef

请在您的问题中提供更多详细信息。我不知道“min priorityqueue”指的是什么。那是什么语言?它的功能是什么?如何创建最小堆。所以它总是返回最小元素。我可以看到我需要使用一个比较器,我需要用Java语言定义它,并在matlab中使用它。您能告诉我如何在中使用自己的java类吗matlab@user34790一点谷歌搜索出现了,一个,一个。这将使您对如何在Matlab中加载自定义类有一个很好的了解,并让您开始使用比较器类。感谢链接。但是,我还需要将成本与索引关联起来。所以我需要存储的不仅仅是成本,还有索引。如何在优先级中传递和存储这些索引