Binomial Priority Queues

Slides:



Advertisements
Similar presentations
Interval Heaps Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in.
Advertisements

CSE 373 Data Structures Lecture 12
DEAPS By: Michael Gresenz Austin Forrest. Deaps A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min,
Binomial Heaps. Min Binomial Heap Collection of min trees
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
ANALYSIS OF SOFT HEAP Varun Mishra April 16,2009.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
Foundation of Computing Systems Lecture 6 Trees: Part III.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Binomial Queues Text Read Weiss, §6.8 Binomial Queue Definition of binomial queue Definition of binary addition Building a Binomial Queue Sequence of inserts.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CS 367 Introduction to Data Structures Lecture 8.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
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)
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Partially Ordered Data ,Heap,Binary Heap
Lecture: Priority Queue
School of Computing Clemson University Fall, 2012
Recitation 3 (Heap Sort and Binary Search Tree)
Heaps, Heapsort, and Priority Queues
CSC 533: Programming Languages Spring 2017
Priority Queues and Heaps
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Data Structures & Algorithms
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
Programming Abstractions
CSC 533: Programming Languages Spring 2016
COP4020 Programming Languages
Hashing Exercises.
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Initializing A Max Heap
Priority Queues.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
A simpler implementation and analysis of Chazelle’s
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Binomial Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
ערמות בינומיות ופיבונצ'י
Lecture #8 מבוא מורחב.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS 583 Analysis of Algorithms
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) A heap.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Sorting And Searching CSE116A,B 2/23/2019 B.Ramamurthy.
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Topic 5: Heap data structure heap sort Priority queue
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Binomial heaps, Fibonacci heaps, and applications
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Binomial Priority Queues
Heaps By JJ Shepherd.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
CS 6310 Advanced Data Structure Wei-Shian Wang
Fundamental Structures of Computer Science II
CSE 373 Data Structures Lecture 12
A Heap Is Efficiently Represented As An Array
Presentation transcript:

Binomial Priority Queues CS21b: Structure and Interpretation of Computer Programs Spring 2018

What is a priority queue? A priority queue is a data structure for dynamically maintaining a set of elements (think: positive integers) supporting the following operations: (make-queue) create an empty queue (insert x Q) insert integer x into the queue (find-max Q) find the largest element in the queue (remove-max Q) remove largest queue element (merge Q1 Q2) merge two queues We implement a priority queue using a forest of binomial trees. Goal: implement these operations, on an n-node queue, in O(log n) time.

Why are we learning about this? It’s an interesting and complicated example of list structures and recursion. It’s a nice algorithm. It has something to do with addition. It’s serious. It’s not boring.

What is a binomial tree? B0 B1 B2 B3 B4

Two ways of thinking about binomial trees: Bk+1 is either... . . . Bk-1 Bk-2 B2 B1 B0 Bk

(binomial coefficients) Bk has 2k nodes ( k ) Bk+1 = Bk with nodes at the j-th level--recall that j Bk ∑ ( ) k = 2k j 0≤j≤k (binomial coefficients)

