CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.

Slides:



Advertisements
Similar presentations
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)
Advertisements

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.
Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator.
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.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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. 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.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 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.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max 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.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
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:
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
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.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
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:
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Heapsort CSE 373 Data Structures.
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,
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
7/23/2009 Many thanks to David Sun for some of the included slides!
Priority Queue & Heap CSCI 3110 Nan Chen.
Data Structures & Algorithms Priority Queues & HeapSort
Priority Queues.
CSE 373: Data Structures and Algorithms
Priority Queues.
Priority Queues.
CE 221 Data Structures and Algorithms
Heapsort CSE 373 Data Structures.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Priority Queues (Heaps)
Presentation transcript:

CSC 172 DATA STRUCTURES

Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert an element with a given priority Deletemax or Deletemin Find and remove from the set the element with highest priority

PriorityQueue Interface public Interface PriorityQueue { int size(); boolean isEmpty(); void add(Object element); Object getMax(); Object deleteMax(); }

Sorted Linked List implementation Deletemax is O(1) – take first element Insert is O(n) – on average, go halfway down the list

Partially Ordered Tree A binary tree with elements and priorities at nodes Satisfying the partially ordered tree (POT) property The priority at any node >= the priority of its children In Weiss, the VALUE of the key is <= values of children, interpretation is up to user.

Balanced POT A POT in which all nodes exist, down to a certain level, and for one level below that, “extra” nodes may exist, as far left as possible

Insertion on a Balanced POT 1. Place new element in the leftmost possible place 2. “Bubble up” new node by repeatedly exchanging it with its parent if it has higher priority than its parent  Restores the POT property  Maintains balanced property  O(log n) because an n-node balanced tree has no more than 1 + log 2 n levels

Deletemax on Balanced POT 1. Select element at root 2. Replace root element by rightmost element at lowest level 3. Bubble down root by repeatedly exchanging it with the larger of its children, as long as it is smaller than either  Restores POT property  Preserves balance  O(log n) because O(1) at each step along a path of length at most log 2 n

Heap Data Structure Represent a balanced POT by an array A n nodes represented by A[1] through A[n] A[0] not used Array holds elements level-by-level  A node represented by A[k] has parent A[k/2] Children of A[k] are: A[2*k] A[2*k+1]

Bubble up in Heap Array At position k: 1. If k == 1, then done 2. Otherwise, compare A[k] with A[k/2].  If A[k] is smaller, then done  Otherwise swap and repeat at k/2

Bubble down in Heap Array At positition k 1. If 2k > n, then done 2. Otherwise, see if A[k] is smaller than A[2k] or A[2k + 1] (if 2k+1 <= n)  If not, done  If so, swap with larger and repeat

Insert in Heap Array 1. Put new element in A[n+1] 2. Add 1 to n 3. Bubble up

Insert on heap – O(log n)  Put new element in A[n+1] O(1)  Add 1 to n O(1)  Bubbleup O(log n)

Deletemax in Heap Array 1. Take A[1] 2. Replace it by A[n] 3. Subtract 1 from n 4. Bubble down

Deletemax in heap array – O(log n)  Take A[1] O(1)  Replace it by A[n] O(1)  Subtract 1 from n O(1)  Bubble down O(log n)

POP QUIZ  What heap do you get if you 1. Insert {1,8,2,7,3,6,4} 2. Deletemax 3. Deletemax 4. Insert {5,9} 5. Deletemax

Make a Heap 1. Simplest idea: insert N items: O(n) average, O(nlogn) worst case. 2. Heapify array A by bubbling down A[k] for: k = n/2, n/2-1, n/2-2,…,1 in order How long does this take? Might seems like O(n log n) but really O(n) 1. Repeatedly deletemax until all are selected Priority is reverse of sorted order n O(log n) each == O(n log n)

Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 

Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 

Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 

Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 

Big-Oh of Heapify Max. number of swaps at each level  No swaps At most one each At most two each

Big-Oh of Heapify 0123… n/2 n/4n/8n/16…

Big-Oh of Heapify 0123… n/2 n/4n/8n/16… (n/2)(1/2+2/4+3/8+4/16…..) <

Big-Oh of Heapify 1/2+2/4+3/8+4/16….. 1/2+(1/4+1/4)+(1/8+1/8+1/8) +….. 1/2+ 1/4 + 1/8 + … = 1 1/4 + 1/8 +….. = 1/2 1/8 +….. = 1/4 ….. = …  So, O(2*n/2) = O(n)