CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Nick Harvey & Kevin Zatloukal
CSE 373 Data Structures Lecture 12
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.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
§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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
CS223 Advanced Data Structures and Algorithms 1 Review for Midterm Neil Tang 03/06/2008.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
Review for Exam 1 Topics covered: –Asymptotic analysis –Lists and list implementations Stacks and queues –General trees, binary trees –BST For each of.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
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.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Exam 3 Review Data structures covered: –Hashing and Extensible hashing –Priority queues and binary heaps –Skip lists –B-Tree –Disjoint sets For each of.
Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’
Review for Exam 2 Topics covered: –Recursion and recursive functions –General rooted trees –Binary Search Trees (BST) –Splay Tree –RB Tree –K-D Trees For.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CS 201 Data Structures and Algorithms
October 30th – Priority QUeues
Source: Muangsin / Weiss
Cse 373 April 26th – Exam Review.
Review for Midterm Neil Tang 03/04/2010
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queue and Binary Heap Neil Tang 02/12/2008
CE 221 Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CS223 Advanced Data Structures and Algorithms
Heap code in C++ template <class eType>
CSE 373 Data Structures and Algorithms
Priority Queues (Heaps)
CSE 373: Data Structures and Algorithms
Priority Queues Binary Heaps
Presentation transcript:

CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues

Today – Priority Queues Priority queues Array representation of binary heaps Binary heap operations – insert and deleteMin Build heap operation Why buildHeap takes linear time? Brief review – binary search tree, AVL tree, splay tree, heap

Binary Heaps

Array Implementation

Example – Insert 14

Example – deleteMin

deleteMin

percolateDown

Time Behavior What is the worst-case time for deleteMin? What is the worst-case time for insert? What is the average-case time for insert?

Examples in Class We may illustrate the idea with a few examples:

Build Heap How much time is required?

buildHeap

Example percolateDown(7):

Example percolateDown(6) and then percolateDown(5):

Example percolateDown(4) and then percolateDown(3):

Example percolateDown(2) and then percolateDown(1):

buildHeap We start with an unordered tree. The seven remaining trees show the result of each of the seven percolateDowns. Each dash line corresponds to two comparisons: one to find the smaller child and one to compare the smaller child with the node. Note that there are only 10 dashed lines in the entire algorithm (there could have been an 11 th – where?) corresponding to 20 comparisons. How do we bound the running time of buildHeap? How do we bound the number of dashed lines? Why do we compute the sum of the heights of all the nodes? What is an upper bound for the sum?

Theorem 6.1 For the perfect binary tree of height h containing 2 h+1 – 1 nodes, the sum of the heights of the nodes is 2 h+1 – 1 – (h+1). It is easy to see that this tree consists of one node at height h, two nodes at height h – 1, 2 2 nodes at height h – 2, and in general 2 i nodes at height h – i. The sum of the heights of all the nodes is therefore S = h + 2(h – 1) (h – 2) (h – 3) + … + 2 h–1 (1) How do we find S? Multiply equation by 2: 2S = 2h (h – 1) (h – 2) (h – 3) + … + 2 h (1) Subtract and get S = –h … + 2 h–1 + 2 h What does S equal?

Exact Value of S Sum of the heights equals N – b(N), where b(N) is the number of 1’s in the binary representation of N. We may prove this result by induction. An illustration: N = 10, or 1010 in binary. Sum of height = 10 – 2 = 8. Add one node. N = 11, or 1011 in binary. Sum of height = 11 – 3 = 8. Add one node. N = 12, or 1100 in binary. Sum of height = 12 – 2 = 10. Add one node. N = 13, or 1101 in binary. Sum of height = 13 – 3 = 10.

Binary Search Tree Property that turns a binary tree into a binary search tree Which is the appropriate node traversal scheme? Operation contains Operation findMin Operation findMax Operation insert Operation remove

AVL Tree Property that turns a binary search tree into an AVL tree Balance condition Single rotation Double rotation Operations contains, findMin, findMax, insert, remove. Time?

Splay Tree Is it a binary search tree? What does a splay tree guarantee? Basic idea of splay tree? Zig-zig and zig-zag:

Heap Is it a binary search tree? What is a complete binary tree? Operations insert, deleteMin, and buildHeap. Operations percolateUp and percolateDown.