0% found this document useful (0 votes)
20 views

Lecture 3

The document discusses priority queues and heaps. It covers operations like insert, deleteMin, and decreaseKey. It also discusses building heaps from unsorted arrays using Floyd's method. The document provides pseudocode for priority queue operations on binary heaps.

Uploaded by

demro channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 3

The document discusses priority queues and heaps. It covers operations like insert, deleteMin, and decreaseKey. It also discusses building heaps from unsorted arrays using Floyd's method. The document provides pseudocode for priority queue operations on binary heaps.

Uploaded by

demro channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

CSE 326: Data Structures

Lecture #3
Mind your Priority Qs
Bart Niswonger
Summer Quarter 2001
Today’s Outline
• The First Quiz!
• Things Bart Didn’t Get to on
Wednesday (Priority Queues &
Heaps)
• Extra heap operations
• d-Heaps
Back to Queues
• Some applications
– ordering CPU jobs
– simulating events
– picking the next search site
• Problems?
– short jobs should go first
– earliest (simulated time) events should go
first
– most promising sites should be searched
first
Priority Queue ADT
• Priority Queue operations
– create
– destroy F(7) E(5)
insert deleteMin
– insert G(9) D(100) A(4) C(3)
– deleteMin B(6)
– is_empty
• Priority Queue property: for two
elements in the queue, x and y, if x has
a lower priority value than y, x will be
deleted before y
Applications of the Priority Q
• Hold jobs for a printer in order of
length
• Store packets on network routers
in order of urgency
• Simulate events
• Select symbols for compression
• Sort numbers
• Anything greedy
Naïve Priority Q Data Structures
• Unsorted list:
– insert:

– deleteMin:

• Sorted list:
– insert:

– deleteMin:
Binary Search Tree Priority Q
Data Structure (that’s a mouthful)

8
insert:
5 11

deleteMin: 2 6 10 12

4 7 9 14

13
Binary Heap Priority Q Data
Structure
• Heap-order property
– parent’s key is less
than children’s keys 2
– result: minimum is
always at the top
• Structure property 4 5
– complete tree with
fringe nodes packed 7 6 10 8
to the left
– result: depth is
always O(log n); next 11 9 12 14 20
open location always
known
Nifty Storage Trick
1
• Calculations: 2
– child:
2 3
4 5
– parent:
4 5 6 7
7 6 10 8
– root:
8 9
11 9 12 14 20
– next free: 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12
12 2 4 5 7 6 10 8 11 9 12 14 20
DeleteMin
pqueue.deleteMin() 2
2 ?

4 5 4 5

7 6 10 8 7 6 10 8

11 9 12 14 20 11 9 12 14 20
Percolate Down
? 4

4 5 ? 5

7 6 10 8 7 6 10 8

11 9 12 14 20 11 9 12 14 20
4 4

6 5 6 5

7 ? 10 8 7 12 10 8
11 9 12 14 20 11 9 20 14 20
Finally…

6 5

7 12 10 8

11 9 20 14
DeleteMin Code
int percolateDown(int hole, Object val)
Object deleteMin() { {
while ( 2 * hole <= size ) {
assert(!isEmpty()); left = 2 * hole;
returnVal = Heap[1]; right = left + 1;
size--; if ( right <= size &&
Heap[right] < Heap[left])
newPos = target = right;
percolateDown(1, else
Heap[size+1]); target = left;

Heap[newPos] = if ( Heap[target] < val ) {


Heap[size + 1]; Heap[hole] = Heap[target];
hole = target;
return returnVal;
}
} else
break;
runtime: }
return hole;
Insert
pqueue.insert(3)

2 2

4 5 4 5

7 6 10 8 7 6 10 8

11 9 12 14 20 11 9 12 14 20 ?
Percolate Up
2 2

4 5 4 5

7 6 10 8 7 6 3? 8
3
11 9 12 14 20 ? 11 9 12 14 20 10
2 2
3
4 ? 4 3

7 6 5 8 7 6 5 8

11 9 12 14 20 10 11 9 12 14 20 10
Insert Code
void insert(Object o) { int percolateUp(int hole,
Object val) {
assert(!isFull());
while (hole > 1 &&
size++; val < Heap[hole/2])
newPos = Heap[hole] = Heap[hole/2];
hole /= 2;
percolateUp(size,o);
}
Heap[newPos] = o; return hole;
} }

runtime:
Changing Priorities
• In many applications the priority of an
object in a priority queue may change
over time
– if a job has been sitting in the printer queue
for a long time increase its priority
– unix “renice”

• Must have some (separate) way of find


the position in the queue of the object
to change (e.g. a hash table)
Other Priority Queue
Operations
• decreaseKey
– given the position of an object in the queue,
reduce its priority value
• increaseKey
– given the position of an an object in the
queue, increase its priority value
• remove
– given the position of an object in the queue,
remove it
• buildHeap
– given a set of items, build a heap
DecreaseKey, IncreaseKey,
and Remove
void decreaseKey(int obj) { void remove(int obj) {
assert(size >= obj); assert(size >= obj);
temp = Heap[obj]; percolateUp(obj,
NEG_INF_VAL);
newPos = percolateUp(obj, temp);
deleteMin();
Heap[newPos] = temp; }
}

void increaseKey(int obj) {


assert(size >= obj);
temp = Heap[obj];
newPos = percolateDown(obj, temp);
Heap[newPos] = temp;
}
BuildHeap
Floyd’s Method. Thank you, Floyd.
12 5 11 3 10 6 9 4 8 1 7 2
pretend it’s a heap and fix the heap-order property!
12

5 11

3 10 6 9

4 8 1 7 2
Build(this)Heap
12 12

5 11 5 11

3 10 2 9 3 1 2 9

4 8 1 7 6 4 8 10 7 6
12 12

5 2 1 2

3 1 6 9 3 5 6 9
4 8 10 7 11 4 8 10 7 11
Finally…

3 2

4 5 6 9

12 8 10 7 11

runtime:
Thinking about Heaps
• Observations
– finding a child/parent index is a multiply/divide
by two
– operations jump widely through the heap
– each operation looks at only two new nodes
– inserts are at least as common as deleteMins
• Realities
– division and multiplication by powers of two are
fast
– looking at one new piece of data sucks in a
cache line
– with huge data sets, disk accesses dominate
Solution: d-Heaps
• Each node has d 1
children
• Still representable by 3 7 2
array
• Good choices for d: 4 8 5 12 11 10 6 9
– optimize performance based
on # of inserts/removes
– choose a power of two for 12 1 3 7 2 4 8 5 12 11 10 6 9
efficiency
– fit one set of children in a
cache line
– fit one set of children on a
memory page/disk block
One More Operation
• Merge two heaps. Ideas?
To Do
• Read chapter 6 in the book
• Have teams
• Do project 1
• Ask questions!
Coming Up
• Mergeable heaps
• Dictionary ADT and Self-
Balancing Trees
• Unix Development Tutorial
(Tuesday)
• First project due (next
Wednesday)

You might also like