Algorithms and OOD (CSC 207 2014S) : Outlines
Held: Monday, 21 April 2014
Back to Outline 44 - Hash Tables, Continued.
On to Outline 46 - Pause for Breath.
We explore heaps, a useful and relatively efficient implementation of
- Priority queues, revisited.
- Recent implementation techniques.
- Adding elements to heaps.
- Removing elements from heaps.
- Asymptotic analysis.
- Storing heaps in arrays.
- Heap sort.
- Reading for Tuesday: No reading.
- Today's writeup: No writeup.
- Part 2 of the project is due Wednesday (tentatively).
- Welcome to our prospective
- I've reviewed about half of your projects before running out of time.
We'll talk about some common (and not so common) issues for about
- Given the problems with MathLAN, I'm not sure what to do about
the due date of the project. What do you think?
- Those of you trying to fix the problem using Wireshark or whatever
probably need to stop.
- I need two graders for next semester who can do detailed comments.
Yes, I will provide training.
- Math extra Thursday: Sphere Packing.
- CS Table Friday: Heartbleed.
- Iowater project April 26 - Tag drains. Mail email@example.com for details.
Priority Queues, Reviewed
- What is a priority queue?
- What are the primary operations?
- What are the implementations we have? What are the running times
of the key operations?
- What are implementation techniques we've learned since we last looked
at priority queues?
- I know of two: We've started looking at trees and we've learned some
clever ways of using arrays (in hash tables).
- Will either help?
- Binary search trees are cool, but would be cooler if we could keep
- Simplified binary search trees.
- Binary trees with the heap property
- Each node is at least as big as the roots of its subtrees.
- Each subtree also has the same property.
- Binary trees that are nearly complete
- It's complete in the sense that most nodes have two children
- At the last level all the nodes are at the left.
- Two invariants to maintain: The shape of the tree and the heap property.
- Which is harder? Probably getting the shape right.
- So we add an element at the end of the last level.
- And then we restore the heap property by repeatedly swapping up.
- The largest (highest priority) element is at the top.
- Once we remove it, what do we do?
- Once again, two invariants to maintain: The shape of the tree and the
- So, we take the last element and put it at the root.
- And then we swap it down to the right place?
- Repeatedly swap with the larger child (provided it's smaller than
the larger child).
- Here's the really clever part: We can store heaps in arrays.
- Can you figure out the index of children and parents?
- Sorting using heaps. Turn the array into a heap. Then repeatedly grab
the top element and put it at the end.