pq, binary heaps g.kamberova, algorithms priority queue adt binary heaps gerda kamberova department...

22
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

Post on 22-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Priority Queue ADTBinary Heaps

Gerda KamberovaDepartment of Computer Science

Hofstra University

Page 2: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Overview

• Priority Queue (PQ) ADT definition.

• Applications

• Implementation - binary heaps (heaps).

• Complexity analysis

Page 3: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Priority Queue ADT

• Priority Queue (PQ) ADT is a dynamic set – with keys viewed as priorities– The keys could be compared, so we can identify the

element with highest priority– supports

• insert• delete element with highest priority• retrieve element with highest priority (without

delete)• Depending on what, min or max, is selected as highest priority, we

distinguish between min- and max-prioirity queues Historically, PQ means min-PQ.

• If x is a reference to a record, the operations on priority queue S are:– For max-priority queue: Max(S), DeleteMax(S), Insert(S,x)– For min-priority queue: Min(S), DeleteMin(S), Insert(S,x)

Page 4: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Applications

• Job scheduling on a multiuser system– Queue (FIFO). Not a good choice.– PQ:

• greedy algorithm: let the smallest job run first.• Keep a PQ of incoming jobs and efficiently retrieve

the highest priority job to run.• Minimizes avg time to wait

• Event-driven discrete-event system simulation: most prevalent application of PQ is in

Page 5: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

PQ ADT Implementation

• Linked list (sorted or unsorted)?• Array sorted or unsorted?• Binary search tree (BST)?

– O(log n), – overkill, supports all dynamic set operations

• Heap– no linked lists– better suited than BST for PQ – Implementation, internally the structure is an array

• O(log n) in worst case.• Insert is O(1) on average.• Building PQ is in linear time, O(n)

Page 6: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Binary Heap (Heap)

• Heap (binary heap) is a binary tree (BT) in which the following properties are satisfied:

– HS [the heap structure property or heap shape]: the BT is completely filled at all levels except possibly on the very last, where it is filled from left to right

– PODR[ the partial order heap order property]: the key value stored at any non-leaf node is >= the key values of its children.

– no particular

• Note:– order between key values on the same level.– Max is at the root– when you go from a leaf, up a direct path, to the root, the

keys are encountered in non-decreasing order.– the operations insert and delete may destroy the heap

properties, so these operations should not terminate before restoring the heap properties

Page 7: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

BT Representation of a Max Heap

• Check HS and PORD properties

14

8

2 4

7

10

1

9 3

16

Page 8: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Implementation of a Heap

• since a heap is almost a complete BT, it is easily stored in an array (no need to store pointers):

14

8

2 4

7

10

1

9 3

16

16 14 10 8 7 9 3 2 4 1 …

Page 9: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Implementation of a Heap

( ) / 2

( ) 2

( ) 2 1

PORD: [ ( )] [ ]

Parent i i

Left i i

Right i i

A Parent i A i

14

8

2 4

7

10

1

9 3

16

16 14 10 8 7 9 3 2 4 1 …1 2 3 4 5 6 7 8 9 10i

Read left to right, filltop to bottomleft to right

Read top to bottom, left to right, fill left to right, startat index 1

Nodes are labeled left-to-right in level order (root first,the right-most leaf last), and this order defines the array indices.

1i

Page 10: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Implementation of the PQ operations with heaps

• Retrieve the maximum– Easy, just return the root–

• Insert/DeleteMax– more complicated. – Must preserve HS and PORD properties.

• Insert:– Input: a heap A of size n and an element referenced by x

with key k to be inserted.– Output: A heap A of size n+1.– Where to place the new node?

• not much choice, must preserve HS.• create a new node at index n+1• If we place the new key at that place the PROD may be

destroyed.• restore PORD by sifting up the new node until it falls in

place i.e. where the new key will be in place

(1)

Page 11: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Insertion in a max heap: Example

• Insert element with key 15 in the heap

• Create a new node as most right leaf in tree

• Propagate “hole” up until it falls in place

14

8

2 4

7

10

1

9 3

16

15 1

( 1& [ ( )] 15)

[ ] [ [ ]]

[ ]

i n

while i A Parent i

A i A Parent i

i Parent i

7

14

Page 12: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Insertion in a heap

• Implementation– Create the (n+1)-st node, and sift the key upwards

Heap_Insert(A, key, n) n = n+1 i = n // find the spot for the new key while (i > 1 && A(Parent[i]) < key ) A[i] = A[Parent(i)] i = Parent(i) A[i] = key

• Complexity: ( ) ( ), is the heap height

lg , thus

( ) (log )

T n O h h

h n

T n O n

Page 13: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Delete in a heap

• Input: Heap A of size n;• Output: Heap A of size n-1, the root key value is removed

from the heap (may be returned as well)

• Since the heap HS must be preserved, the output heap is missing the right-most leaf of the input heap.

• The root value must be extracted, and the “hole” at the root must be filled in

• This suggest the following procedure:– copy the key from right-most leaf to root– delete right-most leaf– Restore PORD: heapify

Page 14: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Delete in a heap

Heap_Delete(A, n)

A[1] = A[n] //copy last leaf in root

n = n - 1

Heapify(A,1)

Page 15: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Deletion from the heap

Heap_Delete(A, n) A[1] = A[n] //copy last leaf in root n = n - 1 Heapify(A,1)

• Complexity: O(1)+ time to heapify

14

8

2 4

7

10

1

9 3

161

Page 16: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Heapify

• Input: A rooted tree, such that A[i] is at the root, and the left and the right subtrees (if exist) are heaps.

• Output: The tree is made into a heap.• Procedure:

– Sift down the key value of the root until it falls in place.– a recursive procedure:

• compare key at the root with the keys of its children. • If the root key >= of the children keys, stop,• Otherwise, swap root key with the key of larger child,

and proceed in hipifying now the subtree rooted at the node for which swap occured

Page 17: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Heapify

• Hipify(A,i) : makes the subtree rooted at A[i] into a heap, provided that the trees rooted at A[Left(i)] and A[Right(i)] are heaps.

Hipify(A,i)

if A[i] is smaller than its children

m = index of larger child

swap A[i] and A[m]

Hipify(A,m)

Page 18: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Deletion from the heap: Example

14

8

2 4

7

10

1

9 3

161

Page 19: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Deletion from the heap: Example

• Complexity: if we swap, iteratively, bounded by the heigh• Complexity using recursive heapify?

14

8

2 4

7

10

9 3

1

Page 20: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Complexity of Heapify

• Worst case: at each recursive call the subtree on which the call is made on a complete BT (all leaves at last level exist); the height of that tree must be one more than the height of the other subtree

• In a complete BT the number of leaves is equal half of the nodes

• If the worst-case tree has n nodes, they are distributed as follows

• Recurrence for Heapify

n/3 n/3

n/3

log 1

2( ) , ( log )

3

1, 3 / 2, 0

log 0 , ( ) ( log ) (log )b

l k

a kb

nT n T c c n n

a b k l

a l T n n n n

Page 21: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Constructing a heap

• Top-down: using HeapInsert, – successively insert nodes staring with an empty heap. – Time complexity is O(nlog n)

• Bottom-up: using Heapify– Construct an almost complete binary tree with n

vertices and store the keys arbitrarily there. This is Theta(n)

– Now going bottom up, from the most right node in the last internal level, heapify the trees rooted at the internal nodes.

– This time can be shown to be linear O(n)

Page 22: PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University

PQ, binary heaps G.Kamberova, Algorithms

Heapsort

1. Build a heap: O(n) or O(n log n)

1. Do n times extract_max n times: O(n log n)

• Total time O(n log n)