The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Priority Queues Next: Suffix Trees and Arrays Up: Data Structures Previous: Dictionaries Priority Queues Input description: A set of records with numerically or otherwise totally ordered keys. Problem description: Build and maintain a data structure for quickly inserting and deleting records, while enabling quick access to the smallest or largest key in the set. Discussion: Priority queues are useful data structures in simulations, particularly for maintaining a set of future events ordered by time so that we can quickly retrieve what the next thing to happen is. They are called ``priority'' queues because they enable you to retrieve items not by the insertion time (as in a stack or queue), nor by a key match (as in a dictionary), but by which item has the highest priority of retrieval. If your application performs no insertions after the first query, there is no need for an explicit priority queue. Simply sort the records by priority and proceed from top to bottom, maintaining a pointer to the last record deleted. This situation occurs in Kruskal's minimum spanning tree algorithm, or when simulating a completely scripted set of events. However, if you are mixing insertions, deletions, and queries, you need a real priority queue. The following questions will help select the right one: file:///E|/BOOK/BOOK3/NODE130.HTM (1 of 4) [19/1/2003 1:30:05]
Priority Queues ● Besides access to the smallest element, what other operations will you need? - Will you be searching for arbitrary keys, or just searching for the smallest? Will you be deleting arbitrary elements from the data, or just repeatedly deleting the top or smallest element? ● Will you know the maximum size of your data structure in advance, or might an arbitrary number of items be inserted into it? - The issue here is whether you can preallocate space for the data structure. ● Will you be changing the priority of elements already in the queue, or simply inserting and removing them? - Changing the priority of elements implies that we must be able to look up elements in the queue based on their key, in addition to being able to retrieve the largest element. Depending upon the answers, you have the following basic priority queue choices: ● Sorted array or list - In a sorted array, it is very efficient to find and (by decrementing the top index) delete the smallest element. However, maintaining sortedness makes the insertion of new elements slow. Sorted arrays are suitable when there will be few insertions into the priority queue. ● Binary heaps - This simple, elegant data structure supports both insertion and extract-min in time each. Heaps maintain an implicit binary tree structure in an array, such that the key of the root of any subtree is less than that of all its descendents. Thus the minimum key is always at the root of the heap. New keys can be inserted by placing them at an open leaf and percolating the element upwards until it sits at its proper place in the partial order. Binary heaps are the right answer whenever you know an upper bound on the number of items in your priority queue, since you must specify the array size at creation time. ● Bounded height priority queue - This array-based data structure permits constant-time insertion and find-min operations whenever the range of possible key values is limited. Suppose we know that all key values will be integers between 1 and n. We can set up an array of n linked lists, such that the ith list serves as a bucket containing all items with key i. We will maintain a pointer top to the smallest nonempty list. To insert an item with key k into the priority queue, add it to the kth bucket and set . To extract the minimum, report the first item from bucket top, delete it, and move top down if the bucket is now empty. Bounded height priority queues are very useful in maintaining the vertices of a graph sorted by degree, which is a fundamental operation in graph algorithms. Still, they are not as widely known as they should be. They are usually the right priority queue for any small, discrete range of keys. ● Binary search trees - Binary search trees make effective priority queues, since the smallest element is always the leftmost leaf, while the largest element is always the rightmost leaf. The min (max) is found by simply tracing down left (right) pointers until the next pointer is nil. Binary tree heaps prove most appropriate when you also need to search a dictionary of the values, or if you have an unbounded key range and do not know the maximum priority queue size in advance. ● Fibonacci and pairing heaps - These complicated priority queues are designed to speed up decrease-key operations, where the priority of an item already in the priority queue is reduced. file:///E|/BOOK/BOOK3/NODE130.HTM (2 of 4) [19/1/2003 1:30:05]
- Page 301 and 302: War Story: Going Nowhere Fast Next:
- Page 303 and 304: Exercises Next: Implementation Chal
- Page 305 and 306: Problems and Reductions Next: Simpl
- Page 307 and 308: Simple Reductions Next: Hamiltonian
- Page 309 and 310: Hamiltonian Cycles Next: Independen
- Page 311 and 312: Independent Set and Vertex Cover pr
- Page 313 and 314: Clique and Independent Set These la
- Page 315 and 316: Satisfiability Mon Jun 2 23:33:50 E
- Page 317 and 318: The Theory of NP-Completeness Next:
- Page 319 and 320: 3-Satisfiability where for , , , an
- Page 321 and 322: Integer Programming Next: Vertex Co
- Page 323 and 324: Integer Programming possible IP ins
- Page 325 and 326: Vertex Cover reduction for the 3-SA
- Page 327 and 328: Other NP-Complete Problems hard. Th
- Page 329 and 330: The Art of Proving Hardness easiest
- Page 331 and 332: War Story: Hard Against the Clock N
- Page 333 and 334: War Story: Hard Against the Clock I
- Page 335 and 336: Approximation Algorithms Next: Appr
- Page 337 and 338: Approximating Vertex Cover Next: Th
- Page 339 and 340: The Euclidean Traveling Salesman Ne
- Page 341 and 342: The Euclidean Traveling Salesman Ne
- Page 343 and 344: Exercises 1. Prove that the low deg
- Page 345 and 346: Data Structures Next: Dictionaries
- Page 347 and 348: Dictionaries Next: Priority Queues
- Page 349 and 350: Dictionaries use a function that ma
- Page 351: Dictionaries Implementation-oriente
- Page 355 and 356: Priority Queues Fibonacci heaps [FT
- Page 357 and 358: Suffix Trees and Arrays Figure: A t
- Page 359 and 360: Suffix Trees and Arrays [GBY91]. Se
- Page 361 and 362: Graph Data Structures algorithms).
- Page 363 and 364: Graph Data Structures including the
- Page 365 and 366: Set Data Structures Next: Kd-Trees
- Page 367 and 368: Set Data Structures parent pointers
- Page 369 and 370: Kd-Trees Next: Numerical Problems U
- Page 371 and 372: Kd-Trees about p. Say we are lookin
- Page 373 and 374: Numerical Problems Next: Solving Li
- Page 375 and 376: Numerical Problems Mon Jun 2 23:33:
- Page 377 and 378: Solving Linear Equations algorithm
- Page 379 and 380: Solving Linear Equations Matrix inv
- Page 381 and 382: Bandwidth Reduction images near eac
- Page 383 and 384: Matrix Multiplication Next: Determi
- Page 385 and 386: Matrix Multiplication The linear al
- Page 387 and 388: Determinants and Permanents Next: C
- Page 389 and 390: Determinants and Permanents exposit
- Page 391 and 392: Constrained and Unconstrained Optim
- Page 393 and 394: Constrained and Unconstrained Optim
- Page 395 and 396: Linear Programming variable assignm
- Page 397 and 398: Linear Programming The book [MW93]
- Page 399 and 400: Random Number Generation Next: Fact
- Page 401 and 402: Random Number Generation is largely
Priority Queues<br />
● Besides access to the smallest element, what other operations will you need? - Will you be<br />
searching for arbitrary keys, or just searching for the smallest? Will you be deleting arbitrary<br />
elements from the data, or just repeatedly deleting the top or smallest element?<br />
● Will you know the maximum size of your data structure in advance, or might an arbitrary number<br />
of items be inserted into it? - <strong>The</strong> issue here is whether you can preallocate space for the data<br />
structure.<br />
● Will you be changing the priority of elements already in the queue, or simply inserting and<br />
removing them? - Changing the priority of elements implies that we must be able to look up<br />
elements in the queue based on their key, in addition to being able to retrieve the largest element.<br />
Depending upon the answers, you have the following basic priority queue choices:<br />
● Sorted array or list - In a sorted array, it is very efficient to find and (by decrementing the top<br />
index) delete the smallest element. However, maintaining sortedness makes the insertion of new<br />
elements slow. Sorted arrays are suitable when there will be few insertions into the priority queue.<br />
● Binary heaps - This simple, elegant data structure supports both insertion and extract-min in<br />
time each. Heaps maintain an implicit binary tree structure in an array, such that the<br />
key of the root of any subtree is less than that of all its descendents. Thus the minimum key is<br />
always at the root of the heap. New keys can be inserted by placing them at an open leaf and<br />
percolating the element upwards until it sits at its proper place in the partial order.<br />
Binary heaps are the right answer whenever you know an upper bound on the number of items in<br />
your priority queue, since you must specify the array size at creation time.<br />
● Bounded height priority queue - This array-based data structure permits constant-time insertion<br />
and find-min operations whenever the range of possible key values is limited. Suppose we know<br />
that all key values will be integers between 1 and n. We can set up an array of n linked lists, such<br />
that the ith list serves as a bucket containing all items with key i. We will maintain a pointer top to<br />
the smallest nonempty list. To insert an item with key k into the priority queue, add it to the kth<br />
bucket and set . To extract the minimum, report the first item from bucket top,<br />
delete it, and move top down if the bucket is now empty.<br />
Bounded height priority queues are very useful in maintaining the vertices of a graph sorted by<br />
degree, which is a fundamental operation in graph algorithms. Still, they are not as widely<br />
known as they should be. <strong>The</strong>y are usually the right priority queue for any small, discrete range of<br />
keys.<br />
● Binary search trees - Binary search trees make effective priority queues, since the smallest<br />
element is always the leftmost leaf, while the largest element is always the rightmost leaf. <strong>The</strong><br />
min (max) is found by simply tracing down left (right) pointers until the next pointer is nil. Binary<br />
tree heaps prove most appropriate when you also need to search a dictionary of the values, or if<br />
you have an unbounded key range and do not know the maximum priority queue size in advance.<br />
● Fibonacci and pairing heaps - <strong>The</strong>se complicated priority queues are designed to speed up<br />
decrease-key operations, where the priority of an item already in the priority queue is reduced.<br />
file:///E|/BOOK/BOOK3/NODE130.HTM (2 of 4) [19/1/2003 1:30:05]