Chapter 9 Priority Queues and Sets

Slides:



Advertisements
Similar presentations
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Heaps CS 308 – Data Structures.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CS 201 Data Structures and Algorithms
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
Efficiency of in Binary Trees
C++ Plus Data Structures
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Lecture 22 Chapter 9 Sets.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 5 ADTs Stack and Queue.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
C++ Plus Data Structures
Binary Heaps Priority Queues
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Priority Queues.
CSCE 3110 Data Structures and Algorithm Analysis
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CSCE 3110 Data Structures and Algorithm Analysis
Queues Jyh-Shing Roger Jang (張智星)
CSCE 3110 Data Structures and Algorithm Analysis
Heaps and Priority Queues
Priority Queues.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Sets CS3240, L. grewe.
Presentation transcript:

Chapter 9 Priority Queues and Sets

Real-life Priority Queue

Priority Queue A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time. Queue Enque an item Item returned has been in the queue the longest amount of time. Priority Queue Enque a pair <item, priority> Item returned has the highest priority.

ADT Priority Queue Operations Transformers MakeEmpty Enqueue Dequeue Observers IsEmpty IsFull change state observe state

Implementation Level An unsorted List An Array-Based Sorted List dequeuing would require searching through the entire list An Array-Based Sorted List Enqueuing is expensive A Reference-Based Sorted List Enqueuing again is 0(N) A Binary Search Tree On average, 0(log2N) steps for both enqueue and dequeue A Heap guarantees 0(log2N) steps, even in the worst case

Class PQType Declaration class FullPQ(){}; class EmptyPQ(){}; template<class ItemType> class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType<ItemType> items; int maxItems; };

Class PQType Function Definitions template<class ItemType> PQType<ItemType>::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } void PQType<ItemType>::MakeEmpty() PQType<ItemType>::~PQType() delete [] items.elements;

Class PQType Function Definitions Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1) Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)

Dequeue

Dequeue (cont.) c) ReheapDown

Code for Dequeue template<class ItemType> void PQType<ItemType>::Dequeue(ItemType& item) { if (length == 0) throw EmptyPQ(); else item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }

Enqueue

Code for Enqueue template<class ItemType> void PQType<ItemType>::Enqueue(ItemType newItem) { if (length == maxItems) throw FullPQ(); else length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }

Comparison of Priority Queue Implementations   Enqueue Dequeue Heap O(log2N) O(log2N) Linked List O(N) O(N) Binary Search Tree     Balanced O(log2N) O(log2N)   Skewed O(N) O(N)  

ADT Set Definitions Base type: The type of the items in the set Cardinality: The number of items in a set Cardinality of the base type: The number of items in the base type Union of two sets: A set made up of all the items in either sets Intersection of two sets: A set made up of all the items in both sets Difference of two sets: A set made up of all the items in the first set that are not in the second set

Beware: At the Logical Level Sets can not contain duplicates. Storing an item that is already in the set does not change the set.   If an item is not in a set, deleting that item from the set does not change the set. Sets are not ordered.

Implementing Sets Explicit implementation (Bit vector) Each item in the base type has a representation in each instance of a set. The representation is either true (item is in the set) or false (item is not in the set).   Space is proportional to the cardinality of the base type. Algorithms use Boolean operations.

Implementing Sets (cont.) Implicit implementation (List) The items in an instance of a set are on a list that represents the set. Those items that are not on the list are not in the set.   Space is proportional to the cardinality of the set instance. Algorithms use ADT List operations.

Explain: If sets are not ordered, why is the SortedList ADT a better choice as the implementation structure for the implicit representation?