Representing a binomial tree as a list structure a e c b (a b (c d) (e f (g h)) g f d B3 h (r B0 B1 B2 ... Bk-2 Bk-1 Bk) r . . . Bk-1 Bk-2 B2 B1 B0 Bk

Representing a binomial tree of integers as a list structure (a b (c d) (e f (g h)) e c b g f d (Integer) elements are heap ordered: a > b, c, e c > d e > f, g g > h B3 h Bk-1 Bk-2 B2 B1 B0 Bk

where no two trees are the same size A binomial queue is a forest of binomial trees where no two trees are the same size a j n ? e c b l k g f d m h {a,b,c,d,e,f,g,h,j,k,l,m,n} Note: 1 + 4 + 8 = 13 elements B0 B1 B2 B3 (n () (j k (l m)) (a b (c d) (e f (g h)) Claim: Regardless of the queue on a set of integers (not unique!), the maximum element is always the root of some tree in the forest. B0

Insertion into the queue Why is this procedure like adding 1? (define (make-queue) ‘()) (define (tack x lst) (if (null? lst) (list x) (cons (car lst) (tack x (cdr lst))))) (define (root e) (if (number? e) e (car e))) (define (meld e1 e2) ;; for combining two equal-sized binomial trees (if (and (number? e1) (number? e2)) (list (max e1 e2) (min e1 e2)) (if (< (root e1) (root e2)) (tack e1 e2) (tack e2 e1)))) (define (insert e q) (if (null? q) (list e) (if (null? (car q)) (cons e (cdr q)) (cons '() (insert (meld e (car q)) (cdr q)))))) (tack 10 ‘(0 2 4 6 8)) ;Value: (0 2 4 6 8 10) (meld 5 10) ;Value: (10 5) (meld '(15 0) '(10 5)) ;Value: (15 0 (10 5)) (insert 10 (make-queue)) ;Value: (10) (insert 5 (insert 10 (make-queue))) ;Value: (() (10 5)) (insert 15 (insert 5 (insert 10 (make-queue)))) ;Value: (15 (10 5)) (insert 0 (insert 15 (insert 5 (insert 10 (make-queue))))) ;Value: (() () (15 0 (10 5))) Insertion takes time logarithmic in the queue size---why? Why is this procedure like adding 1?

Why is queue merging like binary addition? Merging two queues (merge ‘(1 () (9 8 (7 6) (5 4 (3 2)))) ‘(10 (14 13 (12 11)) (22 21 (20 19) (18 17 (16 15)))) ;Value: (() (10 1) (18 17 (16 15)) (9 8 (7 6) (5 4 (3 2))))) (define (merge q1 q2) (cond ((null? q1) q2) ((null? q2) q1) ((null? (car q1)) (cons (car q2) (merge (cdr q1) (cdr q2)))) ((null? (car q2)) (cons (car q1) (else (cons '() (mergecarry (cdr q1) (cdr q2) (meld (car q1) (car q2))))))) 1001 + 1101= 10110 Why is queue merging like binary addition?

Merging two queues (with carry) (mergecarry ‘(1 () (9 8 (7 6) (5 4 (3 2)))) ‘(10 (14 13 (12 11)) (22 21 (20 19) (18 17 (16 15))) 100) ;Value: ((100) (10 1) (18 17 (16 15)) (9 8 (7 6) (5 4 (3 2))))) (define (mergecarry q1 q2 carry) (cond ((null? q1) (insert carry q2)) ((null? q2) (insert carry q1)) ((null? (car q1)) (merge (cons carry (cdr q1)) q2) ((null? (car q2)) (merge q1 (cons carry (cdr q2)))) (else (cons carry (mergecarry (cdr q1) (cdr q2) (meld (car q1) (car q2))))))) 1001 + 1101 + 1 = 10111 Why is queue merging like binary addition?

Finding the maximum element in a queue (define (max-elt e) (if (null? e) 0 (if (number? e) e (car e)))) (define (find-max q) (apply max (map max-elt q)))

by removing large, empty subtrees Cleaning up a queue by removing large, empty subtrees (like leading zeroes in a binary number) (define (slinkyleft leftlist rightlist) (if (null? rightlist) leftlist (slinkyleft (cons (car rightlist) leftlist) (cdr rightlist)))) (define (clean lst) (if (null? lst) '() (if (null? (car lst)) (clean (cdr lst)) lst))) (define (cleanup q) (clean (slinkyleft '() q)))) (cleanup '(1 () (9 8 (7 6) (5 4 (3 2))) ())) ;Value: (1 (9 8 (7 6) (5 4 (3 2))))

Removing the maximum element in a queue (define (select q maxval) ; returns (tree with max root . rest of queue ; with () in place of selected tree) (if (null? q) (error "Cannot select from empty queue") (if (= maxval (max-elt (car q))) (cons (car q) (cleanup (cons '() (cdr q)))) (let ((v (select (cdr q) maxval))) (cons (car v) (cdr v)))))))) (define (remove-max q) (let ((q (select q (find-max q)))) (if (number? (car q)) (cdr q) (merge (cdar q) (cdr q))))) (define (removes q n) (if (= n 0) q (removes (remove-max q) (- n 1)))) (define qq ‘(() () () () (16 15 (14 13) (12 11 (10 9)) (8 7 (6 5) (4 3 (2 1))))) ;Value: qq (removes qq 1) ;Value: (15 (14 13) (12 11 (10 9)) (8 7 (6 5) (4 3 (2 1)))) (removes qq 2) ;Value: (() (14 13) (12 11 (10 9)) (8 7 (6 5) (4 3 (2 1)))) (removes qq 3) ;Value: (13 () (12 11 (10 9)) (8 7 (6 5) (4 3 (2 1)))) (removes qq 4) ;Value: (() () (12 11 (10 9)) (removes qq 5) ;Value: (11 (10 9) ()

Removing the maximum element in a queue (define qq '(() () () () (16 14 (13 12) (15 11 (10 9)) (8 4 (6 5) (7 3 (2 1)))))) ;Value: qq (removes qq 1) ;Value: (14 (13 12) (15 11 (10 9)) (8 4 (6 5) (7 3 (2 1)))) (removes qq 2) ;Value: (() (14 11) (13 12 (10 9)) (removes qq 3) ;Value: (11 () (13 12 (10 9)) (removes qq 4) ;Value: (() () (12 11 (10 9)) (removes qq 5) ;Value: (11 (10 9) () (removes qq 6) ;Value: (() (10 9) () (removes qq 7) ;Value: (9 () () (8 4 (6 5) (7 3 (2 1)))) (removes qq 8) ;Value: (() () () (8 4 (6 5) (7 3 (2 1)))) (removes qq 9) ;Value: (4 (6 5) (7 3 (2 1))) (removes qq 10) ;Value: (() (4 3) (6 5 (2 1))) (removes qq 11) ;Value: (5 () (4 3 (2 1))) (removes qq 12) ;Value: (() () (4 3 (2 1))) (removes qq 13) ;Value: (3 (2 1)) (removes qq 14) ;Value: (() (2 1)) (removes qq 15) ;Value: (1) (removes qq 16) ;Value: ()