Data Structures in Java
Lecture 13: Priority Queues (Heaps)
11/4/2015 Daniel Bauer
1
Data Structures in Java Lecture 13: Priority Queues (Heaps) - - PowerPoint PPT Presentation
Data Structures in Java Lecture 13: Priority Queues (Heaps) 11/4/2015 Daniel Bauer 1 The Selection Problem Given an unordered sequence of N numbers S = (a 1 , a 2 , a N ) , select the k -th largest number. 2 Process Scheduling
Lecture 13: Priority Queues (Heaps)
11/4/2015 Daniel Bauer
1
S = (a1, a2, … aN), select the k-th largest number.
2
Process 1 600ms Process 2 200ms
t CPU
3
schedule them in FIFO oder. (Why is a Stack a terrible idea?)
Process 1 600ms Process 2 200ms
t CPU
3
schedule them in FIFO oder. (Why is a Stack a terrible idea?)
do not even know how long).
(CPU vs. I/O bound, critical real time systems) .
Process 1 600ms Process 2 200ms
t CPU
3
interval in round robin fashion.
Process 2
Queue:
Process 1
front back
CPU t
4
interval in round robin fashion.
Process 2
Queue:
Process 1
front back
Process 1
CPU t
4
interval in round robin fashion.
Process 2
Queue:
Process 1
front back
Process 1
CPU t
Process 3
4
interval in round robin fashion.
Process 2
Queue:
Process 1
front back
Process 1
CPU t
Process 3 Process 1
4
interval in round robin fashion.
Process 2
Queue:
Process 1
front back
Process 1
CPU t
Process 3 Process 1
Sometimes Process 3 is so crucial that we want to run it immediately when the CPU becomes available!
4
process with the highest priority first.
Process 2 Process 1
CPU t Queued Processes
priority 10 priority 10
5
process with the highest priority first.
Process 2 Process 1 Process 1
CPU t Queued Processes
priority 10 priority 10
5
process with the highest priority first.
Process 2 Process 1 Process 1
CPU t
Process 3
Queued Processes
priority 10 priority 1
5
process with the highest priority first.
Process 2 Process 1 Process 1
CPU t
Process 3
Queued Processes
priority 10 priority 1
5
supports the following operations:
enqueue).
Q and delete it from Q (compare to dequeue).
6
programing algorithm.
search).
7
8
insert(x):O(1), deleteMin(): O(N)
8
insert(x):O(1), deleteMin(): O(N)
insert(x):O(log N), deleteMin(): O(log N)
8
insert(x):O(1), deleteMin(): O(N)
insert(x):O(log N), deleteMin(): O(log N)
8
D E C A B F G H I J
9
D E C A B F G H I J
A B C D E F G H I J
10
D C A B F H I
tree, but only store the nodes that actually exist.
A B C D F I
11
following heap order property:
subtree rooted in n are greater or equal than x.
8
15 10
1 5
14 13
9
20
16
1 5 10 8 15 14 13 9 20 16
12
following heap order property:
subtree rooted in n are less or equal than x.
13
14
15
20
16
8 9
10
5
1
20 16 15 13 14 8 9 10 5 1
13
the last layer).
percolate the value up.
the parent cell, then try the new cell.
continue until correct position is found.
8
10
1
14 13
9
20
16
1 5 10 8 15 14 13 9 20 16
15
insert(3)
5 3
15 3
14
the last layer).
percolate the value up.
the parent cell, then try the new cell.
continue until correct position is found.
8
10
1
14 13
9
20
16
1 5 10 8 14 13 9 20 16
15
insert(3)
3
5 3
15
14
the last layer).
percolate the value up.
the parent cell, then try the new cell.
continue until correct position is found.
8
10
1
14 13
9
20
16
1 5 10 8 14 13 9 20 16
15
insert(3)
5
5
3
3
15
14
3
cell in the root.
the root.
percolate the value down:
until correct position is found.
10 8 14 13 9 20 16
8
10 14 13
9
20
16
15
15
5
1 1 3 5
15
3
cell in the root.
the root.
percolate the value down:
until correct position is found.
10 8 14 13 9 20 16
8
10 14 13
9
20
16
deleteMin() 1
15
5 15
3 5
15
15
3
cell in the root.
the root.
percolate the value down:
until correct position is found.
10 8 14 13 9 20 16
8
10 14 13
9
20
16
deleteMin() 1
5 15
3 5
15
3
cell in the root.
the root.
percolate the value down:
until correct position is found.
10 8 14 13 9 20 16
8
10 14 13
9
20
16
deleteMin() 1
5 15
3 5 15
15
height is about log N.
deleteMin() is therefore O(log N).
16
heap.
so the total time is O(N log N).
17
subtrees under i are already heaps.
layers are heaps.
18
subtrees under i are already heaps.
layers are heaps.
18
subtrees under i are already heaps.
layers are heaps.
18
subtrees under i are already heaps.
layers are heaps.
18
subtrees under i are already heaps.
layers are heaps.
For i = N/2 … 1 percolateDown(i)
19
1 2 3 4 5 6 7 8 9
10 11
5 4 6 9 1 8 3 10 7 2 11
i=11/2 = 5 For i = N/2 … 1 percolateDown(i)
20
1 2 3 4 5 6 8 9
10 11
7
5 4 6 9 1 8 3 10 7 2 11
i=4 For i = N/2 … 1 percolateDown(i)
21
1 2 3 4 5 6 8 9
10 11
7
5 4 6 1 8 3 10 2 11
i=4
9 7
For i = N/2 … 1 percolateDown(i)
21
1 2 4 5 6 7 8 9
10 11
5 4 6 7 1 8 3 10 9 2 11
i=3 3 For i = N/2 … 1 percolateDown(i)
22
1 2 4 5 6 7 8 9
10 11
5 4 7 1 8 10 9 2 11
i=3
6 3
3 For i = N/2 … 1 percolateDown(i)
22
2 6 4 5 3 7 8 9
10 11
5 4 3 7 1 8 6 10 9 2 11
i=2 1 For i = N/2 … 1 percolateDown(i)
23
2 6 4 5 3 7 8 9
10 11
5 3 7 1 8 6 10 9 2 11
i=2 1
1 4
For i = N/2 … 1 percolateDown(i)
23
2 6 4 5 3 7 8 9
10 11
5 3 7 1 8 6 10 9 2 11
i=2 1
1 2 4
For i = N/2 … 1 percolateDown(i)
23
6 3 7 8 9
10
5 3 7 8 6 10 9 4 11
i=1
1 2
1 5 2 4
11
For i = N/2 … 1 percolateDown(i)
24
6 3 7 8 9
10
3 7 8 6 10 9 4 11
i=1
1 2
1 5 2 4
11
5 1
For i = N/2 … 1 percolateDown(i)
24
6 3 7 8 9
10
3 7 8 6 10 9 4 11
i=1
1 2
1 5 2 4
11
1 2 5
For i = N/2 … 1 percolateDown(i)
24
6 3 7 8 9
10
3 7 8 6 10 9 4 11
i=1
1 2
1 5 2 4
11
1 2 5 4
For i = N/2 … 1 percolateDown(i)
24
N/2 percolateDown calls?
needs to move the value all the way down to the leaf level.
25
levels are full) :
height:
26
8 · 0 nodes · 0 steps
27
8 · 0 nodes · 0 steps 4 · 1 nodes · 1 steps
27
8 · 0 nodes · 0 steps 4 · 1 nodes · 1 steps 2 · 2 nodes · 2 steps
27
8 · 0 nodes · 0 steps 4 · 1 nodes · 1 steps 2 · 2 nodes · 2 steps 1 · 3 nodes · 3 steps
27
8 · 0 nodes · 0 steps 4 · 1 nodes · 1 steps 2 · 2 nodes · 2 steps 1 · 3 nodes · 3 steps
27
8 · 0 nodes · 0 steps 4 · 1 nodes · 1 steps 2 · 2 nodes · 2 steps 1 · 3 nodes · 3 steps
27
=N
28
S = (a1, a2, … aN), select the k-th largest number.
pick the number at k-th position. => O(N log N + k)
element in the sequence, if it is larger than the k-th entry in the array, replace the appropriate entry in the array with the new number. => O(k log k) + O(N · k)
29
S = (a1, a2, … aN), select the k-th largest number.
total is O(N log N).
30
S = (a1, a2, … aN), select the k-th largest number.
and insert the new number into S. This takes O(log k) time.
31