Priority queues and heap sorting

35
PRIORITY QUEUES PRIORITY QUEUES Priority Queues Binary Heaps

Transcript of Priority queues and heap sorting

Page 1: Priority queues and heap sorting

PRIORITY QUEUESPRIORITY QUEUESPriority QueuesBinary Heaps

Page 2: Priority queues and heap sorting

2

543210

Page 3: Priority queues and heap sorting

3

Page 4: Priority queues and heap sorting

GROUP MEMBERS 4

Page 5: Priority queues and heap sorting

OBJECTIVES TO BE MET

5

Page 6: Priority queues and heap sorting

6

• Sometimes we want to process items in a specific order.

• Importance is one possible criterion.• Examples:• job scheduling according to their priorities.

Page 7: Priority queues and heap sorting

• A priority queue is an abstract data type, such as queue, stacks.

• In a priority queue each item(entry) has an associated priority and data.

• The main operation(that must be very efficient) is to find the highest priority item (usually the one with the minimum or maximum key value).

• Other common operations are insert and delete.

7

Page 8: Priority queues and heap sorting

• A priority queue is a queue where:• Requests are inserted in the order of arrival• The request with highest priority is processed

first (deleted from the queue)• The priority is indicated by a number, the lower

the number - the higher the priority.

8

Page 9: Priority queues and heap sorting

• The heap is a data structure that satisfies the heap property:

• If the node a is the parent of node b, the key value of a is ordered with respect to the key value of b.

• The above ordering applies to every node a in the tree.

9

Page 10: Priority queues and heap sorting

• A binary heap is a implementation of a heap using a binary tree.

• A binary heap is basically a binary tree with two additional constraints:

• Shape property: A binary heap is a complete ( only the last level may be incomplete). The nodes at the last level do not have children. New nodes are inserted at the last level from left to right.

• Heap property: all nodes respect the heap property.Each node has a higher priority than its children

10

Page 11: Priority queues and heap sorting

• Since the highest priority item is at the root of the binary heap, find min/max has just to return it(the cost is O(1).

11

2

5 8

710 13 18

21 19

Min value at the root in a min heap

Page 12: Priority queues and heap sorting

12

BINARY HEAP

6

1012

15 17 18 23

20 19 34

Next node to be inserted - right child of the yellow node

The highest priority item (in this case the minimum key) is the root of the tree.

Page 13: Priority queues and heap sorting

BINARY HEAP IMPLEMENTATION WITH AN ARRAY

13

Root - A(1)Left Child of A(i) - A(2i)Right child of A(i) - A(2i+1)Parent of A(I) - A([i/2]).

The smallest element is always at the root, the access time to the element with highest priority is constant O(1).

Page 14: Priority queues and heap sorting

EXAMPLE14

6 10 12 15 17 18 23 20 19 34

Consider 17: position in the array - 5.parent 10 is at position [5/2] = 2left child is at position 5*2 = 10 (this is 34)right child - position 2*5 + 1 = 11 (empty.)

Page 15: Priority queues and heap sorting

PROBLEMS 15

2 8 10 16 17 18 23 20 21 30

Reconstruct the binary heap

Page 16: Priority queues and heap sorting

PROBLEMS16

Problem 2: Give the array representation for 3

1016

13 12 18 23

15 19 30 14

Page 17: Priority queues and heap sorting

ALGORITHMMAX-HEAPIFY (A,i){ l=2i;r=2i+1;If(l≤ A.heapsize and A[l]>A[i])Largest=l;elseLargest=i;If(r≤ A.heapsize and A[r]>A[largest])Largest=r;If(largest≠i) exchange A[i] with A[largest]MAX-HEAPIFY(A,largest)}

17

10

39

1

4

7

2

8

14

6

i

l r

Page 18: Priority queues and heap sorting

NOW I WOULD LIKE TO CALL

HAJRA MUSTAFA

18

Page 19: Priority queues and heap sorting

BASIC OPERATIONS• Insert a node – Up heapify• Delete a node–Down heapify• Build the heap

19

Page 20: Priority queues and heap sorting

PERCOLATE UP – INSERT A NODE

20

A hole is created at the bottom of the tree, in the next available position.

6

1012

15 17 18 23

21 19 34

Page 21: Priority queues and heap sorting

PERCOLATE UP21

Insert 20 6

1012

15 17 18 23

21 19 34 20

Page 22: Priority queues and heap sorting

PERCOLATE UP

22

Insert 16

6

1012

15 18 23

21 19 34 17

Page 23: Priority queues and heap sorting

PERCOLATE UP

23

6

1012

15 18 23

21 19 34 17

16

Complexity of insertion: O(logN)

Page 24: Priority queues and heap sorting

PERCOLATE DOWN –

DELETE A NODE

24

1016

13 12 18 23

15 19 30 20

● The deletion has 3 steps:Step 1) Remove the root item.

Page 25: Priority queues and heap sorting

PERCOLATE DOWN

25

Last element - 20. The hole at the root.

1016

12 13 18 23

15 19 30 20

● The deletion has 3 steps:Step 2) Replace the root with the last item, from left to right, in the last level.

Page 26: Priority queues and heap sorting

PERCOLATE DOWN

26

16

12 13 18 23

15 19 3020

10● The deletion has 3 steps:

Step 3) percolate down the root item until the heap property is satisfied.

Page 27: Priority queues and heap sorting

PERCOLATE DOWN

27

16

13 18 23

15 19 3020

10

12

● The deletion has 3 steps:Step 3) percolate down the root item until the heap property is satisfied.

Page 28: Priority queues and heap sorting

PERCOLATE DOWN

28

16

13 18 23

20 19 30

10

12

15

Complexity of deletion: O(logN)

● The deletion has 3 steps:Step 3) percolate down the root item until the heap property is satisfied.

Page 29: Priority queues and heap sorting

BUILD HEAP 29

Given an array of elements to be inserted in the heap,

•Treat the array as a heap with order property violated, •And then do operations to fix the order property.

Page 30: Priority queues and heap sorting

EXAMPLE:150 80 40 30 10 70 110 100 20 90 60 50

120 140 130

30

150

80 40

30 1070 110

100 20 90 60 50 120 140 130

Creating a min heap

Page 31: Priority queues and heap sorting

EXAMPLE (CONT)31

150

80 40

20 1050 110

100 30 90 60 70 120 140 130

After processing height 1

Page 32: Priority queues and heap sorting

EXAMPLE (CONT)32

150

10 40

20 6050 110

100 30 90 80 70 120 140 130

After processing height 2

Page 33: Priority queues and heap sorting

EXAMPLE (CONT)33

10

20 40

30 6050 110

100 150 90 80 70 120 140 130

After processing height 3

Page 34: Priority queues and heap sorting

ANY QUESTIONS34

Page 35: Priority queues and heap sorting

35