Data Structures and Algorithm Analysis Priority Queues (Heaps)

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
BST Data Structure A BST node contains: A BST contains
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queues (Heaps)
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Priority Queues A priority queue is an ADT where:
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority.
AA Trees.
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
AVL TREES.
Multiway Search Trees Data may not fit into main memory
Binary Search Tree (BST)
Programming Abstractions
Hashing Exercises.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps, Heap Sort, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queue & Heap CSCI 3110 Nan Chen.
Heapsort Heap & Priority Queue.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
i206: Lecture 14: Heaps, Graphs intro.
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
A Kind of Binary Tree Usually Stored in an Array
CSE332: Data Abstractions Lecture 4: Priority Queues
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
A Robust Data Structure
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Priority Queues & Heaps
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Important Problem Types and Fundamental Data Structures
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
B-Trees.
Heaps & Multi-way Search Trees
Data Structures Using C++ 2E
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Data Structures and Algorithm Analysis Priority Queues (Heaps) Lecturer: Ligang Dong Email: donglg@zjgsu.edu.cn Tel: 28877721,13306517055 Office: SIEE Building 305

General Idea Example: Suppose there are a sequence of jobs are sent to a printer. Although jobs sent to a printer are generally placed on a queue, this might not always be the best thing to do. For instance, if, when the printer becomes available, there are several 1-page jobs and one 100-page job, it might be reasonable to make the long job go last, even if it is not the last job submitted. This particular application seems to require a special kind of queue, known as a priority queue.

Model A priority queue is a data structure that allows at least the following two operations: (1) Insert: is the equivalent of Enqueue (2) DeleteMin: finds, returns, and removes the minimum element in the priority queue. Insert(H) DeleteMin(H) Priority Queue H

Simple Implementations There are several obvious ways to implement a priority queue. We could use a simple linked list, performing insertions at the front and traversing the list to delete the minimum. Alternatively, we could insist that the list be kept always sorted. Another way of implementing priority queues would be to use a binary search tree. Recall that the only element we ever delete is the minimum. Repeatedly removing a node that is in the left subtree would seem to hurt the balance of the tree by making the right subtree heavy.

Binary Heap The implementation we will use is known as a binary heap. Like binary search trees, binary heaps have two properties, namely, a structure property and a heap order property.

Structure Property Structure property: A heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Such a tree is known as a complete binary tree. A B C D E F G H I J

Structure Property An important observation is that because a complete binary tree is so regular, it can be represented in an array and no pointers are necessary. For any element in array position i, the left child is in position 2i, the right child is in the cell after that left child (2i+1), and the parent is in position i/2.

Structure Property A B C D E F G H I J A B C D E F G H I J 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 11 12 13

Structure Property Thus, not only are pointers not required, but the operations required to traverse the tree are extremely simple and likely to be very fast on most computers. The only problem with this implementation is that an estimate of the maximum heap size is required in advance. A heap data structure will then consist of an array (of whatever type the key is) and an integer representing the maximum and current heap sizes. Figure 6.4 shows a typical priority queue declaration.

Structure Property Struct HeapStruct { int Capacity; int Size; elementType *Elements; } Initialize(int MaxElements) { Line 3: H=malloc(sizeof(struct HeapStruct)); Line 6: H->Elements= malloc((MaxElements+1)*sizeof(ElementType)); }

Heap Order Property The property that allows operations to be performed quickly is the heap order property. Since we want to be able to find the minimum quickly, it makes sense that the smallest element should be at the root. If we consider that any subtree should also be a heap, then any node should be smaller than all of its descendants.

Heap Order Property Applying this logic, we arrive at the heap order property: In a heap, 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 is has no parent). Analogously, we can declare a (max) heap, which enables us to efficiently find and remove the maximum element, by changing the heap order property. Thus, a priority queue can be used to find either a minimum or a maximum, but this needs to be decided ahead of time.

Basic Heap Operations It is easy (both conceptually and practically) to perform the two required operations, namely Insert and DeleteMin. All the work involves ensuring that the heap order property is maintained.

Basic Heap Operations Insert: To insert an element X into the heap, we create a hole in the next available location, since otherwise the tree will not be complete. If X can be placed in the hole without violating heap order, then we do so and are done. Otherwise we slide the element that is in the hole’s parent node into the hole, thus bubbling the hole up towards the root. We continue this process until X can be placed in the hole.

Basic Heap Operations Example: 13 21 16 Insert 14 24 31 19 68 65 26 32 Original Tree

Basic Heap Operations 13 13 21 16 21 16 24 31 19 24 31 19 68 68 65 26 32 65 26 32

Basic Heap Operations 13 13 21 16 21 16 24 31 19 24 19 68 68 65 26 32 65 26 32 31

Basic Heap Operations 13 13 21 16 16 24 19 24 21 19 68 68 65 26 32 31 65 26 32 31

Basic Heap Operations 13 13 16 14 16 24 21 19 24 21 19 68 68 65 26 32 65 26 31 32 31

Basic Heap Operations DeleteMin: DeleteMins are handled in a similar manner as insertions. Finding the minimum is easy; the hard part is removing it. When the minimum is removed, a hole is created at the root. Since the heap now becomes one smaller, it follows that the last element X in the heap must move some where in the heap.

Basic Heap Operations If X can be placed in the hole, then we are done. This is unlikely, so we slide the smaller of the hole’s children into the hole, thus pushing the hole down one level. We repeat this step until X can be placed in the hole. Thus, our action is to place X in its correct spot along a path from the root containing minimum children.

Basic Heap Operations 13 Example: Delete the minimum key 14 16 19 21 68 65 26 32 31 Original Tree

Basic Heap Operations 13 14 16 14 16 19 21 19 68 19 21 19 68 65 26 32 31 65 26 32 31

Basic Heap Operations 14 14 16 16 19 21 19 68 19 21 19 68 65 26 32 31 65 26 32 31

Basic Heap Operations 14 14 16 19 16 19 21 19 68 21 19 68 65 26 32 31 65 26 32 31

Basic Heap Operations 14 14 19 16 19 16 21 19 68 26 21 19 68 65 26 32 31 65 32 31

Basic Heap Operations 14 14 19 16 19 16 26 21 19 68 26 21 19 68 65 32 31 65 31 32

d-Heap Binary heaps are so simple that they are almost always used when priority queues are needed. A simple generalization is a d-heap, which is exactly like a binary heap except that all nodes have d children (thus, a binary heap is a 2-heap).

d-Heap 1 2 3 5 4 7 10 13 15 6 8 17 9 11 9 An example of 3-heap

Homework #7 7.1 Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time, into an initially empty binary heap. 7.2 Show the result of performing three delete_min operations in the heap of the previous exercise. 7.3 If a d-heap is stored as an array, for an entry located in position i, where are the parents and children?