Docsity

Introduction to Priority Queues - Data Structures - Slides | CMSC 420, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Home Page
Title Page
Contents
JJ II
JI
Page 1of 18
Go Back
Full Screen
Close
Quit
Introduction to Priority Queues
•Want to maintain a collection of nitems to support findMin,
insert and deleteMin in O(log n) time
–Note that we are not interested in doing arbitrary remove() or
find()
–A linked list or array requires that some operations take O(n)
time
–An unbalanced binary search tree does not have a good worst
case. A balanced tree (or even splay trees) is too much work
•A binary heap is implemented like an array and in addition supports
insert in O(1) expected time, and findMin in worst-case O(1)
time.
•Excellent for general hierarchical searches, and possibly sorting
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Introduction to Priority Queues - Data Structures - Slides | CMSC 420 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Title Page

Contents

JJ II

J I

Page 1 of 18

Go Back

Full Screen

Close

Introduction to Priority Queues

• Want to maintain a collection of n items to support findMin,

insert and deleteMin in O(log n) time

– Note that we are not interested in doing arbitrary remove() or

find()

– A linked list or array requires that some operations take O(n)

time

– An unbalanced binary search tree does not have a good worst

case. A balanced tree (or even splay trees) is too much work

• A binary heap is implemented like an array and in addition supports

insert in O(1) expected time, and findMin in worst-case O(1)

time.

• Excellent for general hierarchical searches, and possibly sorting

Title Page

Contents

JJ II

J I

Page 2 of 18

Go Back

Full Screen

Close

What Is A Heap?

• A heap is a complete binary tree that stores keys in internal nodes

with the order property key(parent) ≤ key(child)

• Height of the tree is logarithmic

• With n keys, a heap can be constructed in O(n) time (stored in an

array of size n)

Title Page

Contents

JJ II

J I

Page 4 of 18

Go Back

Full Screen

Close

Example

Title Page

Contents

JJ II

J I

Page 5 of 18

Go Back

Full Screen

Close

Example

Title Page

Contents

JJ II

J I

Page 7 of 18

Go Back

Full Screen

Close

Example

Title Page

Contents

JJ II

J I

Page 8 of 18

Go Back

Full Screen

Close

Example

Title Page

Contents

JJ II

J I

Page 10 of 18

Go Back

Full Screen

Close

Application of Heaps: Nearest Neighbor

• Given hierarchical data structure containing points (in general data

objects) and a query object

• We want to output the data object closest to the query object

• Approach: Recursive travel of the data structure using a priority

queue to prune the search space

– Q contains objects with a lower bound

– Want to insert and remove items from Q

– We stop searching if current cost is less than the lower bound

stored in Q

Title Page

Contents

JJ II

J I

Page 11 of 18

Go Back

Full Screen

Close

Nearest Neighbor: Pruned version

f l o a t lowerbound ( R e c t a n g l e r , Point p ) { i f ( r. i n s i d e ( p ) ) return 0 ; i f ( r. l e f t ( p ) ) return r. minX − p. x ; i f ( r. r i g h t ( p ) ) return p. x − r. maxX ; i f ( r. southEastCorner ( p ) )... } R e s u l t p r o c e s s (KDNode k , int cd , R e c t a n g l e r , R e s u l t r e s ) { i f ( k == n u l l ) return r e s ; i f ( lowerbound ( r , q) >= r e s. d i s t a n c e ) return r e s ; f l o a t d i s t = d i s t a n c e ( node. data , query ) ; i f ( d i s t < r e s. d i s t a n c e ) { r e s u l t. p o i n t = node. data ; r e s u l t. d i s t a n c e = d i s t a n c e ( node. data , query ) ; } i f ( q [ cd ] < k. data [ cd ] )... e l s e... return r e s ; }

Title Page

Contents

JJ II

J I

Page 13 of 18

Go Back

Full Screen

Close

Which Nodes Are Processed?

Title Page

Contents

JJ II

J I

Page 14 of 18

Go Back

Full Screen

Close

Any Improvement Possible?

• Nine nodes examined. Can we do better?

• Yes, if decision to pick a node is based on dynamic changing costs

instead of initial left-right decision.

• Use a priority queue. (Seven nodes examined instead of nine)

Title Page

Contents

JJ II

J I

Page 16 of 18

Go Back

Full Screen

Close

Nearest Neighbor: Priority Q Version

Q contents

• [(35, 90), 0];!

• [(70, 80), 0],

[(10, 75), 5];

• [(80, 40), 0],

[(10, 75), 5],

[(50, 90), 30];

• [(70, 30), 0],... ,

[(90, 60), 40];!

• [(10, 75), 5],

[(50, 25 , 20],... ;

• [(25, 10), 5],... ,

• [(20, 50), 15],... ,!

Title Page

Contents

JJ II

J I

Page 17 of 18

Go Back

Full Screen

Close

Nearest Neighbor: Q version

i n s e r t Q (KDNode n , f l o a t c o s t , int cd ) { / / i n s e r t i n t o a p r i o r t y Q based on c o s t. / / s t o r e c u t t i n g dimension and node too } i n i t ( ) { r e s u l t. d i s t a n c e = i n f i n i t y ; r e s u l t. p o i n t = n u l l i n s e r t Q (T. r o o t , 0 , 0 ) ; p r o c e s s (Q , r e s u l t ) ; } R e s u l t processQ ( Queue Q , R e s u l t r e s ) { i f ( n u l l (Q) ) return r e s ; int c o s t = Q. e x t r a c t M i n ( ). c o s t ; int cd = Q. e x t r a c t M i n ( ). cd ; KDNode node = Q. d e l e t e M i n ( ) ; i f ( c o s t > r e s. d i s t a n c e ) return r e s ;

... }