CSE 326: Data Structures Lecture #4 Mind Your Priority Queues

advertisement
CS221: Algorithms and
Data Structures
Lecture #3
Mind Your Priority Queues
Steve Wolfman
2014W1
1
Learning Goals
• Provide examples of appropriate applications for
priority queues.
• Describe efficient implementations of priority
queues.
• Relate heaps to binary trees, binary search trees,
and arrays.
2
It is not a goal for this term to be able to manipulate heaps by hand.
Today’s Outline
• Priority Queue ADT
• Solutions So Far?
• 10km View of Heaps
3
Back to Queues
• Some applications
– ordering CPU jobs
– simulating events
– picking the next search site when searching {a maze, a
game tree, a room, the WWW}
• Problems?
– short jobs should go first
– earliest (simulated time) events should go first
– most promising sites should be searched first
4
Priority Queue ADT
• Priority Queue operations
–
–
–
–
–
create
destroy
insert
deleteMin
is_empty
G(9) insert
F(7) E(5)
D(100) A(4)
B(6)
deleteMin
C(3)
• 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
5
Applications of the Priority Q
•
•
•
•
•
•
•
Call queues for help lines (or don’t you think so?)
Your call will
Hold jobs for a printer in order of length
not be answered
in the order it
Simulate events (hold in order of time)
was received.
Sort numbers
Store packets on network routers in order of urgency
Select symbols for compression (hold in order of frequency)
Anything greedy: an algorithm that makes the “locally best
choice” at each step (hold in order of quality)
6
Naïve Priority Q Data Structures
• Unsorted array (or linked list):
– insert:
– deleteMin:
• Sorted array:
– insert:
– deleteMin:
a.
b.
c.
d.
e.
O(lg n)
O(n)
O(n lg n)
O(n2)
Something else
7
Today’s Outline
• Priority Queue ADT
• Solutions So Far?
• 10km View of Heaps
8
How Can We Efficiently
Implement a PQ?
• Stack?
• Queue?
• Linked List
– Singly, doubly, …
Priority value is _______
insert is ________
• Array
– Circular, resizing, …
• BST
• AVL Tree
delete_min is _______
9
AVL Tree as PQ
How well does this work in terms of the number of
priority values/data in the tree, n?
Runtime of insert?
Runtime of delete_min?
10
Today’s Outline
• Priority Queue ADT
• Solutions So Far?
• 10km View of Heaps
11
Binary Heap Priority Q Data
Structure
Look! Invariants!
• Heap-order property
2
– parent’s key is less than or
equal to children’s keys
– result: minimum is always
at the top
4
5
• Structure property
– “nearly complete tree”
– result: depth is always
O(log n); next open location
always known
7
11
6
9
10
8
12 14 20
WARNING: this has NO SIMILARITY to the “heap” you hear about
when people say “things you create with new go on the heap”. And,
12
this is a binary tree but is NOT a binary search tree.
Nifty Storage Trick
2
0
1
2
4
5
3
4
7
6
7
11
6
5
10
8
8
9 12 14 20
9
11
10
0
1
2
3
4
5
6
7
8
2
4
5
7
6
10
8
11
9
9
10
11
12
12 14 20
So, we get the speed and “spatial locality” of arrays.
13
(Also the occasional expensive resizes of arrays.)
DeleteMin
pqueue.deleteMin()
2
2
?
4
7
11
5
6
9
10
12 14 20
4
8
7
11
5
6
9
Invariants violated! DOOOM!!!
10
8
12 14 20
14
Percolate Down
?
4
4
7
5
6
?
10
8
7
11 9 12 14 20
5
6
10
11 9 12 14 20
4
4
6
7
5
?
8
10
11 9 12 14 20
6
8
7
5
12
10
11 9 20 14 20
8
15
Finally…
4
6
7
11
5
12
10
8
9 20 14
Algorithm intuition:
move the rightmost, bottom element to the root;
then, fix the invariant downward until stable.
16
Insert
pqueue.insert(3)
2
2
4
7
11
5
6
9
10
12 14 20
4
8
7
11
5
6
9
10
12 14 20
Invariant violated! What will we do?
Intuition: insert at the bottom-right;
then, fix invariant upward until stable.
8
3
17
Insert Result
2
2
4
7
4
5
6
10
11 9 12 14 20 3
8
7
3
6
5
8
11 9 12 14 20 10
18
Closer Look at Creating Heaps
To create a heap given a list of items:
Create an empty heap.
For each item: insert into heap.
Time complexity?
a. O(lg n)
b. O(n)
c. O(n lg n)
d. O(n2)
e. None of these
9, 4, 8, 1, 7, 2
3
5
10
3
12
11
19
A Better 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
Invariant violated!
Where can the order
invariant be violated
3
in general?
a. Anywhere
b. Non-leaves
4 8
c. Non-roots
5
11
10
1
6
7
2
9
20
Build(this)Heap: fix from bottom to top
12
12
5
11
3
4
10
8
1
2
7
5
9
11
3
6
4
1
2
8 10 7
6
12
12
5
3
4
2
1
8 10 7
6
11
9
1
9
3
4
2
5
8 10 7
6
11
9
21
Finally…
1
3
4
2
5
12 8 10 7
6
9
11
runtime:
22
Build(any)Heap
This is as many violations as we can get.
How do we fix them? Let’s play colouring games!
23
“amortized analysis!”
Other Uses for Trees
(besides BSTs and heaps)
• Family Trees
• Organization Charts
• Classification trees
– what kind of flower is this?
– is this mushroom poisonous?
• File directory structure
– folders, subfolders in Windows
– directories, subdirectories in UNIX
• Function call tree (i.e., a record of everything that
24
goes in the call stack)
To Do
• Read: KW Section 8.5
25
Coming Up
• Sorting, sorting, and more sorting!
26
Tree Terminology Reference
A
root: the single node with no parent
leaf: a node with no children
B
child: a node pointed to by me
parent: the node that points to me
D E
sibling: another child of my parent
ancestor: my parent or my parent’s ancestor
descendent: my child or my child’s descendent
subtree: a node and its descendents
C
F
H
G
I
J K L M N
We sometimes use degenerate versions
of these definitions that allow NULL as
the empty tree. (This can be very handy for recursive base cases!)
27
Download