Algorithms and Data Structures

Similar documents
3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSCI2100B Data Structures Heaps

Priority Queues (Heaps)

Priority Queues (Heaps)

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

CS350: Data Structures Heaps and Priority Queues

Data Structures Lesson 9

Priority Queues Heaps Heapsort

CMSC 341 Lecture 14: Priority Queues, Heaps

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CE 221 Data Structures and Algorithms

Sorting and Searching

Heaps, Heap Sort, and Priority Queues.

Sorting and Searching

Heap Model. specialized queue required heap (priority queue) provides at least

Data Structures and Algorithms

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority Queues and Binary Heaps

Priority Queues Heaps Heapsort

UNIT III BALANCED SEARCH TREES AND INDEXING

The priority is indicated by a number, the lower the number - the higher the priority.

9. Heap : Priority Queue

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Binary Heaps. CSE 373 Data Structures Lecture 11

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Programming II (CS300)

Programming II (CS300)

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Module 2: Priority Queues

Module 2: Priority Queues

Figure 1: A complete binary tree.

Recall: Properties of B-Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Priority Queues. Binary Heaps

Priority queues. Priority queues. Priority queue operations

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COS 226 Fall2007 HW03 ( pts.) Due :00 p.m. Name:

CS 240 Data Structures and Data Management. Module 2: Priority Queues

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

COMP : Trees. COMP20012 Trees 219

CS165: Priority Queues, Heaps

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Figure 1: A complete binary tree.

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Module 2: Priority Queues

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

PRIORITY QUEUES AND HEAPS

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

CSE 214 Computer Science II Heaps and Priority Queues

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

PRIORITY QUEUES AND HEAPS

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Name CPTR246 Spring '17 (100 total points) Exam 3

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

BM267 - Introduction to Data Structures

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Priority Queues. Chapter 9

HEAPS & PRIORITY QUEUES

PRIORITY QUEUES AND HEAPS

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

4. Sorting and Order-Statistics

COMP Data Structures

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CH 8. HEAPS AND PRIORITY QUEUES

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Tables, Priority Queues, Heaps

CH. 8 PRIORITY QUEUES AND HEAPS

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CSE 373: Data Structures and Algorithms

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

CMSC 341. Binary Heaps. Priority Queues

CS301 - Data Structures Glossary By

Binary heaps (chapters ) Leftist heaps

Heaps and Priority Queues

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Lecture 13: AVL Trees and Binary Heaps

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

CompSci 201 Tree Traversals & Heaps

CSE 2123: Collections: Priority Queues. Jeremy Morris

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures Question Bank Multiple Choice

Priority Queues and Heaps. Heaps of fun, for everyone!

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Transcription:

Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1

6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO. ffl ADT Queue : FIFO. ADT Priority Queue : peek the element with the lowest (or ffl highest) priority. Malek Mouhoub, CS3620 Fall 2002 2

Priority Queues ffl 6.1 Model. ffl 6.2 Implementation. ffl 6.3 Binary Heap. Malek Mouhoub, CS3620 Fall 2002 3

6.1 Model 2 operations : ffl insert : add an element to the priority queue. ffl deletemin : find, return and remove the minimum element. Malek Mouhoub, CS3620 Fall 2002 4

ffl insert at the front of the list : O(1) ffl deletemin requires traversing the list : O(N ) Priority Queues 6.2 Implementation Λ 1st case : use a linked list Malek Mouhoub, CS3620 Fall 2002 5

ffl insertion requires O(N ) Priority Queues Λ 2nd case : use a sorted linked list ffl but only O(1) for deletemin. Malek Mouhoub, CS3620 Fall 2002 6

± Ξ Π Priority Queues 3rd case : use a binary search tree ffl O(log N ) average running time for both insertion and deletion. Elements are deleted at each time from the left subtree which ffl affects the balance of the tree. The binary search tree supports many operations that are not ffl required by priority queue. Malek Mouhoub, CS3620 Fall 2002 7

6.3 Binary Heap A binary heap is a complete binary tree : a tree that is completely filled, ffl with possible exception of the bottom level, which is filled from left to right. Can easily be represented by an array (and an integer representing the ffl current heap size) instead of a linked list. For any element in array position i : ffl the left child is in position 2i, the right child is in position 2i + 1, and the parent is in position [i=2]. The operations required to traverse the tree are very simple and are ffl performed quickly (because of the heap-order property). Malek Mouhoub, CS3620 Fall 2002 8

I Priority Queues A B C D E F G H J A B C D E F G H I J 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Malek Mouhoub, CS3620 Fall 2002 9

Heap-Order Property ffl The smallest element should be at the root. Any node should be smaller than all of its descendants (any ffl subtree should also be a heap). Heap-order property : ffl For every node X, the key in the parent of X is smaller than (or equal to) the key in X, with the exception of the root (which has no parent). Malek Mouhoub, CS3620 Fall 2002 10

13 13 21 16 21 16 24 31 19 68 6 31 19 68 65 26 32 65 26 32 Two complete trees (only the left is a heap) Malek Mouhoub, CS3620 Fall 2002 11

