八、優先佇列、堆排序
優先佇列
一種常見的資料結構,需要支援兩種操作:刪除最大(最小)元素和插入元素。這種資料型別叫做優先佇列。
API
MaxPQ()//建立一個優先佇列
MaxPQ(int max)//建立一個最大容量為max的優先佇列
MaxPQ(key[] a)//用a[]中的元素建立一個優先佇列
void Insert()//向優先佇列中插入一個元素
key max()//向優先佇列中插入一個元素
key delMax()//刪除並返回最大元素
boolean isEmpty()//返回佇列是否為空
int size()//返回優先佇列中的元素個數
問題:輸入N個字串,每個字串都對應著一個整數,你的任務就是從中找出最大的(或者最小的)M個整數(及其關聯的字串)。這些輸入可能是金融事務,例如
初級實現
- 陣列實現(無序):刪的時才找最大的元素
- 有序陣列實現:insert的之後就排序
- 連結表示法:基於連結串列的下壓棧,可以選擇修改Push或者Pop來實現功能
對比(使用堆是比較理想的,下面將會討論):
堆得定義
定義:當一棵二叉樹的每個結點都大於等於它的兩個子節點時,它稱為堆有序的
相應地,在堆有序的二叉樹中,每個結點都小於等於它的父節點。從任意結點向上,我們都能得到一列非遞減的元素;從任意結點向下,我們都能得到一列非遞增的元素。特別的:
二叉堆表示法:
二叉堆:就是堆有序的完全二叉樹,元素在陣列中按照層級儲存(一層一層的放入陣列中,不用陣列的第一個元素)。下面簡稱堆
堆中:位置K的結點的父節點的位置為k/2,子節點的位置分別是2k和2k+1
一個結論:一棵大小為N的完全二叉樹的高度為lgN
堆的演算法
堆的有序化:就是使堆有序。一般會遇到兩種情況:
當某個節點的優先順序上升(或是在堆底加入一個新的元素時),我們需要由下至上的恢復堆的順序(上浮, 和父節點比較,大就交換)。
相反,我們要由上至下恢復元素(下沉,和子節點中較大的元素交換)。
給出基於堆得有序優先佇列程式碼(注意下沉和上浮操作,不難):
public class MaxPQ<Key> implements Iterable<Key> {
private Key[] pq; // store items at indices 1 to N
private int N; // number of items on priority queue
private Comparator<Key> comparator; // optional Comparator
public MaxPQ(int initCapacity) {
pq = (Key[]) new Object[initCapacity + 1];
N = 0;
}
public MaxPQ() {
this(1);
}
public MaxPQ(int initCapacity, Comparator<Key> comparator) {
this.comparator = comparator;
pq = (Key[]) new Object[initCapacity + 1];
N = 0;
}
public MaxPQ(Comparator<Key> comparator) {
this(1, comparator);
}
public MaxPQ(Key[] keys) {
N = keys.length;
pq = (Key[]) new Object[keys.length + 1];
for (int i = 0; i < N; i++)
pq[i+1] = keys[i];
for (int k = N/2; k >= 1; k--)
sink(k);
assert isMaxHeap();
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public Key max() {
if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
// helper function to double the size of the heap array
private void resize(int capacity) {
assert capacity > N;
Key[] temp = (Key[]) new Object[capacity];
for (int i = 1; i <= N; i++) {
temp[i] = pq[i];
}
pq = temp;
}
public void insert(Key x) {
// double size of array if necessary
if (N >= pq.length - 1) resize(2 * pq.length);
// add x, and percolate it up to maintain heap invariant
pq[++N] = x;
swim(N);
assert isMaxHeap();
}
public Key delMax() {
if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
Key max = pq[1];
exch(1, N--);
sink(1);
pq[N+1] = null; // to avoid loiterig and help with garbage collection
if ((N > 0) && (N == (pq.length - 1) / 4)) resize(pq.length / 2);
assert isMaxHeap();
return max;
}
private void swim(int k) {
while (k > 1 && less(k/2, k)) {
exch(k, k/2);
k = k/2;
}
}
private void sink(int k) {
while (2*k <= N) {
int j = 2*k;
if (j < N && less(j, j+1)) j++;
if (!less(k, j)) break;
exch(k, j);
k = j;
}
}
private boolean less(int i, int j) {
if (comparator == null) {
return ((Comparable<Key>) pq[i]).compareTo(pq[j]) < 0;
}
else {
return comparator.compare(pq[i], pq[j]) < 0;
}
}
private void exch(int i, int j) {
Key swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
}
// is pq[1..N] a max heap?
private boolean isMaxHeap() {
return isMaxHeap(1);
}
// is subtree of pq[1..N] rooted at k a max heap?
private boolean isMaxHeap(int k) {
if (k > N) return true;
int left = 2*k, right = 2*k + 1;
if (left <= N && less(k, left)) return false;
if (right <= N && less(k, right)) return false;
return isMaxHeap(left) && isMaxHeap(right);
}
public Iterator<Key> iterator() {
return new HeapIterator();
}
private class HeapIterator implements Iterator<Key> {
// create a new pq
private MaxPQ<Key> copy;
// add all items to copy of heap
// takes linear time since already in heap order so no keys move
public HeapIterator() {
if (comparator == null) copy = new MaxPQ<Key>(size());
else copy = new MaxPQ<Key>(size(), comparator);
for (int i = 1; i <= N; i++)
copy.insert(pq[i]);
}
public boolean hasNext() { return !copy.isEmpty(); }
public void remove() { throw new UnsupportedOperationException(); }
public Key next() {
if (!hasNext()) throw new NoSuchElementException();
return copy.delMax();
}
}
public static void main(String[] args) {
MaxPQ<String> pq = new MaxPQ<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) pq.insert(item);
else if (!pq.isEmpty()) StdOut.print(pq.delMax() + " ");
}
StdOut.println("(" + pq.size() + " left on pq)");
}
}
索引優先佇列
能引用已經進入優先佇列中的元素。多了change、delete等方法,能夠將索引為K的元素設為傳進來的item以及刪除索引位置的key。程式碼(這裡採用的是MinPQ):
注意理解這個的陣列pq和keys的作用(使用qp將不斷變化的pq陣列的元素和索引掛鉤,keys存值,很聰明的想法)
public class IndexMinPQ<Key extends Comparable<Key>> implements Iterable<Integer> {
private int maxN; // maximum number of elements on PQ
private int N; // number of elements on PQ
private int[] pq; // binary heap using 1-based indexing
private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i
private Key[] keys; // keys[i] = priority of i
public IndexMinPQ(int maxN) {
if (maxN < 0) throw new IllegalArgumentException();
this.maxN = maxN;
keys = (Key[]) new Comparable[maxN + 1]; // make this of length maxN??
pq = new int[maxN + 1];
qp = new int[maxN + 1]; // make this of length maxN??
for (int i = 0; i <= maxN; i++)
qp[i] = -1;
}
public boolean isEmpty() {
return N == 0;
}
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
return qp[i] != -1;
}
public int size() {
return N;
}
public void insert(int i, Key key) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
N++;
qp[i] = N;
pq[N] = i;
keys[i] = key;
swim(N);
}
public int minIndex() {
if (N == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key minKey() {
if (N == 0) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int delMin() {
if (N == 0) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1, N--);
sink(1);
assert min == pq[N+1];
qp[min] = -1; // delete
keys[min] = null; // to help with garbage collection
pq[N+1] = -1; // not needed
return min;
}
public Key keyOf(int i) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
else return keys[i];
}
public void changeKey(int i, Key key) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
keys[i] = key;
swim(qp[i]);
sink(qp[i]);
}
public void change(int i, Key key) {
changeKey(i, key);
}
public void decreaseKey(int i, Key key) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
if (keys[i].compareTo(key) <= 0)
throw new IllegalArgumentException("Calling decreaseKey() with given argument would not strictly decrease the key");
keys[i] = key;
swim(qp[i]);
}
public void increaseKey(int i, Key key) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
if (keys[i].compareTo(key) >= 0)
throw new IllegalArgumentException("Calling increaseKey() with given argument would not strictly increase the key");
keys[i] = key;
sink(qp[i]);
}
public void delete(int i) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
int index = qp[i];
exch(index, N--);
swim(index);
sink(index);
keys[i] = null;
qp[i] = -1;
}
private boolean greater(int i, int j) {
return keys[pq[i]].compareTo(keys[pq[j]]) > 0;
}
private void exch(int i, int j) {
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
private void swim(int k) {
while (k > 1 && greater(k/2, k)) {
exch(k, k/2);
k = k/2;
}
}
private void sink(int k) {
while (2*k <= N) {
int j = 2*k;
if (j < N && greater(j, j+1)) j++;
if (!greater(k, j)) break;
exch(k, j);
k = j;
}
}
public Iterator<Integer> iterator() { return new HeapIterator(); }
private class HeapIterator implements Iterator<Integer> {
// create a new pq
private IndexMinPQ<Key> copy;
// add all elements to copy of heap
// takes linear time since already in heap order so no keys move
public HeapIterator() {
copy = new IndexMinPQ<Key>(pq.length - 1);
for (int i = 1; i <= N; i++)
copy.insert(pq[i], keys[pq[i]]);
}
public boolean hasNext() { return !copy.isEmpty(); }
public void remove() { throw new UnsupportedOperationException(); }
public Integer next() {
if (!hasNext()) throw new NoSuchElementException();
return copy.delMin();
}
}
public static void main(String[] args) {
// insert a bunch of strings
String[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" };
IndexMinPQ<String> pq = new IndexMinPQ<String>(strings.length);
for (int i = 0; i < strings.length; i++) {
pq.insert(i, strings[i]);
}
// delete and print each key
while (!pq.isEmpty()) {
int i = pq.delMin();
StdOut.println(i + " " + strings[i]);
}
StdOut.println();
// reinsert the same strings
for (int i = 0; i < strings.length; i++) {
pq.insert(i, strings[i]);
}
// print each key using the iterator
for (int i : pq) {
StdOut.println(i + " " + strings[i]);
}
while (!pq.isEmpty()) {
pq.delMin();
}
}
}
索引優先佇列的使用案例:
多項歸併問題:將多個有序的輸入流歸併成一個有序(按照優先順序)的輸入流。程式碼:
public class Multiway {
// This class should not be instantiated.
private Multiway() { }
// merge together the sorted input streams and write the sorted result to standard output
private static void merge(In[] streams) {
int N = streams.length;
IndexMinPQ<String> pq = new IndexMinPQ<String>(N);
for (int i = 0; i < N; i++)
if (!streams[i].isEmpty())
pq.insert(i, streams[i].readString());
// Extract and print min and read next from its stream.
while (!pq.isEmpty()) {
StdOut.print(pq.minKey() + " ");
int i = pq.delMin();
if (!streams[i].isEmpty())
pq.insert(i, streams[i].readString());
}
StdOut.println();
}
public static void main(String[] args) {
int N = args.length;
In[] streams = new In[N];
for (int i = 0; i < N; i++)
streams[i] = new In(args[i]);
merge(streams);
}
}
堆排序
可以把任意優先佇列變成一種排序方法。將所有元素插入一個查詢最小元素的優先佇列,然後再重複呼叫刪除最小元素的操作來講它們按順序刪去。用無序陣列實現優先佇列這麼做相當於進行一次插入排序。下面討論堆排序。
堆的構造:從中間點到左邊掃描陣列(如下圖中的5開始),並呼叫sink函式(不要從左到右,因為後半元素都是葉子節點,還呼叫sink效率不高。)
程式碼:
public class HeapSort {
public static void sort(int[] a){
int N = a.length;
int[] keys = new int[N+1];
//注意,堆的資料結構是從1開始的,0不用
for (int i = 1; i < keys.length; i++) {
keys[i] = a[i-1];
}
// //構造堆,使得堆是有序的
for(int k = N/2;k>=1;k--) sink(keys,k,N);
//排序,相當於毀掉堆
while(N>1){
exch(keys,1,N--);
sink(keys,1,N);
}
//重新寫回陣列
for (int i = 0; i < a.length; i++) {
a[i] = keys[i+1];
}
}
private static void sink(int[] a, int k, int N) {
// TODO Auto-generated method stub
while(2*k<=N){
int j = 2*k;
if (j < N && less(a[j], a[j+1])) j++;
if (less(a[j], a[k])) break;
exch(a, k, j);
k = j;
}
}
private static boolean less(int k, int j) {
// TODO Auto-generated method stub
if (k<j) return true;
return false;
}
private static void exch(int[] a, int i, int n) {
// TODO Auto-generated method stub
int temp = a[i];
a[i] = a[n];
a[n] = temp;
}
public static void main(String[] args) {
int[] a = {2,4,7,8,2,1,0,9};
HeapSort.sort(a);
System.out.println(Arrays.toString(a));
}
}