WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Leftist Heaps Text Read Weiss, §23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heap: A Special Kind of Tree
5.9 Heaps of optimal complexity
Dr. Andrew Wallace PhD BEng(hons) EurIng
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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 and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH 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.
Cpt S 223 – Advanced Data Structures Priority Queues
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
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 STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
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.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
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:
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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.
CSE 326: Data Structures Priority Queues (Heaps)
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Topics covered (since exam 1):
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
Data Structures – Week #8
Binary Trees, Binary Search Trees
Topics covered (since exam 1):
Ch. 8 Priority Queues And Heaps
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
Data Structures – Week #8
Leftist Heaps Text Leftist Heap Building a Leftist Heap
CS 6310 Advanced Data Structure Wei-Shian Wang
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queues Binary Heaps
Binary Trees, Binary Search Trees
Priority Queues (Heaps)
Presentation transcript:

WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin

OUTLINE REVIEW OF PRIORITY QUEUES AND BINARY HEAPS LEFTIST HEAP – Properties – Recursive Merging – Iterative Merging 2CE222_Dr. Senem Kumova Metin

PRIORITY QUEUE A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority) Example Applications: printer -> print (dequeue) the shortest document first operating system -> run (dequeue) the shortest job first normal queue -> the first enqueued element first 3CE222_Dr. Senem Kumova Metin

PRIORITY QUEUE (HEAP) insert (enqueue) deleteMin (dequeue) – smaller value higher priority – Find / save the minimum element, delete it from structure and return it 4CE222_Dr. Senem Kumova Metin

Priority Queue:Implementation Unsorted linked list – insert takes O(1) time – deleteMin takes O(N) time Sorted linked list – insert takes O(N) time – deleteMin takes O(1) time Binary Tree – insert takes O(log N) time on the average – deleteMin takes O(log N) time on the average – support other operations that are not required by priority queue (for example, findMax) – deleteMin operations make the tree unbalanced 5CE222_Dr. Senem Kumova Metin

Binary Heap Implementation Property 1: Structure Property Binary tree & completely filled (bottom level is filled from left to right) (complete binary tree) If height is h, size between 2 h (bottom level has only one node) and 2 h+1 -1 Property 2: Heap Order Property(for Minimum Heap) Any node is smaller than (or equal to) all of its children (any subtree is a heap) Smallest element is at the root (findMin take O(1) time) 6CE222_Dr. Senem Kumova Metin

Binary Heap Implementation 7CE222_Dr. Senem Kumova Metin Property 1: Structure Property height= ?? Property 2: Heap Order Property(for Minimum Heap) Parent< right child Parent < left child

8 Binary Heap Implementation 8CE222_Dr. Senem Kumova Metin

9 Binary Heap Implementation 9CE222_Dr. Senem Kumova Metin

10 Binary Heap Implementation 10CE222_Dr. Senem Kumova Metin

11 Binary Heap Implementation

CE222_Dr. Senem Kumova Metin12 Binary Heap Implementation 1

CE222_Dr. Senem Kumova Metin Binary Heap Implementation : deleteMin

Binary Heap Implementation insert worst case: takes O(log N) time, moves an element from the bottom to the top on average: takes a constant time (2.607 comparisons), moves an element up levels deleteMin worst case: takes O(log N) time on average: takes O(log N) time (element that is placed at the root is large, so it is percolated almost to the bottom ) 14CE222_Dr. Senem Kumova Metin

LEFTIST HEAPS CE222_Dr. Senem Kumova Metin15

16 Leftist Heaps : Motivation Binary heaps perform merge operation in O(N) time Challenge: Build a heap that can merge in O(logN) time Use an array just like the Binary heap Traditionally data structures support efficient merging use linked lists Using linked lists is inefficient due to pointer operations 16CE222_Dr. Senem Kumova Metin

17 Leftist Heaps : Definitions A Leftist (min)Heap is a binary tree that satisfies the following conditions: If X is a node and L and R are its left and right children, then: Property 1. Heap Order Property X.value ≤ L.value X.value ≤ R.value Property 2. Leftist Heap Property Null path length of L ≥ Null path length of R 17CE222_Dr. Senem Kumova Metin

18 Leftist Heaps : Definitions Null Path Length (npl) of a node is the shortest distance from a node to a descendant with 0 or 1 child If a node is null, its null path length is −1. 18CE222_Dr. Senem Kumova Metin

19 Leftist Heaps : Definitions npl(X) of any node X to be the length of the shortest path from X to a node without two children. npl of a node with zero or one child is 0, while npl(NULL) = -1 19CE222_Dr. Senem Kumova Metin

Example: Null Path Length node npl CE222_Dr. Senem Kumova Metin

Theorem 6.2 Theorem: A leftist tree with r nodes on the right path must have at least 2 r -1 nodes. 21CE222_Dr. Senem Kumova Metin

Theorem 6.2 :Proof by Induction r=1, at least 1 tree node is required  True Induction hypothesis: Tree with r nodes on the right path has 2 r -1 nodes. For a leftist tree with r+1 nodes on the right path then the root has a right subtree with r nodes on the right path, and a left subtree with at least r nodes on the right path (npl(left)>=npl(right)) Then each subtree has at least 2 r -1 nodes The tree including the root then has (2 r -1) + (2 r -1) +1=2 r+1 -1 nodes then, proving the theorem 22CE222_Dr. Senem Kumova Metin

Theorem 6.2 Result From this theorem, it follows immediately that a leftist tree of N nodes has a right path containing at most log(N + 1) nodes. The general idea for the leftist heap operations is to perform all the work on the right path, which is guaranteed to be short. 23CE222_Dr. Senem Kumova Metin

Leftist Heap : Operations The fundamental operation on leftist heaps is merging. – Recursive – Iterative Insertion ?  A special case of merging, since we may view an insertion as a merge of a one-node heap with a larger heap. 24CE222_Dr. Senem Kumova Metin

Leftist Heap Merge : Iterative version Requires 2 passes on the heap: First pass: Create a new tree by merging the right paths of both heaps in sorted order Second pass: Perform child swaps at nodes that violate the leftist heap property. Performs merging in O(logN) time 25CE222_Dr. Senem Kumova Metin

Leftist Heap Merge : Iterative version Example Heaps H1 and H  SORT 

Iterative Merge: Example Heaps First pass 27CE222_Dr. Senem Kumova Metin

Iterative Merge: Example First pass Second pass 28CE222_Dr. Senem Kumova Metin

(1) If either of the two heaps is empty, then return the other heap. (2) Otherwise, to merge the two heaps, compare their roots, and recursively merge the heap with the larger root (H2) with the right subheap of the heap with the smaller root (H1) (3) Make the new heap the right child of the root of H1 (4) Make the entire tree leftist by swapping the root's left and right children and updating the null path length Leftist Heap Merge : Recursive version 29CE222_Dr. Senem Kumova Metin

Binary Heap versus Leftist Heap CE222_Dr. Senem Kumova Metin30 InsertionDelete Minimum Merge Binary Heap O(logN) O(N) Leftist Heap O(logN)