アルゴリズム導論Java実装-優先順位行列(6.5章)


優先列は、スタックデータ構造の典型的な応用である。優先順位の行列の典型的なアプリケーションは、現在の優先順位が最も高いタスクを優先的に実行するタスクの限られたスケジュールである。キューの一つのタスクは、INSERTメソッドを呼び出すことです。

  
  
  
  
  1. package lhz.algorithm.chapter.six; 
  2. /** 
  3.  * “ ”,《 》6.5  
  4.  *  : 
  5.  * A priority queue is a data structure for maintaining a set S of elements, each with an 
  6.  * associated value called a key. A max-priority queue supports the following operations. 
  7.  * • INSERT(S, x) inserts the element x into the set S. This operation could be written as S← S{x}. 
  8.  * • MAXIMUM(S) returns the element of S with the largest key. 
  9.  * • EXTRACT-MAX(S) removes and returns the element of S with the largest key. 
  10.  * • INCREASE-KEY(S, x, k) increases the value of element x's key to the new value k, 
  11.  * which is assumed to be at least as large as x's current key value. 
  12.  *  , 。 。 
  13. * : http://mushiqianmeng.blog.51cto.com/3970029/743611
  14.  * @author lihzh( coder) 
  15.  */ 
  16. public class PriorityQueue { 
  17.      
  18.     private final int DEFAULT_CAPACITY_VALUE = 16
  19.     // 16 ( ), 16 HashMap  
  20.     private int capacity = DEFAULT_CAPACITY_VALUE; 
  21.     private int[] quene = new int[capacity]; 
  22.     //  
  23.     private int heapSize = 0
  24.  
  25.     /** 
  26.      *   
  27.      * @return 
  28.      */ 
  29.     public int maximum() { 
  30.         return quene[0]; 
  31.     } 
  32.      
  33.     /** 
  34.      *  ,  
  35.      *  INCREASE-Key ,  
  36.      *  : 
  37.      * MAX-HEAP-INSERT(A, key) 
  38.      * 1 heap-size[A] ← heap-size[A] + 1 
  39.      * 2 A[heap-size[A]] ← -∞ 
  40.      * 3 HEAP-INCREASE-KEY(A, heap-size[A], key) 
  41. *  :O(lg n) 
  42.  
  43.      * @param value   
  44.      */ 
  45.     public void insert(int value) { 
  46.         //  1 
  47.         quene[heapSize] = value; 
  48.         heapSize++; 
  49.         increaceKey(heapSize, value); 
  50.     } 
  51.      
  52.     /** 
  53.      *  , MaxHeap。 
  54.      *   
  55.      *  : 
  56.      * HEAP-INCREASE-KEY(A, i, key) 
  57.      * 1 if key < A[i] 
  58.      * 2 then error "new key is smaller than current key" 
  59.      * 3 A[i] ← key 
  60.      * 4 while i > 1 and A[PARENT(i)] < A[i] 
  61.      * 5 do exchange A[i] ↔ A[PARENT(i)] 
  62.      * 6 i ← PARENT(i) 
  63.      *  :O(lg n) 
  64.      * @param index   
  65.      * @param newValue   
  66.      */ 
  67.     public void increaceKey (int heapIndex, int newValue) { 
  68.         if (newValue < quene[heapIndex-1]) { 
  69.             System.err.println(" : !"); 
  70.             return
  71.         } 
  72.         quene[heapIndex-1] = newValue; 
  73.         int parentIndex = heapIndex / 2
  74.         while (parentIndex > 0 && quene[parentIndex-1] < newValue ) { 
  75.             int temp = quene[parentIndex-1]; 
  76.             quene[parentIndex-1] = newValue; 
  77.             quene[heapIndex-1] = temp; 
  78.             heapIndex = parentIndex; 
  79.             parentIndex = parentIndex / 2
  80.         } 
  81.     } 
  82.      
  83.     /** 
  84.      *  ( ),  
  85.      *  : 
  86.      * HEAP-EXTRACT-MAX(A) 
  87.      * 1 if heap-size[A] < 1 
  88.      * 2 then error "heap underflow" 
  89.      * 3 max ← A[1] 
  90.      * 4 A[1] ← A[heap-size[A]] 
  91.      * 5 heap-size[A] ← heap-size[A] - 1 
  92.      * 6 MAX-HEAPIFY(A, 1) 
  93.      * 7 return max 
  94.      *  :O(lg n), 
  95.      * @return 
  96.      */ 
  97.     public int extractMax() { 
  98.         if (heapSize < 1) { 
  99.             System.err.println(" !"); 
  100.             return -1
  101.         } 
  102.         int max = quene[0]; 
  103.         quene[0] = quene[heapSize-1]; 
  104.         heapSize--; 
  105.         maxHeapify(quene, 1); 
  106.         return max; 
  107.     } 
  108.      
  109.      
  110.     /** 
  111.      *   
  112.      * @param array 
  113.      * @param index 
  114.      */ 
  115.     private  void maxHeapify(int[] array, int index) { 
  116.         int l = index * 2
  117.         int r = l + 1
  118.         int largest; 
  119.         // , ,  
  120.         if (l <= heapSize && array[l-1] > array[index-1]) { 
  121.             largest = l; 
  122.         } else { 
  123.             largest = index; 
  124.         } 
  125.         // , ,  
  126.         if (r <= heapSize && array[r-1] > array[largest-1]) { 
  127.             largest = r; 
  128.         } 
  129.         // , 。 
  130.         if (largest != index) { 
  131.             int temp = array[index-1]; 
  132.             array[index-1] = array[largest-1]; 
  133.             array[largest-1] = temp; 
  134.             maxHeapify(array, largest); 
  135.         } 
  136.     } 
 
簡単なテストコード:

  
  
  
  
  1. public static void main(String[] args) { 
  2.         PriorityQueue q = new PriorityQueue(); 
  3.         q.insert(2); 
  4.         q.insert(6); 
  5.         q.insert(3); 
  6.         q.insert(8); 
  7.         q.insert(7); 
  8.         q.insert(9); 
  9.         q.insert(1); 
  10.         q.insert(10); 
  11.         q.insert(9); 
  12.         System.out.println(q.extractMax()); 
  13.         System.out.println(q.extractMax()); 
  14.         q.insert(9); 
  15.         q.insert(1); 
  16.         q.insert(10); 
  17.         System.out.println(q.extractMax()); 
  18.         System.out.println(q.extractMax()); 
  19.         System.out.println(q.extractMax()); 
  20.         System.out.println(q.extractMax()); 
  21.         System.out.println(q.extractMax()); 
  22.         System.out.println(q.extractMax()); 
  23.         System.out.println(q.extractMax()); 
  24.     }