优先队列(Priority Queues)
algs4
目前已经把 algs4 中的优先队列这一节阅读完毕。我改变以前的看法,以前我认为通过 algs4 这本书我是一个算法都学不会,真要学算法还是得看算法导论,这是一年前在看 KMP 算法时的想法,更早之前阅读前几章时我也是这么想的,之前不管是哪一个时段,我都没有把书中的源码给跑起来,加上那个 KMP 算法它讲得确实有点地方有问题,现在我明白了,可能作者的水平真的是很高,所有很多东西他的脑袋里面是清清楚楚的,但是他水平太高了,所以写出来的时候不免思维有些跳跃,这对于我们想要去钻每一个牛角尖的读者来说就有些难搞了。
好在这个优先队列的部分写得很好,加上我的编程经验的提升,像编译源码之类的问题,是不可能绊住在这个地方的,因此,可以亲手运行得出正确的结果,这也增大了我学习这个算法的欲望。
至于其他的算法,用到的时候再来看一看,到时再作评价。
现在来看,这个代码的质量确实是经过千锤百炼的。很结实。
用例
/******************************************************************************
* Compilation: javac TopM.java
* Execution: java TopM m < input.txt
* Dependencies: MinPQ.java Transaction.java StdIn.java StdOut.java
* Data files: https://algs4.cs.princeton.edu/24pq/tinyBatch.txt
*
* Given an integer m from the command line and an input stream where
* each line contains a String and a long value, this MinPQ client
* prints the m lines whose numbers are the highest.
*
* % java TopM 5 < tinyBatch.txt
* Thompson 2/27/2000 4747.08
* vonNeumann 2/12/1994 4732.35
* vonNeumann 1/11/1999 4409.74
* Hoare 8/18/1992 4381.21
* vonNeumann 3/26/2002 4121.85
*
******************************************************************************/
package algs4;
/**
* The {@code TopM} class provides a client that reads a sequence of
* transactions from standard input and prints the <em>m</em> largest ones
* to standard output. This implementation uses a {@link MinPQ} of size
* at most <em>m</em> + 1 to identify the <em>M</em> largest transactions
* and a {@link Stack} to output them in the proper order.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class TopM {
// This class should not be instantiated.
private TopM() { }
/**
* Reads a sequence of transactions from standard input; takes a
* command-line integer m; prints to standard output the m largest
* transactions in descending order.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
MinPQ<Transaction> pq = new MinPQ<Transaction>(m+1);
while (StdIn.hasNextLine()) {
// Create an entry from the next line and put on the PQ.
String line = StdIn.readLine();
Transaction transaction = new Transaction(line);
pq.insert(transaction);
// remove minimum if m+1 entries on the PQ
if (pq.size() > m)
pq.delMin();
} // top m entries are on the PQ
// print entries on PQ in reverse order
Stack<Transaction> stack = new Stack<Transaction>();
for (Transaction transaction : pq)
stack.push(transaction);
for (Transaction transaction : stack)
StdOut.println(transaction);
}
}
/******************************************************************************
* Copyright 2002-2020, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
注意,我们最好把 alg4 包中的所有代码全部编译出来,然后再执行程序。
然后,在 powershell 中测试用例:
Get-Content .\tinyBatch.txt | java algs4.TopM 5
初级实现
随书代码给了一个优先队列的有序数组实现,这里来测试一下。
/******************************************************************************
* Compilation: javac OrderedArrayMaxPQ.java
* Execution: java OrderedArrayMaxPQ
* Dependencies: StdOut.java
*
* Priority queue implementation with an ordered array.
*
* Limitations
* -----------
* - no array resizing
* - does not check for overflow or underflow.
*
*
******************************************************************************/
package exer;
public class OrderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
private int n; // number of elements
// set inititial size of heap to hold size elements
public OrderedArrayMaxPQ(int capacity) {
pq = (Key[]) (new Comparable[capacity]);
n = 0;
}
public boolean isEmpty() { return n == 0; }
public int size() { return n; }
public Key delMax() { return pq[--n]; }
public void insert(Key key) {
int i = n-1;
while (i >= 0 && less(key, pq[i])) {
pq[i+1] = pq[i];
i--;
}
pq[i+1] = key;
n++;
}
/***************************************************************************
* Helper functions.
***************************************************************************/
private boolean less(Key v, Key w) {
return v.compareTo(w) < 0;
}
/***************************************************************************
* Test routine.
***************************************************************************/
public static void main(String[] args) {
OrderedArrayMaxPQ<String> pq = new OrderedArrayMaxPQ<String>(10);
pq.insert("this");
pq.insert("is");
pq.insert("a");
pq.insert("test");
while (!pq.isEmpty())
StdOut.println(pq.delMax());
}
}
PS …\myalgs4\exer> java exer.OrderedArrayMaxPQ
this
test
is
a
无序数组实现:
/******************************************************************************
* Compilation: javac UnorderedArrayMaxPQ.java
* Execution: java UnorderedArrayMaxPQ
* Dependencies: StdOut.java
*
* Priority queue implementation with an unsorted array.
*
* Limitations
* -----------
* - no array resizing
* - does not check for overflow or underflow.
*
******************************************************************************/
package exer;
public class UnorderedArrayMaxPQ<Key extends Comparable<Key>> {
private Key[] pq; // elements
private int n; // number of elements
// set inititial size of heap to hold size elements
public UnorderedArrayMaxPQ(int capacity) {
pq = (Key[]) new Comparable[capacity];
n = 0;
}
public boolean isEmpty() { return n == 0; }
public int size() { return n; }
public void insert(Key x) { pq[n++] = x; }
public Key delMax() {
int max = 0;
for (int i = 1; i < n; i++)
if (less(max, i)) max = i;
exch(max, n-1);
return pq[--n];
}
/***************************************************************************
* Helper functions.
***************************************************************************/
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0;
}
private void exch(int i, int j) {
Key swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
}
/***************************************************************************
* Test routine.
***************************************************************************/
public static void main(String[] args) {
UnorderedArrayMaxPQ<String> pq = new UnorderedArrayMaxPQ<String>(10);
pq.insert("this");
pq.insert("is");
pq.insert("a");
pq.insert("test");
while (!pq.isEmpty())
StdOut.println(pq.delMax());
}
}
PS …\myalgs4\exer> java exer.UnorderedArrayMaxPQ
this
test
is
a
基于堆的优先队列
/******************************************************************************
* Compilation: javac MaxPQ.java
* Execution: java MaxPQ < input.txt
* Dependencies: StdIn.java StdOut.java
* Data files: https://algs4.cs.princeton.edu/24pq/tinyPQ.txt
*
* Generic max priority queue implementation with a binary heap.
* Can be used with a comparator instead of the natural order,
* but the generic Key type must still be Comparable.
*
* % java MaxPQ < tinyPQ.txt
* Q X P (6 left on pq)
*
* We use a one-based array to simplify parent and child calculations.
*
* Can be optimized by replacing full exchanges with half exchanges
* (ala insertion sort).
*
******************************************************************************/
package algs4;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The {@code MaxPQ} class represents a priority queue of generic keys.
* It supports the usual <em>insert</em> and <em>delete-the-maximum</em>
* operations, along with methods for peeking at the maximum key,
* testing if the priority queue is empty, and iterating through
* the keys.
* <p>
* This implementation uses a <em>binary heap</em>.
* The <em>insert</em> and <em>delete-the-maximum</em> operations take
* Θ(log <em>n</em>) amortized time, where <em>n</em> is the number
* of elements in the priority queue. This is an amortized bound
* (and not a worst-case bound) because of array resizing operations.
* The <em>min</em>, <em>size</em>, and <em>is-empty</em> operations take
* Θ(1) time in the worst case.
* Construction takes time proportional to the specified capacity or the
* number of items used to initialize the data structure.
* <p>
* For additional documentation, see
* <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*
* @param <Key> the generic type of key on this priority queue
*/
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
/**
* Initializes an empty priority queue with the given initial capacity.
*
* @param initCapacity the initial capacity of this priority queue
*/
public MaxPQ(int initCapacity) {
pq = (Key[]) new Object[initCapacity + 1];
n = 0;
}
/**
* Initializes an empty priority queue.
*/
public MaxPQ() {
this(1);
}
/**
* Initializes an empty priority queue with the given initial capacity,
* using the given comparator.
*
* @param initCapacity the initial capacity of this priority queue
* @param comparator the order in which to compare the keys
*/
public MaxPQ(int initCapacity, Comparator<Key> comparator) {
this.comparator = comparator;
pq = (Key[]) new Object[initCapacity + 1];
n = 0;
}
/**
* Initializes an empty priority queue using the given comparator.
*
* @param comparator the order in which to compare the keys
*/
public MaxPQ(Comparator<Key> comparator) {
this(1, comparator);
}
/**
* Initializes a priority queue from the array of keys.
* Takes time proportional to the number of keys, using sink-based heap construction.
*
* @param keys the array of keys
*/
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();
}
/**
* Returns true if this priority queue is empty.
*
* @return {@code true} if this priority queue is empty;
* {@code false} otherwise
*/
public boolean isEmpty() {
return n == 0;
}
/**
* Returns the number of keys on this priority queue.
*
* @return the number of keys on this priority queue
*/
public int size() {
return n;
}
/**
* Returns a largest key on this priority queue.
*
* @return a largest key on this priority queue
* @throws NoSuchElementException if this priority queue is empty
*/
public Key max() {
if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
// resize the underlying array to have the given capacity
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;
}
/**
* Adds a new key to this priority queue.
*
* @param x the new key to add to this priority queue
*/
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();
}
/**
* Removes and returns a largest key on this priority queue.
*
* @return a largest key on this priority queue
* @throws NoSuchElementException if this priority queue is empty
*/
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 loitering and help with garbage collection
if ((n > 0) && (n == (pq.length - 1) / 4)) resize(pq.length / 2);
assert isMaxHeap();
return max;
}
/***************************************************************************
* Helper functions to restore the heap invariant.
***************************************************************************/
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;
}
}
/***************************************************************************
* Helper functions for compares and swaps.
***************************************************************************/
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() {
for (int i = 1; i <= n; i++) {
if (pq[i] == null) return false;
}
for (int i = n+1; i < pq.length; i++) {
if (pq[i] != null) return false;
}
if (pq[0] != null) return false;
return isMaxHeapOrdered(1);
}
// is subtree of pq[1..n] rooted at k a max heap?
private boolean isMaxHeapOrdered(int k) {
if (k > n) return true;
int left = 2*k;
int right = 2*k + 1;
if (left <= n && less(k, left)) return false;
if (right <= n && less(k, right)) return false;
return isMaxHeapOrdered(left) && isMaxHeapOrdered(right);
}
/***************************************************************************
* Iterator.
***************************************************************************/
/**
* Returns an iterator that iterates over the keys on this priority queue
* in descending order.
* The iterator doesn't implement {@code remove()} since it's optional.
*
* @return an iterator that iterates over the keys in descending order
*/
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();
}
}
/**
* Unit tests the {@code MaxPQ} data type.
*
* @param args the command-line arguments
*/
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)");
}
}
/******************************************************************************
* Copyright 2002-2020, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
使用优先队列的多项归并
这里需要重点理解一下,我复习这个优先队列的目的其实就是在做 LeetCode 的第 22 题时,那个题目需要用到这个数据结构,然后,这里的多项归并所用到的思想和解那一道题的思路是一模一样的。
/******************************************************************************
* Compilation: javac Multiway.java
* Execution: java Multiway input1.txt input2.txt input3.txt ...
* Dependencies: IndexMinPQ.java In.java StdOut.java
* Data files: https://algs4.cs.princeton.edu/24pq/m1.txt
* https://algs4.cs.princeton.edu/24pq/m2.txt
* https://algs4.cs.princeton.edu/24pq/m3.txt
*
* Merges together the sorted input stream given as command-line arguments
* into a single sorted output stream on standard output.
*
* % more m1.txt
* A B C F G I I Z
*
* % more m2.txt
* B D H P Q Q
*
* % more m3.txt
* A B E F J N
*
* % java Multiway m1.txt m2.txt m3.txt
* A A B B B C D E F F G H I I J N P Q Q Z
*
******************************************************************************/
package algs4;
/**
* The {@code Multiway} class provides a client for reading in several
* sorted text files and merging them together into a single sorted
* text stream.
* This implementation uses a {@link IndexMinPQ} to perform the multiway
* merge.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
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();
}
/**
* Reads sorted text files specified as command-line arguments;
* merges them together into a sorted output; and writes
* the results to standard output.
* Note: this client does not check that the input files are sorted.
*
* @param args the command-line arguments
*/
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);
}
}
/******************************************************************************
* Copyright 2002-2020, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
java algs4.Multiway .\m1.txt .\m2.txt .\m3.txt
这个程序的作用,用这里的
m1.txt
、m2.txt
、m3.txt
来辅助说明,就是每一次把每一个文件中的最左边的数据取出来,然后从中挑出最小的数据。之后再重复这个过程。最终输出的结果是所有文件中的所有数据得到有序的排列。
堆排序
/******************************************************************************
* Compilation: javac Heap.java
* Execution: java Heap < input.txt
* Dependencies: StdOut.java StdIn.java
* Data files: https://algs4.cs.princeton.edu/24pq/tiny.txt
* https://algs4.cs.princeton.edu/24pq/words3.txt
*
* Sorts a sequence of strings from standard input using heapsort.
*
* % more tiny.txt
* S O R T E X A M P L E
*
* % java Heap < tiny.txt
* A E E L M O P R S T X [ one string per line ]
*
* % more words3.txt
* bed bug dad yes zoo ... all bad yet
*
* % java Heap < words3.txt
* all bad bed bug dad ... yes yet zoo [ one string per line ]
*
******************************************************************************/
package algs4;
/**
* The {@code Heap} class provides a static method to sort an array
* using <em>heapsort</em>.
* <p>
* This implementation takes Θ(<em>n</em> log <em>n</em>) time
* to sort any array of length <em>n</em> (assuming comparisons
* take constant time). It makes at most
* 2 <em>n</em> log<sub>2</sub> <em>n</em> compares.
* <p>
* This sorting algorithm is not stable.
* It uses Θ(1) extra memory (not including the input array).
* <p>
* For additional documentation, see
* <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Heap {
// This class should not be instantiated.
private Heap() { }
/**
* Rearranges the array in ascending order, using the natural order.
* @param pq the array to be sorted
*/
public static void sort(Comparable[] pq) {
int n = pq.length;
// heapify phase
for (int k = n/2; k >= 1; k--)
sink(pq, k, n);
// sortdown phase
int k = n;
while (k > 1) {
exch(pq, 1, k--);
sink(pq, 1, k);
}
}
/***************************************************************************
* Helper functions to restore the heap invariant.
***************************************************************************/
private static void sink(Comparable[] pq, int k, int n) {
while (2*k <= n) {
int j = 2*k;
if (j < n && less(pq, j, j+1)) j++;
if (!less(pq, k, j)) break;
exch(pq, k, j);
k = j;
}
}
/***************************************************************************
* Helper functions for comparisons and swaps.
* Indices are "off-by-one" to support 1-based indexing.
***************************************************************************/
private static boolean less(Comparable[] pq, int i, int j) {
return pq[i-1].compareTo(pq[j-1]) < 0;
}
private static void exch(Object[] pq, int i, int j) {
Object swap = pq[i-1];
pq[i-1] = pq[j-1];
pq[j-1] = swap;
}
// print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
StdOut.println(a[i]);
}
}
/**
* Reads in a sequence of strings from standard input; heapsorts them;
* and prints them to standard output in ascending order.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Heap.sort(a);
show(a);
}
}
/******************************************************************************
* Copyright 2002-2020, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
优先队列(Priority Queues)
http://fanyfull.github.io/2021/12/06/优先队列-Priority-Queues/