Containers Priority Queues Jordi Cortadella and Jordi Petit

  • Slides: 19
Download presentation
Containers: Priority Queues Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Priority Queues Jordi Cortadella and Jordi Petit Department of Computer Science

A priority queue • Containers: Priority Queues © Dept. CS, UPC 2

A priority queue • Containers: Priority Queues © Dept. CS, UPC 2

Binary Heap 13 21 16 24 65 0 31 26 19 33 1 2

Binary Heap 13 21 16 24 65 0 31 26 19 33 1 2 3 4 5 6 7 8 9 10 13 21 16 24 31 19 68 65 26 33 Containers: Priority Queues 68 © Dept. CS, UPC 11 12 13 14 15 3

Binary Heap 13 21 16 24 65 31 26 19 68 33 Locations in

Binary Heap 13 21 16 24 65 31 26 19 68 33 Locations in the vector: Heap-Order Property: the key of the parent of X is smaller than (or equal to) the key in X. 0 1 2 3 4 5 6 7 8 9 10 13 21 16 24 31 19 68 65 26 33 Containers: Priority Queues © Dept. CS, UPC 11 12 13 14 15 4

Binary Heap 13 21 16 24 65 31 26 19 68 33 Two main

Binary Heap 13 21 16 24 65 31 26 19 68 33 Two main operations on a binary heap: • Insert a new element • Remove the min element Both operations must preserve the properties of the binary heap: • Completeness • Heap-Order property Containers: Priority Queues © Dept. CS, UPC 5

Binary Heap: insert 14 13 21 16 24 65 31 26 33 19 68

Binary Heap: insert 14 13 21 16 24 65 31 26 33 19 68 Insert in the last location 14 13 21 16 24 65 14 26 33 19 68 … and bubble up … 31 13 14 16 24 65 21 26 Containers: Priority Queues 33 19 68 done ! 31 © Dept. CS, UPC 6

Binary Heap: remove min 13 13 14 16 24 65 21 26 33 19

Binary Heap: remove min 13 13 14 16 24 65 21 26 33 19 68 31 Extract the min element and move the last one to the root of the heap 31 14 16 24 65 21 26 19 68 … and bubble down … 33 14 31 16 24 65 21 26 Containers: Priority Queues 19 68 33 © Dept. CS, UPC 7

Binary Heap: remove min 14 31 16 24 65 21 26 19 68 33

Binary Heap: remove min 14 31 16 24 65 21 26 19 68 33 14 21 16 24 65 31 26 Containers: Priority Queues 19 done ! 68 33 © Dept. CS, UPC 8

Binary Heap: complexity • Containers: Priority Queues © Dept. CS, UPC 9

Binary Heap: complexity • Containers: Priority Queues © Dept. CS, UPC 9

Binary Heap: other operations • Containers: Priority Queues © Dept. CS, UPC 10

Binary Heap: other operations • Containers: Priority Queues © Dept. CS, UPC 10

Building a heap from a set of elements • Containers: Priority Queues © Dept.

Building a heap from a set of elements • Containers: Priority Queues © Dept. CS, UPC 11

Building a heap from a set of elements bubble down 15 8 4 3

Building a heap from a set of elements bubble down 15 8 4 3 10 1 2 9 7 6 5 15 8 11 12 14 4 3 13 10 1 2 9 7 6 8 10 8 4 1 2 9 5 6 7 3 11 12 14 13 10 1 2 4 1 3 9 9 5 6 7 11 12 14 2 13 10 1 3 4 6 3 9 9 5 Containers: Priority Queues 13 6 7 11 12 2 5 8 14 14 13 1 1 10 12 4 15 2 7 11 8 5 6 13 15 8 10 14 4 15 2 12 15 15 3 5 11 7 11 12 14 4 3 13 10 © Dept. CS, UPC 6 15 9 5 8 7 11 12 14 13 12

Building a heap: implementation // Constructor from a collection of items Binary. Heap(const vector<Elem>&

Building a heap: implementation // Constructor from a collection of items Binary. Heap(const vector<Elem>& items) { v. push_back(Elem()); // v is the vector holding the elements for (auto& e: items) v. push_back(e); for (int i = size()/2; i > 0; --i) bubble_down(i); } v: 3 2 1 1 1 0 0 0 0 Subtract the two equations: A heap can be built from a collection of items in linear time. Containers: Priority Queues © Dept. CS, UPC 13

Heap sort template <typename T> void Heap. Sort(vector<T>& v) { Binary. Heap<T> heap(v); for

Heap sort template <typename T> void Heap. Sort(vector<T>& v) { Binary. Heap<T> heap(v); for (T& e: v) e = heap. remove_min(); } Containers: Priority Queues © Dept. CS, UPC 14

EXERCISES Containers: Priority Queues © Dept. CS, UPC 15

EXERCISES Containers: Priority Queues © Dept. CS, UPC 15

Exercise: insert/remove element Given the binary heap implemented in the following vector, draw the

Exercise: insert/remove element Given the binary heap implemented in the following vector, draw the tree represented by the vector. 6 7 9 10 11 12 13 15 19 14 21 17 16 Execute the following sequence of operations insert(8); remove_min(); insert(6); insert(18); remove_min(); and draw the tree after the execution of each operation. Containers: Priority Queues © Dept. CS, UPC 16

 • 3 7 7 10 8 Containers: Priority Queues 10 15 18 25

• 3 7 7 10 8 Containers: Priority Queues 10 15 18 25 13 20 17 22 19 © Dept. CS, UPC 17

 • Containers: Priority Queues © Dept. CS, UPC 18

• Containers: Priority Queues © Dept. CS, UPC 18

Exercise: bubble-up/down Consider the following declaration for a Binary Heap: template <typename T> //

Exercise: bubble-up/down Consider the following declaration for a Binary Heap: template <typename T> // T must be a comparable type class Binary. Heap { private: vector<Elem> v; // Table for the heap (location 0 not used) // Bubbles up the element at location i void bubble_up(int i); // Bubbles down the element at location i void bubble_down(int i); Give an implementation for the methods bubble_up and bubble_down. Containers: Priority Queues © Dept. CS, UPC 19