1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Priority Queues. 2 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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
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.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
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.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Chapter 21 Binary Heap.
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.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
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.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
2 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)
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.
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)
CS 201 Data Structures and Algorithms
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
7/23/2009 Many thanks to David Sun for some of the included slides!
CSCI2100 Data Structures Tutorial 7
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Priority Queues (Heaps)
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queue and Heap
Priority Queues Binary Heaps
Priority Queues (Heaps)
Presentation transcript:

1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues

2 6.2 Simple Implementations Simple linked list –insert in front O (1) –delete minimum O (N) Sorted linked list –insert O (N) –delete minimum O (1)

3 6.2 Simple Implementations Binary search tree – O (log N) for both operations Binary heap – worst case time O (log N) – building a priority queue in linear time

4 6.3 Binary Heap Sometimes just called heap A binary heap tree that is completely filled, except at the bottom level, which is filled from left to right. A complete binary tree of height h has between 2 h and 2 h nodes The height of a complete binary tree is floor (log N).

5 6.3 Binary Heap A complete binary tree can be represented in an array and no pointers are necessary.

6 6.3 Binary Heap The root is at position 1 (reserve position 0 for a sentinel - MinData). For an element at position i, its left child is at position 2i and its it right child at 2i +1; its parent is at floor (i/2). Heap order property –The value at any node should be smaller than all of its descendants.

7 6.3 Binary Heap struct HeapStruct { int Capacity; /* maximum size */ int Size; /* actual size */ ElementType *Elements; }; typedef struct HeapStruct *PriorityQueue; Definitions

8 6.3 Binary Heap PriorityQueue Initialize (int MaxElements); void Destroy (PriorityQueue H); void MakeEmpty (PriorityQueue H); void Insert (ElementType X, PriorityQueue H); ElementType DeleteMin (PriorityQueue H); ElementType FindMin (PriorityQueue H); int IsEmpty (PriorityQueue H); int IsFull (PriorityQueue H); Some functions

9 6.3 Binary Heap - Initialize /* Fig 6.4 */ PriorityQueue Initialize (int MaxElements) { PriorityQueue H; if (MaxElements < MinPQSize) Error ("Priority queue size is too small"); H = malloc (sizeof (struct HeapStruct)); if (H == NULL) FatalError ("Out of space!!!");

Binary Heap - Initialize /* Allocate the array plus one extra for sentinel */ H->Elements = malloc ((MaxElements + 1) * sizeof (ElementType)); if (H->Elements == NULL) FatalError ("Out of space!!!"); H->Capacity = MaxElements; H->Size = 0; H->Elements [0] = MinData; return H; }

Binary Heap - Insert To insert an element X, create a hole in the next available location. If X can be placed in the hole without violating heap order, insertion is complete. Otherwise slide the element that is in the hole’s parent node into the hole, i.e., bubbling the hole up toward the root.

Binary Heap - Initialize Continue this process until X can be placed in the hole (a percolating up process). Worst case running time is O (log N), the new element is percolating up all the way to the root.

Binary Heap - Insert

Binary Heap - Insert

Binary Heap - Insert /* Fig 6.8 */ /* H->Element[ 0 ] is a sentinel */ void Insert (ElementType X, PriorityQueue H) { int i; if (IsFull (H)) { Error ("Priority queue is full"); return; }

Binary Heap - Insert for (i = ++H->Size; H->Elements [i / 2] > X; i /= 2 ) H->Elements [i] = H->Elements [i / 2]; H->Elements [i] = X; }

Binary Heap - DeleteMin The element at the root (position 1) is to be removed, and a hole is created. Place the last element X in the hole. If X is smaller than the child(ren), job is done. Otherwise slide the smaller of the hole’s children into the hole, thus pushing the hole down one level.

Binary Heap - DeleteMin Repeat the previous step until X can be placed in the hole (percolating down). Some node may have only one child. Worst case running time is O (log N). On average, the element that is placed at the root is percolated almost to the bottom of the heap, so the average running time is O (log N).

Binary Heap - DeleteMin

Binary Heap - DeleteMin

Binary Heap - DeleteMin

Binary Heap - DeleteMin /* Fig */ ElementType DeleteMin (PriorityQueue H) { int i, Child; ElementType MinElement, LastElement; if (IsEmpty (H)) { Error ("Priority queue is empty"); return H->Elements [0]; }

Binary Heap - DeleteMin MinElement = H->Elements [1]; LastElement = H->Elements [H->Size--]; for (i = 1; i * 2 Size; i = Child) { /* Find smaller child */ Child = i * 2; if (Child != H->Size && H->Elements [Child + 1] Elements [Child]) Child++;

Binary Heap - DeleteMin /* Percolate one level */ if (LastElement > H->Elements [Child]) H->Elements [i] = H->Elements [Child]; else break; } H->Elements [i] = LastElement; return MinElement; }