Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
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)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
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.
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.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Week 10: Heap and Priority queue. Any feature here?
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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.
1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
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.
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does.
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 Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
Heaps & Priority Queues
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.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
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.
"Teachers open the door, but you must enter by yourself. "
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heap Sort and Priority Queues
Algorithms and Data Structures Lecture VI
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,
Priority Queues Chuan-Ming Liu
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heapsort.
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Heaps 11/27/ :05 PM Heaps Heaps.
© 2013 Goodrich, Tamassia, Goldwasser
"Teachers open the door, but you must enter by yourself. "
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
Priority Queues CSE 373 Data Structures.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

Data Structure & Algorithm II

 In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required by each job is known in advance. Further, jobs cn be preempted (stopped and resumed later)  One policy which minimizes the average waiting time is SPRT (shortest remaining processing time)  The processor schedules the job with the smallest remaining processing time

 If while a job is running a new job arrives with processing time less than the remaining time of current job, the current job is preempted.

 We need to maintain the remaining processing time of the unfinished jobs at any point in time.  We need to find the job with the shortest remaining processing time.  When a job finishes we should remove it from our collection.  When a new job arrives we need to add it to the collection.

 A priority queue is an ADT (abstract data type) for maintaining a set S of elements, each which an assosiated value called priority.  A PQ supports the following operations  Insert(x) insert element x in set S (S  SU(x))  Minimum() returns the element of S with smallest priority.  Delete-min() returns and removes the element of S with smallest priority.

 A PQ ranks its elements by priority.  Every element has a priority. Priorities are not necessarily unique but are totally ordered.  Total order relation, denoted by ≤  Reflexive : k ≤ k  Antisymetric: if k1 ≤ k2 and k2 ≤ k1. then k1 = k2  Transitive: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3

 The most general and reusable form of a PQ makes use of comparator objects.  Comparator objects are external to the keys that are to be compared and compare two objects.  When the priority queue needs to compare two keys, it uses the comparator it was given to do the comparison.  Thus a priority queue can be general enough to store any object.

 The comparator ADT includes:  isLessThen(a,b)  isLessThenOrEqualTo(a,b)  isEqualTo(a,b)  isGreaterThen(a,b)  isGreaterThenOrEqualTo(a,b)  isComparable(a)

 The items are pairs (priority, element)  We can implement insert() by using insertLast() on the sequence. This takes O(1) time.  However, because we always insert at the end, irrespective of the key value, our sequence is not ordered

 Thus, for methods such as minimum(), delete- min(), we need to look at all the elements of S. The worst case time complexity for these methods is O(n)

 Another implementation uses a sequence S, sorted by by increasing priorities.  minimum() and delete-min() take O(1) time

 However, to implement insert(), we must now scan through the entire sequence in the worst case. Thus insert() runs in O(n) time

 Applications:  Job scheduling shared computing resources (unix)  Event simulation.  As a building block for other algorithms.  A heap can be used to implement a PQ

 A binary tree that stores priorities (or priority- element) pairs at nodes.  Structural property: All levels except last are full. Last level is left-filled.  Heap Priority: Priority of node is at least as large as that of its parents.

 Heap property violated

 Last level not left-filled

 The element with smallest priority always sits at the root of the heap.  This is because if it was else where, it would have parent with the larger priority and this would violate the heap property.  Hence minimum() can be done in O(1) time.

 Suppose a heap of n nodes has height h.  Recall: complete binary tree of height h has 2^(h+1)-1 nodes.  Hence 2^h-1 <=n<=2^(h+1)-1.  h=[log2 n]

Parent(i) Return [i/2] Left(i) Return 2i Right(i) Return 2i+1 A Heap property: A[parent(i)] <= A[i]

 Notice the implicit tree links; children of node i are 2i and 2i+1  Why is this usefull?  In a binary representation, a multiplication/division by two is left/right shift

 Insert 40  Insert

 Enlarge heap  Consider path from root to inserted node  Find topmost element on this path with higher priority that of inserted element.  Insert new element at this location by shifting down the other elements on the path

 The only nodes whose contents change are the ones on the path.  Heap property may be violated only for children of these nodes.  But new contents of these nodes only have lower priority.

 i is index into the array A  Binary trees rooted at Left(i) and Right(i) are heaps.  But, A[i] might be larger than its children, thus violating the heap property.  The method heapify makes binary tree rooted at i heap by moving A[i] down the heap.

 Heap property violated at node with index 1 but subtrees rooted at 2,3 are heaps.  Heapify(1)

 Heapify(i) traces a path down the tree.  Last node on the path (say j) has both A[left(j)], A[right(j)] are larger than A[i].  All elements on path have lower priority than their siblings.  All elements on this path are moved up.  A[i] goes to location.

 A heap of n nodes has height O(log n).  While inserting we might have to move the element all the way to the top.  Hence at most O(log n) steps required.  In Heapify, the element might be moved all the way to the last level.  Hence heapify also requires O(log n) time