Download Priority Queues-Constructing Algorithms and Representing Data-Lecture Slides and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
Priority Queues
2
Priority Queues
Data. Items that can be compared.
Basic operations.
! Insert.
! Remove largest.
! Copy.
! Create.
! Destroy.
! Test if empty.
defining ops generic ops 3
Priority Queue Applications
Applications.
! Event-driven simulation. [customers in a line, colliding particles] ! Numerical computation. [reducing roundoff error] ! Data compression. [Huffman codes] ! Graph searching. [Dijkstra's algorithm, Prim's algorithm] ! Computational number theory. [sum of powers] ! Artificial intelligence. [A* search] ! Statistics. [maintain largest M values in a sequence] ! Operating systems. [load balancing, interrupt handling] ! Discrete optimization. [bin packing, scheduling] ! Spam filtering. [Bayesian spam filter]
Generalizes: stack, queue, randomized queue.
4
Priority Queue Client Example
Problem: Find the largest M of a stream of N elements.
! Fraud detection: isolate $$ transactions.
! File maintenance: find biggest files or directories.
Constraint. Not enough memory to store N elements.
Solution. Use a priority queue.
MinPQ pq = new MinPQ(); while(!StdIn.isEmpty()) { String s = StdIn.readString(); pq.insert(s); if (pq.size() > M) pq.delMin(); } while (!pq.isEmpty()) System.out.println(pq.delMin()); sort Operation elementary PQ binary heap best in theory N space M M M N lg N time M N N lg M N
5
Priority Queue: Elementary Implementations
Two elementary implementations.
Challenge. Implement both operations efficiently.
unordered array Implementation ordered array N Del Max 1 1 Insert N worst-case asymptotic costs for PQ with N items 6
Priority Queue: Unordered Array Implementation
public class UnorderedPQ { private Item[] pq; // pq[i] = ith element on PQ private int N; // number of elements on PQ public UnorderedPQ(int maxN) { pq = (Item[]) new Comparable[maxN]; } public boolean isEmpty() { return N == 0 ; } public void insert(Item x) { pq[N++] = x; } public Item delMax() { int max = 0 ; for (int i = 1 ; i < N; i++) if (less(max, i)) max = i; exch(max, N- 1 ); return pq[--N]; } } no generic array creation 9
Binary Heap
Heap: Array representation of a heap-ordered complete binary tree.
Binary tree.
! Empty or
! Node with links to left and
right trees.
Heap-ordered binary tree.
! Keys in nodes.
! No smaller than children’s keys.
Array representation.
! Take nodes in level order.
! No explicit links needed since
tree is complete.
12
Binary Heap Properties
Property A. Largest key is at root.
Property B. Can use array indices to move through tree.
! Note: indices start at 1.
! Parent of node at k is at k/2.
! Children of node at k are at 2k and 2k+1.
Property C. Height of N node heap is 1 + !lg N".
N = 16 height = 5 height only increases when N is a power of 2
17 public class MaxPQ { private Item[] pq; private int N; public MaxPQ(int maxN) { } public boolean isEmpty() { } public void insert(Item x) { } public Item delMax() { } private void swim(int k) { } private void sink(int k) { } private boolean less(int i, int j) { } private void exch(int i, int j) { } }
Binary Heap: Skeleton
array helper functions same as array-based PQ, but allocate one extra element in array heap helper functions PQ ops 18
Binary Heap Considerations
Minimum oriented priority queue. Replace less() with greater() and
implement greater().
Array resizing. Add no-arg constructor, and apply repeated doubling.
Immutability of keys. We assume client does not change keys while
they're on the PQ. Best practice: make keys immutable.
Other operations.
! Remove an arbitrary item.
! Change the priority of an item.
! Can implement using sink() and swim() abstractions, but we defer.
O(log N) amortized time per op 19
Priority Queues Implementation Cost Summary
Hopeless challenge. Make all ops O(1). Why hopeless?
ordered array Operation ordered list unordered array unordered list binary heap
Remove Max 1 N N lg N
Find Max 1 N N 1
N
Insert N 1 1 lg N worst-case asymptotic costs for PQ with N items 20
Digression: Heapsort
First pass: build heap.
! Insert items into heap, one at at time.
! Or can use faster bottom-up method; see book.
Second pass: sort.
! Remove maximum items, one at a time.
! Leave in array, instead of nulling out.
Property D. At most 2 N lg N comparisons.
while (N > 1 ) { exch(a, 1 , N--); sink(a, 1 , N); } for (int k = N/ 2 ; k >= 1 ; k--) sink(a, k, N);
21
Significance of Heapsort
Q. Sort in O(N log N) worst-case without using extra memory?
A. Yes. Heapsort.
Not mergesort? Linear extra space.
Not quicksort? Quadratic time in worst case.
Heapsort is optimal for both time and space, but:
! Inner loop longer than quicksort’s.
! Makes poor use of cache memory.
challenge for bored: in-place merge challenge for bored: O(N log N) worst-case quicksort 22
Introsort
Introsort. [David Musser 1997] Run quicksort, but switch over to
heapsort if things are not going well.
Q1. How would you define "not going well"?
Q2. How would you detect it?
In the wild: g++ STL uses introsort.
combo of quicksort, heapsort, and insertion
Sorting Summary
In-Place Bubble sort X Selection sort Insertion sort Shellsort Quicksort Mergesort Heapsort X X X X X Stable X X X Worst N^2 / 2 N^2 / 2 N^2 / 2 N3/ N^2 / 2 N lg N 2 N lg N Average N^2 / 2 N^2 / 2 N^2 / 4 N3/ 2N ln N N lg N 2 N lg N Best N N^2 / 2 N N3/ N lg N N lg N N lg N Remarks never use it N exchanges use as cutoff for small N with Knuth sequence fastest in practice N log N guarantee, stable
Key Comparisons
A* Algorithm
Event-Driven Simulation
30
Molecular Dynamics Simulation of Hard Spheres
Goal. Simulate the motion of N moving particles that behave
according to the laws of elastic collision.
Hard sphere model.
! Moving particles interact via elastic collisions with each other,
and with fixed walls.
! Each particle is a sphere with known position, velocity, mass, and radius.
! No other forces are exerted.
Significance. Relates macroscopic observables to microscopic dynamics.
! Maxwell and Boltzmann: derive distribution of speeds of interacting
molecules as a function of temperature.
! Einstein: explain Brownian motion of pollen grains.
motion of individual atoms and molecules temperature, pressure, diffusion constant 31
Time-Driven Simulation
Time-driven simulation.
! Discretize time in quanta of size dt.
! Update the position of each particle after every dt units of time, and
check for overlaps.
! If overlap, roll back the clock to the time of the collision, update the
velocities of the colliding particles, and continue the simulation.
t t + dt (^) t + 2 dt (collision detected) t + #t (roll back clock) 32
Time-Driven Simulation
Main drawbacks.
! N^2 overlap checks per time quantum.
! May miss collisions if dt is too large and colliding particles fail to
overlap when we are looking.
! Simulation is too slow if dt is very small.
t t + dt (^) t + 2 dt
33
Event-Driven Simulation
Event-driven simulation.
! Between collisions, particles move in straight-line trajectories.
! Focus only on times when collisions occur.
! Maintain priority queue of collision events, prioritized by time.
! Remove the minimum = get next collision.
Collision prediction. Given position, velocity, and radius of a particle,
when will it collide next with a wall or another particle?
Collision resolution. If collision occurs, update colliding particle(s)
according to laws of elastic collisions.
34
Particle-Wall Collision
Collision prediction.
! Particle of radius $ at position (rx, ry), moving with velocity (vx, vy).
! Will it collide with a horizontal wall? If so, when?
Collision resolution. (vx', vy') = (vx, -vy).
" t =
# if vy = 0
($ % ry )/ vy if v y < 0
( 1 % $ % ry )/ vy if v y > 0
)^ (
$ (rx, ry) time = t (vx, vy) (vx,^ - vy) (rx', ry') time = t + #t 35
Particle-Particle Collision Prediction
Collision prediction.
! Particle i: radius $i, position (rxi, ryi), velocity (vxi, vyi).
! Particle j: radius $j, position (rxj, ryj), velocity (vxj, vyj).
! Will particles i and j collide? If so, when?
$j $i (rxi , ryi) time = t (vxi , vyi ) m (^) i i j (rxi', ryi') time = t + #t (vxj', vyj') (vxi', vyi') (vxj , vyj) 36
Particle-Particle Collision Prediction
Collision prediction.
! Particle i: radius $i, position (rxi, ryi), velocity (vxi, vyi).
! Particle j: radius $j, position (rxj, ryj), velocity (vxj, vyj).
! Will particles i and j collide? If so, when?
" t =
# if " v $ " r % 0
# if d < 0
" v $ " v
otherwise
d = (" v # " r )^2 $ (" v # " v ) (" r # " r $ %^2 )
" = " i + " j
" v = (" vx , " vy ) = ( vxi # vx (^) j , vyi # vyj ) ! " r = (" rx , " ry ) = ( rxi # rx (^) j , ryi # ryj ) ! " v # " v = (" vx )^2 + (" vy )^2 ! " r # " r = (" rx )^2 + (" ry )^2 ! " v # " r = (" vx )(" rx ) + (" vy )(" ry )