Upload
heshekik
View
28
Download
0
Embed Size (px)
Citation preview
PRIORITY QUEUESPRIORITY QUEUESPriority QueuesBinary Heaps
2
543210
3
GROUP MEMBERS 4
OBJECTIVES TO BE MET
5
6
• Sometimes we want to process items in a specific order.
• Importance is one possible criterion.• Examples:• job scheduling according to their priorities.
• 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
• 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
• 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
• 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
• 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
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.
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).
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.)
PROBLEMS 15
2 8 10 16 17 18 23 20 21 30
Reconstruct the binary heap
PROBLEMS16
Problem 2: Give the array representation for 3
1016
13 12 18 23
15 19 30 14
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
NOW I WOULD LIKE TO CALL
HAJRA MUSTAFA
18
BASIC OPERATIONS• Insert a node – Up heapify• Delete a node–Down heapify• Build the heap
19
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
PERCOLATE UP21
Insert 20 6
1012
15 17 18 23
21 19 34 20
PERCOLATE UP
22
Insert 16
6
1012
15 18 23
21 19 34 17
PERCOLATE UP
23
6
1012
15 18 23
21 19 34 17
16
Complexity of insertion: O(logN)
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.
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.
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.
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.
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.
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.
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
EXAMPLE (CONT)31
150
80 40
20 1050 110
100 30 90 60 70 120 140 130
After processing height 1
EXAMPLE (CONT)32
150
10 40
20 6050 110
100 30 90 80 70 120 140 130
After processing height 2
EXAMPLE (CONT)33
10
20 40
30 6050 110
100 150 90 80 70 120 140 130
After processing height 3
ANY QUESTIONS34
35