Docsity

CSE 331 Lecture 10: Priority Queues (Heaps), Quizzes of Computer Science

Toggle sidebar
bg1
CSE 331, Summer 2002, Lecture #10 (6/7/02)
1. Quiz
2. Priority Queues (Heaps)
(a) Problem: find the minimum or maximum value from an ever-changing list.
(b) Many possible methods: linear search, sort and access, BST, AVL Tree, ...
(c) Specialized structure: a binary heap (aka, a heap): binary tree with two properties:
i. Structural completeness (but not perfection)
ii. Order: any given node must be the lowest (highest) value among all values in its
subtree
(d) Operations: Insert,Delete{Min,Max},BuildHeap,{Increase,Decrease}Key,Delete
(e) Insert
i. First makes a new node without violating the completeness property – picks the last
in array (if linked list – maintain pointer to last)
ii. Next we recursively bump (“percolate”) the node up until it is larger (smaller) than
its parent.
Average case: O(1) (??); worst case: O(log N).
(f) Delete{Min,Max}
i. First picks/returns the top node.
ii. Then recursively bumps (“percolates”) it down the side of the lowest (highest) value
until there are no more subtrees.
iii. Swaps value in “last” position with newly freed spot.
Average case: O(log N). Worst case: O(log N).
(g) BuildHeap
i. Insert values in any order.
ii. Starting from bottom internal node (parent of “last” node), percolate each down (if
possible).
iii. Continue moving across (visually right-to-left, bottom-to-top) until root.
Average case: O(N); worst case: O(Nlog N).
(h) {Increase,Decrease}Key (i.e., change its priority)
i. Change the key’s value and then percolate node down (up).
Worst case: O(log N).
(i) Delete
i. First IncreaseKey to ∞and then DeleteMax (or DecreaseKey to −∞ and then
DeleteMin).
Worst case: O(log N).
3. Applications? Selection Problem, OS/Printing/etc. Scheduling, Sorting ...
4. d-Heaps – generalization of binary heaps.
pf2

Partial preview of the text

Download CSE 331 Lecture 10: Priority Queues (Heaps) and more Quizzes Computer Science in PDF only on Docsity!

CSE 331, Summer 2002, Lecture #10 (6/7/02)

  1. Quiz
  2. Priority Queues (Heaps)

(a) Problem: find the minimum or maximum value from an ever-changing list. (b) Many possible methods: linear search, sort and access, BST, AVL Tree, ... (c) Specialized structure: a binary heap (aka, a heap): binary tree with two properties: i. Structural completeness (but not perfection) ii. Order: any given node must be the lowest (highest) value among all values in its subtree (d) Operations: Insert, Delete{Min,Max}, BuildHeap, {Increase,Decrease}Key, Delete (e) Insert i. First makes a new node without violating the completeness property – picks the last in array (if linked list – maintain pointer to last) ii. Next we recursively bump (“percolate”) the node up until it is larger (smaller) than its parent. Average case: O(1) (??); worst case: O(log N ). (f) Delete{Min,Max} i. First picks/returns the top node. ii. Then recursively bumps (“percolates”) it down the side of the lowest (highest) value until there are no more subtrees. iii. Swaps value in “last” position with newly freed spot. Average case: O(log N ). Worst case: O(log N ). (g) BuildHeap i. Insert values in any order. ii. Starting from bottom internal node (parent of “last” node), percolate each down (if possible). iii. Continue moving across (visually right-to-left, bottom-to-top) until root. Average case: O(N ); worst case: O(N log N ). (h) {Increase,Decrease}Key (i.e., change its priority) i. Change the key’s value and then percolate node down (up). Worst case: O(log N ). (i) Delete i. First IncreaseKey to ∞ and then DeleteMax (or DecreaseKey to −∞ and then DeleteMin). Worst case: O(log N ).

  1. Applications? Selection Problem, OS/Printing/etc. Scheduling, Sorting ...
  2. d-Heaps – generalization of binary heaps.

(a) Binary heap is a 2-heap; in a d-heap, nodes have d children; highest (lowest) is selected between d children.

  1. Binomial Queue

(a) d-Heaps are great; why do we need another kind of queue? (b) How do we merge two priority queues with a d-heap? Expensive. (c) Binomial Queue is not a tree, but a forest – a collection of trees, on each of which heap order is maintained. (d) One extra restriction any given tree in forest must have 2k^ nodes where k is unique among trees. (e) Let Bk be a tree with 2k^ nodes; B 0 has one node; B 1 has two nodes (or two B 0 compo- nents); B 2 has four nodes (or two B 1 components); ... (f) In this way, we can represent a forest having any given number of nodes. (Think binary radix: 11010 2 = 26 10 , meaning one B 1 tree, one B 3 and one B 4 tree; 2 + 8 + 16 = 26.) (g) Most (relevant) heap operations are supported. (h) Merge is the fundamental operation of a binomial queue – two binomial queues are merged by successively merging in member trees of one into the other. A binomial queue tree can be merged with a binomial queue merged as follows: i. Let Bk be the binomial queue to be inserted. ii. If a Bk already exists in the binomial queue, combine the two Bk trees into a B 2 k tree; if another B 2 k tree exists, recursively repeat. Worst case: O(1). (i) Insert i. Insert is a merge of one node. Average case: O(log N ). (j) Delete{Min,Max} i. Scan for the lowest (highest) root node in the forest. ii. Remove it. iii. Merge forest formed by subtrees of the removed node into rest of forest. Average case: O(log N ). (k) BuildHeap i. Implemented by successive inserts. Average case: N log N.