Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4, 20091.

Slides:



Advertisements
Similar presentations
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
Winner trees. Loser Trees.
Data Structure Dr. Mohamed Khafagy.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
CS 315 Lecture 15 March 31 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Priority Queues, Heaps & Leftist Trees
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
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.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
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.
SNU IDB Lab. Ch.13 Priority Queues © copyright 2006 SNU IDB Lab.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Heaps & Priority Queues
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Leftist Trees Linked binary tree.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues An abstract data type (ADT) Similar to a queue
Priority Queues and Heaps
The Greedy Method and Text Compression
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Initializing A Max Heap
Ch 6: Heapsort Ming-Te Chi
Tree Representation Heap.
Heaps A heap is a binary tree.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Priority Queues An abstract data type (ADT) Similar to a queue
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
Algorithms: Design and Analysis
การวิเคราะห์และออกแบบขั้นตอนวิธี
Heaps & Multi-way Search Trees
A Heap Is Efficiently Represented As An Array
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4, 20091

Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations:  isEmpty  size  add/put an element into the priority queue  get element with min priority  remove element with min priority Nov 4, 20092

Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations:  isEmpty  size  add/put an element into the priority queue  get element with max priority  remove element with max priority Nov 4, 20093

Complexity Of Operations Two good implementations are heaps and leftist trees. isEmpty, size, and get => O(1) time put and remove => O(log n) time where n is the size of the priority queue Nov 4, 20094

Applications Sorting use element key as priority put elements to be sorted into a priority queue extract elements in priority order  if a min priority queue is used, elements are extracted in ascending order of priority (or key)  if a max priority queue is used, elements are extracted in descending order of priority (or key) Nov 4, 20095

Sorting Example Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue.  Put the five elements into a max priority queue.  Do five remove max operations placing removed elements into the sorted array from right to left. Nov 4, 20096

After Putting Into Max Priority Queue Sorted Array Max Priority Queue Nov 4, 20097

After First Remove Max Operation Sorted Array Max Priority Queue Nov 4, 20098

After Second Remove Max Operation Sorted Array Max Priority Queue Nov 4, 20099

After Third Remove Max Operation Sorted Array Max Priority Queue Nov 4,

After Fourth Remove Max Operation Sorted Array Max Priority Queue Nov 4,

After Fifth Remove Max Operation Sorted Array Max Priority Queue Nov 4,

Complexity Of Sorting Sort n elements.  n put operations => O(n log n) time.  n remove max operations => O(n log n) time.  total time is O(n log n).  compare with O(n 2 ) for sort methods Nov 4,

Heap Sort Uses a max priority queue that is implemented as a heap. Initial put operations are replaced by a heap initialization step that takes O(n) time. Nov 4,

Machine Scheduling  m identical machines (drill press, cutter, sander, etc.)  n jobs/tasks to be performed  assign jobs to machines so that the time at which the last job completes is minimum Nov 4,

Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule A B C time > Nov 4,

Machine Scheduling Example Finish time = 21 Objective: Find schedules with minimum finish time. A B C time > Nov 4,

LPT Schedules Longest Processing Time first. Jobs are scheduled in the order 14, 10, 7, 6, 5, 3, 2 Each job is scheduled on the machine on which it finishes earliest. Nov 4,

LPT Schedule [14, 10, 7, 6, 5, 3, 2] A B C Finish time is 16! Nov 4,

LPT Schedule LPT rule does not guarantee minimum finish time schedules. Usually LPT finish time is much closer to minimum finish time. Minimum finish time scheduling is NP-hard. Nov 4,

NP-hard Problems Infamous class of problems for which no one has developed a polynomial time algorithm. That is, no algorithm whose complexity is O(n k ) for any constant k is known for any NP- hard problem. The class includes thousands of real-world problems. Highly unlikely that any NP-hard problem can be solved by a polynomial time algorithm. Nov 4,

NP-hard Problems Since even polynomial time algorithms with degree k > 3 (say) are not practical for large n, we must change our expectations of the algorithm that is used. Usually develop fast heuristics for NP-hard problems.  Algorithm that gives a solution close to best.  Runs in acceptable amount of time. LPT rule is good heuristic for minimum finish time scheduling. Nov 4,

Complexity Of LPT Scheduling Sort jobs into decreasing order of task time.  O(n log n) time (n is number of jobs) Schedule jobs in this order.  assign job to machine that becomes available first  must find minimum of m (m is number of machines) finish times  takes O(m) time using simple strategy  so need O(mn) time to schedule all n jobs. Nov 4,

Using A Min Priority Queue Min priority queue has the finish times of the m machines. Initial finish times are all 0. To schedule a job remove machine with minimum finish time from the priority queue. Update the finish time of the selected machine and put the machine back into the priority queue. Nov 4,

Using A Min Priority Queue m put operations to initialize priority queue 1 remove min and 1 put to schedule each job each put and remove min operation takes O(log m) time time to schedule is O(n log m) overall time is O(n log n + n log m) = O(n log (mn)) Nov 4,

Huffman Codes Useful in lossless compression. Nov 4,

Min Tree Definition Each tree node has a value. Value in any node is the minimum value in the subtree for which that node is the root. Equivalently, no descendent has a smaller value. Nov 4,