Basic Heap Operations Λ Insert 1. Create a hole in the next available location. 2. Place the element to be inserted in the hole if it does not violate the heap order. 3. Otherwise slide the element that is in the hole s parent node into the hole, thus bubbling the hole up toward the root. Continue the process until the element can be placed in the hole. Malek Mouhoub, CS3620 Fall 2002 12

13 21 16 bubbling the hole up 13 21 16 24 31 19 68 6 19 68 65 26 32 65 26 32 Insert 14 31 Malek Mouhoub, CS3620 Fall 2002 13

13 13 16 14 16 6 21 19 68 6 21 19 68 65 26 32 31 65 26 32 31 Insert 14 Malek Mouhoub, CS3620 Fall 2002 14

Insert /* Inserting item into the priority queue, maintaining the heap order. Duplicates are not allowed */ template <Class Comparable> void BinaryHeap<Comparable>::insert(const Comparable & x) if (isfull()) f throw Overflow(); // Percolate up int hole = ++currentsize; for ( ;hole > 1 && x< array[hole/2]; hole /= 2) array[hole] = array[hole/2]; 10 array[hole] = g x; Malek Mouhoub, CS3620 Fall 2002 15

deletemin 1. Create a hole at the root. 2. The last element X in the heap must move somewhere in the heap. 3. Slide the smaller of the hole s children into the hole, thus pushing the hole down one level. Repeat this step until X can be placed in the hole. Malek Mouhoub, CS3620 Fall 2002 16

13 14 16 deletemin 14 16 19 21 19 68 19 21 19 68 65 26 32 31 65 26 32 31 Creation of the hole at the root Malek Mouhoub, CS3620 Fall 2002 17

14 14 16 19 16 19 21 19 68 21 19 68 26 32 31 65 26 32 31 Malek Mouhoub, CS3620 Fall 2002 18

14 14 19 16 19 16 26 21 19 68 26 21 19 68 32 31 65 31 32 Malek Mouhoub, CS3620 Fall 2002 19

Other heap operations ffl A heap has very little ordering information, so there is no way to find any particular element without a linear scan through the entire heap. ffl Assuming that the position of every element is known by some other method, the following operations run in logarithmic worst-case time. decreasekey(p, ): lowers the value of the item at position p by a positive amount. Apercolate up is required sometimes to maintain the heap order. increasekey(p; ): increases the value of the item at position p by a positive amount. Apercolate down is required sometimes to maintain the heap order. remove : removes the node at position p from the heap. This is done by performing decreasekey(p;1) and then performing deletemin(). Malek Mouhoub, CS3620 Fall 2002 20

± Ξ Π Priority Queues Other heap operations Buildheap ffl Takes as input N items and places them into an empty heap. ffl N successive inserts ) O(N ) average case but O(N log N ) worst-case. Malek Mouhoub, CS3620 Fall 2002 21

± Ξ Π Priority Queues Other heap operations Buildheap /** Establish heap order property from an arbitrary arrangement of items. Runs in linear time. */ template <class Comparable> void BinaryHeap<Comparable>::buildHeap() f for (int i = currentsize / 2; i > 0; i ) perlocatedown(i); g Malek Mouhoub, CS3620 Fall 2002 22

Applications of Priority Queues Λ The selection Problem ffl Given a list of N elements, which can be totally ordered, and an integer k, find the kth largest element. ffl Algorithm 1A : read the elements into an array and sort them, returning the appropriate element. ) assuming a simple sorting algorithm, the running time is O(N 2 ). ffl Algorithm 1B : 1. read k elements into an array and sort them. The smallest of these is in the kth position. 2. Process the remaining elements one by one. As an element arrives, it is compared with the kth element in the array. If it is larger, then the kth element is removed, and the new element is placed in the correct place among the remaining k 1 elements. ) O(N _ k) running time. Why? ffl If k = N=2 then both algorithms are O(N 2 ). k is known as the median in this case. ffl The following algorithms run in O(N log N ) in the extreme case of k = N=2. Malek Mouhoub, CS3620 Fall 2002 23

Algorithm 6A ffl Algorithm for finding the kth smallest element 1. Read N elements into an array. 2. Apply the buildheap algorithm to this array. 3. Perform k deletemin operations. The last element extracted from the heap is the answer. ffl Complexity : O(N + k log N ) in the worst case. k = O(N= log N ) ) O(N ) k = N=2 ) N (N log N ) For large values of k : O(k log N ) k = N ) O(N log N ) (Idea of the heapsort). ffl By changing the heap-order property, we will solve the problem of finding the kth largest element. Malek Mouhoub, CS3620 Fall 2002 24

Algorithm 6B ffl Find the kth largest element 1. Same idea as algorithm 1B. 2. At any point in time, maintain a set S of the k largest elements. 3. After the first k elements are read, when a new element is read it is compared with the kth largest element, which we denote by S k (S k is the smallest element in S). If the new element is larger, then it replaces S k in S. 4. At the end of the input, we find the smallest element in S and return it as the answer. ffl O(k + (N k) log k) = O(N log k) in the worst case. Why? Malek Mouhoub, CS3620 Fall 2002 25