2012-09-18 43 views
2

優先級隊列中是否有MATLAB提供分的功能的PriorityQueue如何實現在MATLAB

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()); 
    } 
} 

我已經實現了在Java這個先決隊列中的所有庫。我可以從matlab中調用它。但是,我需要將每個成本與索引關聯起來。我的意思是,這不僅是我需要存儲的節點的成本,還有它的索引。我怎樣才能做到這一點。我需要從MATLAB

+0

請在您的問題中提供更多詳細信息。我不知道「min priorityqueue」是指什麼。那是什麼語言?它的功能是什麼? – cjh

回答

4

由於發現here通過索引,你可以使用默認的Java PriorityQueue像這樣:

>> q=java.util.PriorityQueue; 
>> q.add({value,index}); 

這是可用,因爲Java 1.5中,這是所有Matlab的預先捆綁的釋放,因爲7.0.4。
否則,您可以使用file exchange中的那個,您必須編譯它。

還有一個Simulink塊,但我懷疑這是你的後。

+0

如何創建最小堆。所以它總是返回最小元素。 – user34790

+0

我可以看到我需要使用一個比較器,我需要用Java語言定義並在matlab中使用它。你能告訴我如何在matlab中使用我自己的java類 – user34790

+1

@ user34790有一點谷歌搜索出現了[本站點](http://www.mathworks.de/matlabcentral/answers/37185-cannot-call-java- class-from-matlab)和[this](http://www.cs.yale.edu/homes/spielman/ECC/javaMatlab.html)之一,[this](http://www.mathworks.com/matlabcentral/newsreader/view_thread/266022)一。這應該給你一個關於如何在Matlab中加載自定義類的好主意,並讓你開始使用Comparator類。 –

3

下面是一個完全用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