Priority Queues and Heaps in Computer Science, Study notes of Computer Science

Lecture notes for cs2110 fall 2008, covering priority queues and heaps, including implementation, examples, and complexity analysis. It also includes an introduction to heaps as a data structure for implementing priority queues, and a comparison of their complexity to ordered and unordered list implementations.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-ixa-1
koofers-user-ixa-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 49

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 17
CS2110 Fall 2008
Priority Queues
and Heaps
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

Partial preview of the text

Download Priority Queues and Heaps in Computer Science and more Study notes Computer Science in PDF only on Docsity!

Lecture 17 CS2110 Fall 2008

Priority Queues

and Heaps

Announcements

  • Some changes to the CS major to be

announced soon

  • 2111 no longer required for new majors
  • A&S and Engr students may drop 2111 until

Nov 14 without transcript annotation

  • If this affects you, please see me offline

Stacks and Queues as Lists

  • Stack (LIFO) implemented as list
    • (^) insert() , extract() from front of list
  • Queue (FIFO) implemented as list
    • (^) insert() on back of list, extract() from front of list
  • All^ Bag^ operations are O(1) first 55 120 19 16 last

Priority Queue

  • A^ Bag^ in which data items are^ Comparable
  • lesser elements (as determined by

compareTo() ) have higher priority

  • extract() returns the element with the

highest priority = least in the compareTo()

ordering

  • break ties arbitrarily

java.util.PriorityQueue

boolean add(E e) {...} //insert an element (insert) void clear() {...} //remove all elements E peek() {...} //return min element without removing //(null if empty) E poll() {...} //remove min element (extract) //(null if empty) int size() {...}

Priority Queues as Lists

  • Maintain as unordered list
    • (^) insert() puts new element at front โ€“ O(1)
    • (^) extract() must search the list โ€“ O(n)
  • Maintain as ordered list
    • (^) insert() must search the list โ€“ O(n)
    • (^) extract() gets element at front โ€“ O(1)
  • In either case, O(n 2

) to process n elements

Can we do better?

Heaps

  • A heap is a concrete data structure that can

be used to implement priority queues

  • Gives better complexity than either ordered

or unordered list implementation:

  • (^) insert(): O(log n)
  • (^) extract(): O(log n)
  • O(n log n) to process n elements
  • Do not confuse with heap memory , where

the Java virtual machine allocates space for

objects โ€“ different usage of the word heap

Heaps

โ€ข Binary tree with data at each node

โ€ข Satisfies the Heap Order Invariant :

The least (highest priority)

element of any subtree is found

at the root of that subtree

Examples of Heaps

  • Ages of people in family tree
    • parent is always older than children, but you can have an uncle who is younger than you
  • Salaries of employees of a company
    • bosses generally make more than subordinates, but a VP in one subdivision may make less than a Project Supervisor in a different subdivision

Balanced Heaps

Two restrictions:

1. Any node of depth < d โ€“ 1 has exactly 2

children, where d is the height of the tree

  • implies that any two maximal paths (path from a root to a leaf) are of length d or d โ€“ 1, and the tree has at least 2 d nodes

2. All maximal paths of length d are to the left

of those of length d โ€“ 1

  • Elements of the heap are stored in the array

in order, going across each level from left to

right, top to bottom

  • The children of the node at array index n are

found at 2n + 1 and 2n + 2

  • The parent of node n is found at (n โ€“ 1)/

Store in an ArrayList or Vector

0 1 2 (^3 4 5 ) 7 8 9 10 11 children of node n are found at 2n + 1 and 2n + 2

Store in an ArrayList or Vector

insert()

insert()