Priority Queues 4-19-2002. Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? The designs for assignment.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
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.
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.
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
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!
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
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.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
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 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
David Stotts Computer Science Department UNC Chapel Hill.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
AVL Trees Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? zYour minute essay last.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CSE 250 September 29 – October 3, A NNOUNCEMENTS Homework 4 due 10/5 Project 1 posted for 10/6 Exam 2 10/8 No classes meet 10/9 Project 1 due 10/26.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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.
CS 367 Introduction to Data Structures Lecture 8.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
"Teachers open the door, but you must enter by yourself. "
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
CSE 373: Data Structures and Algorithms
Computer Science 2 Heaps.
"Teachers open the door, but you must enter by yourself. "
Priority Queues & Heaps
HEAPS.
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Presentation transcript:

Priority Queues

Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? The designs for assignment #6 are supposed to be due on Monday, but since I won’t be here you can have that extended until Wednesday. zWhat is a priority queue and what do we use them for?

Motivation zConsider the following problems. yYou are writing software to control what jobs get sent to a printer. Different jobs have different priorities so that things that matter more should get printed first. How do you set this up in an efficient way? yYou are writing some type of “event” handling system. Every so often you are given a task and a time when it needs to be started. You need to start them in the proper order.

Operations and Requirements zFor these types of problems we need a container with only three operations. One is to be able to get the minimum or maximum element. The others are to be able to add new elements and remove the minimum. zThe idea would be O(1) for both operations, but that generally isn’t attainable. We can get O(1) for finding and O(log n) for the adding/removing.

Heaps zThe data structure we want to use to achieve this is called a heap. A heap is a tree type structure, but it isn’t sorted in the same way as the binary trees we discussed. Instead it uses the “Heap- Order Property” where all the nodes below a given node have values higher than it. zRecursively we can just say, the root is the minimum value in the tree.

Complete Trees as Arrays zWhen a tree is complete we can easily represent it as an array. In a complete tree, the left most nodes in a generation get their children first, and nodes get a left child before a right child. zNote that for a binary tree, the path to a leaf can be expressed as a binary number. If we prefix it with a 1 we get a unique encoding where the first element is 1. This also allows up to quickly find parents.

Inserting to a Heap zTo insert into a binary heap, we put a “bubble” node at the next open space and let it move up the heap until it moves into place. zAt each step it gets swapped with a value greater than it so the heap-order property is maintained. zThis operation takes O(log n) time because the heap is a complete tree.

Removing from a Heap zTo remove the smallest element we simply make the “bubble” node at the top and this time the last element in the heap is what will wind up filling it. We let it “sink” down through the tree. At each step we swap it with the smaller of the two children assuming that it is larger than them. If it isn’t it can stop there.

Minute Essay zI want you to draw the heaps that result from adding the following numbers into an empty heap then removing two of them. 4,8,6,2,3,7 zThere will be no class on Monday. On Wednesday my flight arrives at 8:30am so there is a non-zero chance I could miss class that day as well. I will call the office if I’m running late so someone will come in here to let you know.