Priority Queues Supports the following operations. Insert element x.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
Advanced Data structure
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSE 373 Data Structures Lecture 12
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.
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.
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.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Binomial heaps, Fibonacci heaps, and applications.
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.
Christopher Moh 2005 Application of Data Structures.
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.
Chapter 21 Binary Heap.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
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.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
Analysis of Algorithms
School of Computing Clemson University Fall, 2012
Heaps (8.3) CSE 2011 Winter May 2018.
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
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,
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Binomial Heaps Chapter 19.
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Part-D1 Priority Queues
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Chapter 20 Binomial Heaps
ערמות בינומיות ופיבונצ'י
Fundamental Structures of Computer Science
Strict Fibonacci Heaps
CS 583 Analysis of Algorithms
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Binary Trees, Binary Search Trees
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Binary and Binomial Heaps
Binomial heaps, Fibonacci heaps, and applications
HEAPS.
Binary SearchTrees [CLRS] – Chap 12.
Fibonacci Heaps.
Implementation of Dijkstra’s Algorithm
Binomial heaps, Fibonacci heaps, and applications
CS 6310 Advanced Data Structure Wei-Shian Wang
Fibonacci Heaps & Doubled-Ended Heap Structures
CSE 373 Data Structures Lecture 12
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Binary Trees, Binary Search Trees
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Priority Queues Supports the following operations. Insert element x. Return min element. Return and delete minimum element. Decrease key of element x to k. Applications. Dijkstra's shortest path algorithm. Prim's MST algorithm. Event-driven simulation. Huffman encoding. Heapsort. . . . Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks

Priority Queues in Action PQinit() for each v  V key(v)   PQinsert(v) key(s)  0 while (!PQisempty()) v = PQdelmin() for each w  Q s.t (v,w)  E if (w) > (v) + c(v,w) PQdecrease(w, (v) + c(v,w)) Dijkstra's Shortest Path Algorithm

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 not possible to get all ops O(1) if only use comparisons because of N log N sorting lower bound Brodal gives variant of relaxed heap that achieves worst-case bounds for all operations relaxed heap: relaxes heap ordering property decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 O(|V|2) O(|E| log |V|) O(|E| + |V| log |V|) Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1

Binomial Tree Binomial tree. Recursive definition: Bk-1 B0 Bk B0 B1 B2 not binary tree B0 B1 B2 B3 B4

Binomial Tree Useful properties of order k binomial tree Bk. Number of nodes = 2k. Height = k. Degree of root = k. Deleting root yields binomial trees Bk-1, … , B0. Proof. By induction on k. B1 Bk Bk+1 B2 B0 B0 B1 B2 B3 B4

Binomial Tree A property useful for naming the data structure. Bk has nodes at depth i. depth 0 depth 1 depth 2 depth 3 depth 4 B4

Binomial Heap Binomial heap. Vuillemin, 1978. Sequence of binomial trees that satisfy binomial heap property. each tree is min-heap ordered 0 or 1 binomial tree of order k 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 sequence -> ordered 37 B4 B1 B0

Binomial Heap: Implementation Represent trees using left-child, right sibling pointers. three links per node (parent, left, right) Roots of trees connected with singly linked list. degrees of trees strictly decreasing from left to right heap coding with a variable number of links per node would be more painful 6 3 18 6 3 18 Parent 37 29 10 44 37 29 Right Left 48 31 17 48 10 50 50 31 17 44 Binomial Heap Leftist Power-of-2 Heap

Binomial Heap: Properties Properties of N-node binomial heap. Min key contained in root of B0, B1, . . . , Bk. Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is binary representation of N. At most log2 N + 1 binomial trees. Height  log2 N. 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 37 N = 19 # trees = 3 height = 4 binary = 10011 B4 B1 B0

Binomial Heap: Union Create heap H that is union of heaps H' and H''. "Mergeable heaps." Easy if H' and H'' are each order k binomial trees. connect roots of H' and H'' choose smaller key to be root of H 6 8 29 10 44 30 23 22 48 31 17 45 32 24 50 55 H' H''

+ Binomial Heap: Union 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 amusing to consider "multiplying" binomial heaps or other arithmetic ops. Any uses??? 32 24 50 28 33 25 + 55 41 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1

Binomial Heap: Union 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41

Binomial Heap: Union 12 18 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41

3 12 7 37 18 25 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 3 12 15 7 37 18 28 33 25 41

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 6 3 12 8 29 10 44 15 7 37 18 30 23 22 48 31 17 28 33 25 45 32 24 50 41 55

Binomial Heap: Union Create heap H that is union of heaps H' and H''. Analogous to binary addition. Running time. O(log N) Proportional to number of trees in root lists  2( log2 N + 1). 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1

Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H'  broken binomial trees H  Union(H', H) Running time. O(log N) 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 H

Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H'  broken binomial trees H  Union(H', H) Running time. O(log N) 6 18 8 29 10 44 37 H' 30 23 22 48 31 17 H 45 32 24 50 55

Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. Suppose x is in binomial tree Bk. Bubble node x up the tree if x is too small. Running time. O(log N) Proportional to depth of node x  log2 N . Bk has height k and 2k nodes 3 6 18 depth = 3 8 29 10 44 37 30 23 22 48 31 17 H x 32 24 50 55

Binomial Heap: Delete Delete node x in binomial heap H. Decrease key of x to -. Delete min. Running time. O(log N)

Binomial Heap: Insert Insert a new node x into binomial heap H. H'  MakeHeap(x) H  Union(H', H) Running time. O(log N) 3 6 18 x 8 29 10 44 37 30 23 22 48 31 17 H H' 45 32 24 50 55

Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. If N = .......0, then only 1 steps. If N = ......01, then only 2 steps. If N = .....011, then only 3 steps. If N = ....0111, then only 4 steps. Inserting 1 item can take (log N) time. If N = 11...111, then log2 N steps. But, inserting sequence of N items takes O(N) time! (N/2)(1) + (N/4)(2) + (N/8)(3) + . . .  2N Amortized analysis. Basis for getting most operations down to constant time. 3 6 x 29 10 44 37 48 31 17 50

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min What about search? Idea 1 = use a hash table to keep track of item's position in the heap. Idea 2 = use a (balanced) search tree. N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 just did this