CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap,...

20
CS 310: Heapify and Heap Sort Chris Kauffman Week 15-1

Transcript of CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap,...

Page 1: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

CS 310: Heapify and Heap Sort

Chris Kauffman

Week 15-1

Page 2: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Logistics

ScheduleI Mon 12/7: Heapify and

Heap SortI Wed 12/9: Review, EvalsI Fri 12/11: Codefest!I Sat 12/12: HW 4 DueI Mon 12/14: Final Exams

Goals Today

I HW4 DiscussionI Heapsort

Reading: Weiss21: Priority Queue/Binary Heap

Page 3: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

HW 4: TripleStore

I add(e,r,p): O(logN)

I query(e,r,p): O(K + logN)

I remove(e,r,p): O(K × logN)

whereI N: number of records in DBI K : number of matching records

Note:I Correctness is easy for these methodsI Correctness + Target Complexity is trickier

Page 4: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Binary Heaps

I How are they different from binary trees?I What are the efficient operations a binary heap supports?I How efficient are they?I What is a common use of binary heaps?

I Hint: facilitates cutting in line. . .

Page 5: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Operations for Heaps

// Binary Heap, 1-indexedpublic class BinaryHeapPQ<T>{

private T [] array;private int size;

// Helpersstatic int root(){

return 1;}static int left(int i){

return i*2;}static int right(int i){

return i*2+1;}static int parent(int i){

return i / 2;}

// Insert a datapublic void insert(T x){

size++;ensureCapacity(size+1);array[size] = x;percolateUp(size);

}// Remove the minimum elementpublic void deleteMin(){

array[root()] = array[size];array[size] = null;size--;percolateDown(root());

}

Page 6: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Percolate Up/Down

Up

void percolateUp(int xdx){while(xdx!=root()){

T x = array[xdx];T p = array[parent(xdx)];if(doCompare(x,p) < 0){

array[xdx] = p;array[parent(xdx)] = x;xdx = parent(xdx);

}else{ break; }

}}

Down

void percolateDown(int xdx){while(true){

T x = array[xdx];int cdx = left(xdx);// Determine which child// if any to swap withif(cdx > size){ break; } // No left, bottomif(right(xdx) < size && // Right valid

doCompare(array[right(xdx)], array[cdx]) < 0){cdx = right(xdx); // Right smaller

}T child = array[cdx];if(doCompare(child,x) < 0){ // child smaller

array[cdx] = x; // swaparray[xdx] = child;xdx = cdx; // reset index

}else{ break; }

}}

Page 7: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Height Again. . .

Efficiency of Binary Heap PQs

findMin() clearly O(1)deleteMin() worst case heightinsert(x) worst case height

Complete Binary Tree

I Min number of nodes in a complete tree of height H?I Take a crack at itI Derive a rough formula

Page 8: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

This is how rumors get started

Weiss’s Assertion: insert is O(1)On average the percolation [up] terminates early: It has been shown that 2.6comparisons are required on average to perform the add, so the average addmoves an element up 1.6 levels.

I Weiss 4th ed pg 815

Precise results on the number of comparisons and data movements used byheapsort in the best, worst, and average case are given in (Schaffer andSedgewick).

I pg 839

R. Schaffer and R. Sedgewick, "The Analysis of Heapsort," Journal ofAlgorithms 14 (1993), 76–100.

Page 9: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

And how rumors resolve

Binary Heaps DO have O(1) amortized insertion

Empirical Evaluation Mathematical ProofI Schaffer/Sedgewick (1991)I → Carlsson (1987)I → Porter/Simon (1974):

"note pg. 15 in which theaverage case is stated to beupper-bound by lambda,which computes to1.606691524"

Students in Fall 2013 did the above, edited Wikipedia to reflect thisfact. You should have been there. It was awesome.

Page 10: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Binary Heaps

Great for building Priority Queues:

Op Worst Case Avg CasefindMin() O(1) O(1)insert(x) O(logN) O(1)deleteMin() O(logN) O(logN)

What else can one do with binary heaps?

Page 11: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Exercise: Sort data using a Binary Heaps/PQ

I Sort an array of stuff with a PQ/Binary heapI Define the following method

// Sort an array in place using a priority queuepublic static <T extends Comparable>void heapSort(T data[]){ ... }

I T is ComparableI data unsortedI return: nothing, but data must be sortedI Use pq.insert(x) and pq.deleteMin()

Page 12: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Out of Place Sort Sucks

Initial solution required data duplicationI Copy from data to pq, then backI Out of place sorting, double memory requirement

For large arrays this hurtsI Want truly in place sortingI Don’t make copy, avoid O(N) space overheadI Suggest a solution: no duplication possible?

Page 13: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

In-Place Sorting with Heaps: Three Problems

1. 1-indexing of heaps

I Not a problem: heap root at index 0I Formulas for left(i), right(i), parent(i)?

2. Broken Heap

I Can’t start with an empty heapI Start with unsorted arrayI Heap property doesn’t holdI How to fix it?

3. Pulling Stuff Out

I Have a heap, repeatedly pull out minI Where to put it?

Page 14: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Heapify, a.k.a. buildHeap()

Converts an existing array into a heap (!)

public void buildHeap( ) {for( int i = parent(this.size); i >= root(); i-- ){

this.percolateDown( i );}

}

Build the heap bottom up, repeated percolateDown(i)I Start one level above bottom (where for size N heap?)I Work right to left, low to highI If small guy is down low, will bubble up

Page 15: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Heapify Example

Level 3: Initial, percolateDown([63])

Level 3: percolateDown([45]), percolateDown([12]),

Page 16: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Heapify Example

Level 3: percolateDown([20]), Level 2: percolateDown([21]),

Level 2: percolateDown([47]), Level 1: percolateDown([92]),

Page 17: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Build me a Heap as Quick as You Can

How complex is Heapify / buildHeap()?I Discuss with a neighbor

TryI How many small moves versus big moves?I Derive an expression for the worst possible number of moves

Page 18: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Complexity of HeapifyHeap size n, height h, assume complete (?)Measure level from bottom

I Level 1 is bottom, has 2h-1 nodes,I Level 2 is second from bottom, has 2h-2 nodesI Level i is ith form bottom, has 2h-i nodesI Level h is root, has 2h-h = 1 node

Each level i node can move i down so

moves =h∑

i=1

i × 2h−i =

log2 n∑i=1

i × 2log2 n−i

=

log2 n∑i=1

i × 2log2 n

2i = nlog2 n∑i=1

i2i

≤ n × 2 = O(n)

because∞∑i=1

i2i → 2

Page 19: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

In Place Heap Sort

After heapifying an array. . .

Space Available

I Remove an element from a heapI Now open space at end of array (percolate down)I Put the removed element at end of arrayI Repeat until empty

Min and Max HeapAbove process orders the array

I For Min-Heap will result in biggest to smallestI For Max-Heap will result in smallest to biggest

Page 20: CS 310: Heapify and Heap Sortkauffman/cs310/w15-1.pdf · Operations for Heaps // Binary Heap, 1-indexed public class BinaryHeapPQ{private T [] array; private int size;

Summary of Heap Sort

Input: Array aOutput: a is sorted

Build Max Heap on afor i=0 to length-1 a

tmp = findMax(a)removeMax(a)a[length-i-1] = tmp

done