/
1 CSE 332: Data Structures 1 CSE 332: Data Structures

1 CSE 332: Data Structures - PowerPoint Presentation

mitsue-stanley
mitsue-stanley . @mitsue-stanley
Follow
343 views
Uploaded On 2019-11-08

1 CSE 332: Data Structures - PPT Presentation

1 CSE 332 Data Structures Priority Queues Binary Heaps Richard Anderson Spring 2016 2 Recall Queues FIFO FirstIn FirstOut Print jobs File serving Phone calls and operators Lines at the Department of Licensing ID: 764751

binary heap priority insert heap binary insert priority node hole tree order log percolate deletemin queue find array worst

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "1 CSE 332: Data Structures" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1 CSE 332: Data StructuresPriority Queues – Binary Heaps Richard Anderson Spring 2016

2 Recall QueuesFIFO: First-In, First-OutPrint jobs File serving Phone calls and operators Lines at the Department of Licensing…

3 Priority QueuesPrioritize who goes first – a priority queue : treat ER patients in order of severity route network packets in order of urgency operating system can favor jobs of shorter duration or those tagged as having higher importance Greedy optimization: “ best first ” problem solving

4 Priority Queue ADTNeed a new ADT Operations: Insert an Item, Remove the “Best” Item insert deleteMin 6 2 15 23 12 18 45 3 7

5 4/1/2016 5 Priority Queue ADT PQueue data : collection of data with priority PQueue operations insert deleteMin (also: create, destroy, is_empty) PQueue property: if x has lower priority than y, x will be deleted before y

6 6 Potential implementations insert deleteMin Unsorted list (Array) Unsorted list (Linked-List) Sorted list (Array) Sorted list (Linked-List) Binary Search Tree (BST) O(1)/ O(N) array full, O(1) O(N) – to find value O(N) – to find value O(log N) to find, O(N) to shift O(1) to find, O(N) to move, O(1) if reverse order O(1) O(N) to find loc, O(1) to do the insert O(N) O(N) Binary Heap O(log N) close to O(1) 1.67 levels on average O(log N) Plus – good memory usage Worst case

7 Binary Heap data structurebinary heap (a kind of binary tree) for priority queues: O(log n) worst case for both insert and deleteMin O(1) average insert It’s optimized for priority queues. Lousy for other types of operations (e.g., searching, sorting)

8 Tree Review A E B D F C G I H L J M K N root (T): A leaves (T): D-F, I-N children (B): D-F parent (H): G siblings (E): D,F ancestors (F): descendants (G ): subtree (C): Tree T

9 More Tree Terminology A E B D F C G I H L J M K N depth (B): height (G): height (T): degree (B): branching factor (T): n- ary tree : Tree T # edges # edges on path from root to self # edges on longest path from node to leaf # children of a node MAX # children of a node for a BST this is??

10 Binary Heap PropertiesA binary heap is a binary tree with two important properties that make it a good choice for priority queues: Completeness Heap Order Note: we will sometimes refer to a binary heap as simply a “heap”.

11 Completeness PropertyA binary heap is a complete binary tree: a binary tree with all levels full, except possibly the bottom level, which is filled left to right Examples : Height of a complete binary tree with n nodes?

12 Heap Order Property Heap order property : For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. 20 30 80 15 10 99 60 40 80 20 10 50 700 85 This is a PARTIAL order (diff than BST) For each node, its value is less than all of its descendants (no distinction between left and right) This is the order for a MIN heap – could do the same for a max heap.

13 Heap OperationsMain ops: insert, deleteMinKey is to maintain Completeness Heap Order Basic idea is to propagate changes up/down the tree, fixing order as we go

14 Heap – insert(val)Basic Idea: Put val at last leaf position Percolate up by repeatedly exchanging node with parent as long as needed How long does this take? How long does this take? – max # of exchanges = O(log N) On “average” only need to move up 1.67 levels so get O(1)

15 Insert: percolate up 99 60 40 80 20 10 50 700 85 65 15 99 20 40 80 15 10 50 700 85 65 60 Now insert 90. (no swaps, even though 99 is larger!) Now insert 7. Optimization, bubble up an empty space to reduce # of swaps

16 Heap – deleteMinBasic Idea: Remove min element Put “last” leaf node value at root Find smallest child of node Swap node with its smallest child if needed. Repeat steps 3 & 4 until no swaps needed. Why last?

17 DeleteMin: percolate down 99 60 40 15 20 10 50 700 85 65 T worst = O(log N), T avg ? O(log N), since you usually need to percolate all the way down… - Could also percolate empty bubble down

18 DeleteMin: percolate down 99 60 40 65 20 15 50 700 85

19 Representing Complete Binary Trees in an Array G E D C B A J K H I F L From node i : left child: right child: parent: 7 1 2 3 4 5 6 9 8 10 11 12 A B C D E F G H I J K L 0 1 2 3 4 5 6 7 8 9 10 11 12 13 2 * i (2 * i)+1 └ i / 2 ┘

20 Why use an array? Less space *2 and /2,+ are usually faster operations than dereferencing a pointer An array MAY get better locality in general than a linked structure. Can get to the parent easily (and without an extra pointer)

21 DeleteMin Code Object deleteMin() { assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size + 1]); Heap[newPos] = Heap[size + 1]; return returnVal; } int percolateDown(int hole, Object val) {while (2*hole <= size) { left = 2*hole; right = left + 1; if (right ≤ size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole;}runtime:Θ(log n) worst/ave case (Java code in book)

22 Insert Codevoid insert(Object o) { assert(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole;} runtime: Θ(log n) worst case~ 1.67 = Θ(1) on average: only have to go up a few levels(Java code in book)

23 0 1 2 3 4 5 6 7 8 Insert: 16, 32, 4, 69, 105, 43, 2

24 More Priority Queue Operations decreaseKey(nodePtr, amount): given a pointer to a node in the queue, reduce its priority Binary heap: change priority of node and ________________ increaseKey( nodePtr , amount): given a pointer to a node in the queue, increase its priority Binary heap: change priority of node and ________________ Why do we need a pointer ? Why not simply data value? Worst case running times? percolateUppercolateDownIt’s hard to find in a pQ

25 More Priority Queue Operations remove( objPtr ): given a pointer to an object in the queue, remove it Binary heap: ______________________________________ findMax( ): Find the object with the highest value in the queue Binary heap: ______________________________________ Worst case running times? Search leaves, O(n) decreasePriority(objPtr, ∞), deleteMin O(logn)

26 More Binary Heap Operations expandHeap( ): If heap has used up array, copy to new, larger array. Running time: buildHeap(objList): Given list of objects with priorities, fill the heap. Running time: We do better with buildHeap ... Call insert n times Θ (n log n) worst, Θ(n) average

27 Building a Heap: Take 1 5 11 3 10 6 9 4 8 1 7 2 12

28 BuildHeap: Floyd’s Method Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 2 7 1 8 4 9 6 10 3 11 5 12 Red nodes need to percolate down Key idea : fix red nodes from bottom-up 5 11 3 10 6 9 4 8 1 7 2 12

29 BuildHeap: Floyd’s Method 6 7 1 8 4 9 2 10 3 11 5 12 6 7 10 8 4 9 2 1 3 11 5 12 11 7 10 8 4 9 6 1 3 2 5 12 11 7 10 8 4 9 6 5 3 2 1 12

30 Finally… 11 7 10 8 12 9 6 5 4 2 3 1 - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof.

31 Buildheap pseudocodeprivate void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime:

32 Buildheap Analysisn/4 nodes percolate at most 1 level n/8 percolate at most 2 levels n/16 percolate at most 3 levels ... runtime: