• Non ci sono risultati.

Priority Queues

N/A
N/A
Protected

Academic year: 2021

Condividi "Priority Queues"

Copied!
21
0
0

Testo completo

(1)

Priority Queues

Paolo Camurati and Stefano Quer

Dipartimento di Automatica e Informatica

Politecnico di Torino

(2)

Priority Queues

 Heaps have many applications beyond heap-sort

 A priority queue is a data structure to store elements including a priority field such that all main operations are based on such a field

 Priority queues have several applications

 Job scheduling

 Etc.

(3)

Priority Queues

 It is possible to implement

 Min-priority queues

 Max-priority queues

 Main operations

 Insert, extract maximum, read maximum, change priority

 Possible alternative data structure implementations

 Unordered array/list

 Ordered array/list

(4)

Example

20 15 10 12 11 5 4 9

1 2 3 4 5 6 7 8 9 10 11 12 13 14 pq->A

pq->heapsize = 13 0

a z ba d m g e x

8 5 7 2 0 y pp w k b

a 20

z 15

d 12 m 11

ba 10

g 5 e 4

x 9 y 8 pp 5 w 7 k 2 b 0

PQ

(Priority Queue)

Array representation

Array (maximum) maxN = 15

#define LEFT(i) (2*i+1)

#define RIGHT(i) (2*i+2)

#define PARENT(i) ((int)(i-1)/2)

Heap  PQ

(5)

Function pq_insert

 Add a leaf to the tree

 It grows level-by-level from left to right satisfying the structural property

 From current node up (initially the newest leaf) up to the root

 Compare the parent’s key with the new node’s key, moving the parent’s data from the parent to the child when the key to insert is larger

 Otherwise insert the data into the current node

 Complexity

 T(n) = O(lg n)

(6)

Example

15 0 12

1 9

3

11 4

10 2 5

5

4 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_insert (pq, ((item) 75))

(7)

Solution

75 0 12

1 9

3

11 4

15 2 5

5

10 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_insert (pq, (item) 75))

4

13

(8)

Implementation

void pq_insert (PQ pq, Item item) { int i;

i = pq->heapsize++;

while( (i>=1) &&

(item_less(pq->A[PARENT(i)], item)) ) pq->A[i] = pq->A[PARENT(i)];

i = PARENT (i);

}

pq->A[i] = item;

return;

}

Increase the heap size Function

item_less compares keys

Move node down Move up toward

the root Insert new

element in its final destination

(9)

Function pq_extract_max

 Modify the head, by extracting the largest value, stored into the root

 Swap root with the last leaf (the rightmost onto the last level)

 Reduce by 1 the heap size

 Restore the heap property by applying heapify

 Complexity

 T(n) = O(lg n)

(10)

Example

15 0 12

1 9

3

11 4

10 2 5

5

4 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_extract_max (pq)

(11)

Solution

12 0 11

1 9

3

7 4

10 2 5

5

4 6 1

7

8 8

5 9

0 10

2 11

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_extract_max (pq)

(12)

Implementation

Item pq_extract_max(PQ pq) { Item item;

swap (pq, 0, pq->heapsize-1);

item = pq->A[pq->heapsize-1];

pq->heapsize--;

heapify (pq, 0);

return item;

}

Extract max and move last element into the

root node

Reduce heap size

Heapify from root

(13)

Function pq_change

 Modify the key of an element in a given position given its index

 Can be implemented as two separate operations

 Decrease key

 When a key is decreased, we may need to move it downward

 To move a key downward, we can adopt the same process analyze in heapify

Heapify keeps moving the key from the parent to the

child with the largest key until the key is inserted into

the current node

(14)

Function pq_change

 Increase key

 When a key is increased, we may need to move it upward

 To move a key upward, we can adopt the same process analyze in pq_insert

We move the key up into the parent until the key is inserted into the current node

 Complexity

 Dependent on the tree height

 T(n) = O(lg n)

(15)

Example: Decrease key

15 0 12

1 9

3

11 4

10 2 5

5

4 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_change (pq, 1, ((item) 3))

(16)

Solution

15 0 11

1 9

3

7 4

10 2 5

5

4 6 1

7

8 8

5 9

3 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_change (pq, 1, ((item) 3))

(17)

Example: Increase key

15 0 12

1 9

3

11 4

10 2 5

5

4 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_change (pq, 5, ((item) 32))

(18)

Solution

32 0 12

1 9

3

11 4

15 2 10

5

4 6 1

7

8 8

5 9

7 10

2 11

0 12

Only (integer) keys are shown, not data items Array index, i.e., node

with key 1 is stored in pq->A[0]

 Call function

 pq_change (pq, 5, ((item) 32))

(19)

Implementation

void pq_change (PQ pq, int i, Item item) { if (item_less (item, pq->A[i]) {

decrease_key (pq, i);

} else {

increase_key (pq, i, item);

} }

void decrease_key (PQ pq, int i) { pq->A[i] = item;

heapify (pq, i);

}

void increase_key (PQ pq, int i) { while( (i>=1) &&

(item_less(pq->A[PARENT(i)], item)) ) { pq->A[i] = pq->A[PARENT(i)];

i = PARENT(i);

}

pq->A[i] = item;

}

(20)

Exercise

 Insert the following values into an initially empty max-priority queue

 11 31 77 34 65 1 76 48 55 24 9 98 90 5 13 88

Notice that this is not an application of heapsort

but of pq_insert

(21)

Exercise

 Insert the following values into an initially empty min-priority queue

 11 31 77 34 65 1 76 48 55 24 9 98 90 5 13 88

Notice that this is not an application of heapsort

but of pq_insert

Riferimenti

Documenti correlati

Poland fulfills this requirement by the Ordinance of the Minister of the Environment of 14 August 2009 on the report for the creation of the National Register

The main idea of our allocation control scheme, named Fair Allo- cation Control Window (FACW), is that each sensor main- tains a control window of size N that stores the traffic

On top of the agenda of any business to business (B2B) senior sales or marketing manager worth their salt is the question of tools, processes and capabilities to document

The aims of this study were to: (i) assess the feasi- bility to recruit participants and simultaneously obtain location and activity data on Hispanic preschool chil- dren in real

The reasons underlying the expected shortages are twofold: on the one hand, the ageing of the population will bring an increase of all occupations that are associated with heath

Tasks with shorter relative deadline have higher priority For sporadic tasks, the same rules are valid as for periodic tasks with offsets equal to 0... Utilization bound

The value of the response time is not helpful: even if the response time is well below the deadline, a small increase in the WCET of a higher priority task makes the response