Heaps and Heapsort: Understanding Heap Property, Heapify, BuildHeap, and Priority Queues, Slides of Computer Science

An in-depth exploration of heaps, a data structure used in algorithms like heapsort and for implementing priority queues. Topics covered include the heap property, heapify() and buildheap() functions, and their applications in heapsort and priority queues. Real-world examples are also provided to illustrate the concepts.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Heapsort
Priority Queues
Quicksort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Heaps and Heapsort: Understanding Heap Property, Heapify, BuildHeap, and Priority Queues and more Slides Computer Science in PDF only on Docsity!

Algorithms

Heapsort Priority Queues Quicksort

Review: Heaps

 A heap is a “complete” binary tree, usually represented as an array: 16 4 10 14 7 9 3 2 8 1

A = 16 14 10 8 7 9 3 2 4 1

Review: The Heap Property

 Heaps also satisfy the heap property :

A[ Parent ( i )] ≥ A[ i ] for all nodes i > 1  In other words, the value of a node is at most the value of its parent  The largest value is thus stored at the root (A[1])

 Because the heap is a binary tree, the height of any node is at most Θ(lg n )

Review: Heapify()

Heapify() : maintain the heap property

 Given: a node i in the heap with children l and r  Given: two subtrees rooted at l and r , assumed to be heaps  Action: let the value of the parent node “float down” so subtree at i satisfies the heap property  If A[i] < A[l] or A[i] < A[r], swap A[i] with the largest of A[l] and A[r]  Recurse on that subtree  Running time: O( h ), h = height of heap = O(lg n )

BuildHeap()

// given an unsorted array A, make A a heap BuildHeap(A) { heap_size(A) = length(A); for (i =length[A]/2downto 1) Heapify(A, i); }

Analyzing BuildHeap()

 Each call to Heapify() takes O(lg n ) time

 There are O( n ) such calls (specifically, n/2)

 Thus the running time is O( n lg n )

Is this a correct asymptotic upper bound?Is this an asymptotically tight bound?

 A tighter bound is O (n )

How can this be? Is there a flaw in the above reasoning?

Heapsort

 Given BuildHeap() , an in-place sorting

algorithm is easily constructed:  Maximum element is at A[1]  Discard by swapping with element at A[n]  Decrement heap_size[A]  A[n] now contains correct value  Restore heap property at A[1] by calling Heapify()  Repeat, always swapping A[1] for A[heap_size(A)]

Heapsort

Heapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }

Priority Queues

 Heapsort is a nice algorithm, but in practice Quicksort (coming up) usually wins

 But the heap data structure is incredibly useful for implementing priority queues  A data structure for maintaining a set S of elements, each with an associated value or key  Supports the operations Insert() , Maximum() , and ExtractMax()What might a priority queue be useful for?

Priority Queue Operations

Insert(S, x) inserts the element x into set S

Maximum(S) returns the element of S with the maximum key

ExtractMax(S) removes and returns the element of S with the maximum key

How could we implement these operations using a heap?

Tying It Into The “Real World”

 And now, a real-world example… combat billiards  Sort of like pool...  Except you’re trying to kill the other players…  And the table is the size of a polo field…  And the balls are the size of Suburbans...  And instead of a cue you drive a vehicle with a ram on it  Problem: how do you simulate the physics?

Figure 1: boring traditional pool

Combat Billiards:

Simulating The Physics

 Simplifying assumptions:

 G-rated version: No players  Just n balls bouncing around  No spin, no friction  Easy to calculate the positions of the balls at time Tn from time Tn-1 if there are no collisions in between  Simple elastic collisions

Implementing Priority Queues

HeapInsert(A, key) // what’s running time? { heap_size[A] ++; i = heap_size[A]; while (i > 1 AND A[Parent(i)] < key) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }

Implementing Priority Queues

HeapMaximum(A) { // This one is really tricky:

return A[i]; }