Priority Queues Antrian Berprioritas Priority Queues 1 PQ
Priority Queues (Antrian Berprioritas) Priority Queues 1
PQ Two kinds of priority queues: • Min priority queue. • Max priority queue.
Min Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations: – Empty – Size – insert an element into the priority queue (push) – get element with min priority (top) – remove element with min priority (pop)
Max Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations: – Empty – Size – insert an element into the priority queue (push) – get element with max priority (top) – remove element with max priority (pop)
Applications Sorting • use element key as priority • insert elements to be sorted into a priority queue • remove/pop elements in priority order – if a min priority queue is used, elements are extracted in ascending order of priority (or key) – if a max priority queue is used, elements are extracted in descending order of priority (or key)
Sorting Example • Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. – Insert the five elements into a max priority queue. – Do five remove max operations placing removed elements into the sorted array from right to left.
After Inserting Into Max Priority Queue
After First Remove Max Operation
Min Tree Definition • Each tree node has a value. • Value in any node is the minimum value in the subtree for which that node is the root. • Equivalently, no descendent has a smaller value.
Priority Queue ADT (§ 7. 1. 3) • A priority queue stores a collection of entries • Each entry is a pair (key, value) • Main methods of the Priority Queue ADT • Additional methods – min() returns, but does not remove, an entry with smallest key – size(), is. Empty() – insert(k, x) inserts an entry with key k and value x – remove. Min() removes and returns the entry with smallest key • Applications: Priority Queues – Standby flyers – Auctions – Stock market 10
Min Tree Example
Min Heap Definition • complete binary tree • min tree
A Heap Is Efficiently Represented As An Array
Interface IBT public interface IBT { public boolean is. Empty(); public boolean is. Full(); public void sisip. Data(int dt); public int hapus. Data(); }
Max. Heap. Tree public class Binary. Max. Heap implements IBT{ } private static int max. Size = 100; private int counter; private int []data; public Binary. Max. Heap(){} public boolean is. Full(){return counter == max. Size; } public boolean is. Empty(){return counter == 0; } public void sisip. Data(int value) {//} private void sift. Up(int id) { //} public int hapus. Data() { //} private void sift. Down(int id) { // } public void cetak(){ // }
Binary. Max. Heap public Binary. Max. Heap(){ counter = 0; data = new int[max. Size]; }
is. Full() dan is. Empty() public boolean is. Full(){ return counter == max. Size; } public boolean is. Empty(){ return counter == 0; }
sisip. Data(int value) public void sisip. Data(int value) { if (!is. Full()){ data[++counter] = value; sift. Up(counter); } }
private void sift. Up(int id) { int id. Induk, tmp; if (id != 0) { id. Induk = id/2; if (id. Induk != 0){ if (data[id. Induk] < data[id]) { tmp = data[id. Induk]; data[id. Induk] = data[id]; data[id] = tmp; sift. Up(id. Induk); } }
hapus. Data() public int hapus. Data() { int item. Hapus = -999; if (!is. Empty()){ item. Hapus = data[1]; data[1] = data[counter--]; if (counter > 1) sift. Down(1); } return item. Hapus; }
private void sift. Down(int id) { int id. Anak. Kiri, id. Anak. Kanan, id. Max, tmp; id. Anak. Kiri = id*2; id. Anak. Kanan = id*2 + 1; if (id. Anak. Kanan > counter) { if (id. Anak. Kiri > counter) return; else id. Max = id. Anak. Kiri; } else { if (data[id. Anak. Kiri] >= data[id. Anak. Kanan]) id. Max = id. Anak. Kiri; else id. Max = id. Anak. Kanan; } //swap data if (data[id] < data[id. Max]) { tmp = data[id. Max]; data[id. Max] = data[id]; data[id] = tmp; sift. Down(id. Max); } }
public void cetak(){ for (int i=1; i<counter+1; i++){ System. out. print(data[i]+" "); } }
public static void main(String[] args) { } Binary. Max. Heap bmax = new Binary. Max. Heap(); bmax. sisip. Data(9); bmax. sisip. Data(3); bmax. sisip. Data(10); bmax. sisip. Data(42); bmax. sisip. Data(9); bmax. sisip. Data(70); bmax. sisip. Data(23); bmax. sisip. Data(11); bmax. sisip. Data(7); bmax. sisip. Data(8); bmax. sisip. Data(13); bmax. sisip. Data(900); bmax. cetak(); System. out. println(); while (!bmax. is. Empty()){ System. out. print(bmax. hapus. Data()+" "); }
Total Order Relations (§ 7. 1. 1) • Keys in a priority • Mathematical concept of queue can be total order relation arbitrary objects on – Reflexive property: x x which an order is – Antisymmetric property: defined x y y x x=y • Two distinct entries – Transitive property: in a priority queue x y y z x z can have the same key Priority Queues 25
- Slides: 25