Min Tree Example Root has minimum element. Nov 4,

Max Tree Example Root has maximum element. Nov 4,

Min Heap Definition complete binary tree min tree Nov 4,

Min Heap With 9 Nodes Complete binary tree with 9 nodes. Nov 4,

Min Heap With 9 Nodes Complete binary tree with 9 nodes that is also a min tree Nov 4,

Max Heap With 9 Nodes Complete binary tree with 9 nodes that is also a max tree Nov 4,

Heap Height Since a heap is a complete binary tree, the height of an n node heap is log 2 (n+1). Nov 4,

A Heap Is Efficiently Represented As An Array Nov 4,

Moving Up And Down A Heap Nov 4,

Putting An Element Into A Max Heap Complete binary tree with 10 nodes Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap Complete binary tree with 11 nodes Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Putting An Element Into A Max Heap New element is Nov 4,

Complexity Of Put Complexity is O(log n), where n is heap size Nov 4,

Removing The Max Element Max element is in the root Nov 4,

Removing The Max Element After max element is removed Nov 4,

Removing The Max Element Heap with 10 nodes Reinsert 8 into the heap. Nov 4,

Removing The Max Element Reinsert 8 into the heap Nov 4,

Removing The Max Element Reinsert 8 into the heap Nov 4,

Removing The Max Element Reinsert 8 into the heap Nov 4,

Removing The Max Element Max element is Nov 4,

Removing The Max Element After max element is removed Nov 4,

Removing The Max Element Heap with 9 nodes Nov 4,

Removing The Max Element Reinsert Nov 4,

Removing The Max Element Reinsert Nov 4,

Removing The Max Element Reinsert Nov 4,

Complexity Of Remove Max Element Complexity is O(log n) Nov 4,

Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] Nov 4,

Initializing A Max Heap Start at rightmost array position that has a child Index is n/2. Nov 4,

Initializing A Max Heap Move to next lower array position Nov 4,

Initializing A Max Heap Nov 4,

Initializing A Max Heap Nov 4,

Initializing A Max Heap Nov 4,

Initializing A Max Heap Nov 4,

Initializing A Max Heap Nov 4,

Initializing A Max Heap Find a home for 2. Nov 4,

Initializing A Max Heap Find a home for Nov 4,

Initializing A Max Heap Done, move to next lower array position Nov 4,

Initializing A Max Heap Find home for 1. Nov 4,

11 Initializing A Max Heap Find home for 1. Nov 4,

Initializing A Max Heap Find home for 1. Nov 4,

Initializing A Max Heap Find home for 1. Nov 4,

Initializing A Max Heap Done. 1 Nov 4,

Time Complexity Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1). Nov 4,

Complexity Time for level j subtrees is <= 2 j-1 (h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) = O(n). Nov 4,

Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time. Nov 4,

Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree. Nov 4,

A Binary Tree Nov 4,

An Extended Binary Tree number of external nodes is n+1 Nov 4,

The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x. Nov 4,

s() Values Example Nov 4,

s() Values Example Nov 4,

Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1 Nov 4,

Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x)) Nov 4,

A Leftist Tree Nov 4,

Leftist Trees--Property 1 In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root). Nov 4,

A Leftist Tree Length of rightmost path is 2. Nov 4,

Leftist Trees—Property 2 The number of internal nodes is at least 2 s(root) - 1 Because levels 1 through s(root) have no external nodes. So, s(root) <= log(n+1) Nov 4,

A Leftist Tree Levels 1 and 2 have no external nodes. Nov 4,

Leftist Trees—Property 3 Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree. Follows from Properties 1 and 2. Nov 4,

Leftist Trees As Priority Queues Min leftist tree … leftist tree that is a min tree. Used as a min priority queue. Max leftist tree … leftist tree that is a max tree. Used as a max priority queue. Nov 4,

A Min Leftist Tree Nov 4,

Some Min Leftist Tree Operations put() remove() meld() initialize() put() and remove() use meld(). Nov 4,

Put Operation put(7) Nov 4,

Put Operation put(7) Create a single node min leftist tree. 7 Nov 4,

Put Operation put(7) Create a single node min leftist tree. Meld the two min leftist trees. 7 Nov 4,

Remove Min Nov 4,

Remove Min Remove the root. Nov 4,

Remove Min Remove the root. Meld the two subtrees. Nov 4,

Meld Two Min Leftist Trees Traverse only the rightmost paths so as to get logarithmic performance. Nov 4,

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree. Nov 4,

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree. Nov 4,

Meld Two Min Leftist Trees Meld right subtree of tree with smaller root and all of other tree. Nov 4,

Meld Two Min Leftist Trees 8 6 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree. Nov 4,

Meld Two Min Leftist Trees Swap left and right subtree if s(left) < s(right). Make melded subtree right subtree of smaller root Nov 4,

Meld Two Min Leftist Trees Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right). Nov 4,

Meld Two Min Leftist Trees Swap left and right subtree if s(left) < s(right). Make melded subtree right subtree of smaller root Nov 4,

Meld Two Min Leftist Trees Nov 4,

Initializing In O(n) Time create n single node min leftist trees and place them in a FIFO queue repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue the process terminates when only 1 min leftist tree remains in the FIFO queue analysis is the same as for heap initialization Nov 4,