Instance Simplification: Transform to simpler instance of same problem.
Example — Presorting an array will make many problems easier (searching, median, uniqueness).
- This applies also to topological sort and many geometric algorithms.
Representation Change: Different representation of same instance.
Problem Reduction:
Sorting is important for lots of algorithms, which means the efficiency of the sort is super important.
Factoid — The fastest sorting algorithm in the world is n \log n
Q: Search for k in A[0..n-1]
A:
We can first sort A, and then do a binary search.
\text{Sort} + \text{Binary Search} = n \log n + \log n
Q: Determine if A[0..n-1] has unique elements.
A:
Sort, then scary array to check pairs of adjacent elements.
\text{Sort} + \text{Check Pairs} = O(n \log n) + O(n)
Q: Given a set of keys S and a search keyk, find and occurrence of k in S, if any.
Real-World Uses — File size, dynamic of data.
A: ???
Suppose we implement a dictionary with a BST.
Recall — Efficiency depends on tree’s height.
Efficiency:
Recall — BST works worst on unbalanced trees. This is problematic if our operations gradually make our tree worse.
Solutions:
- AVL
- red-black
- 2-3
- 2-3-4
- ???
AVL Tree: BST in which, for every node, balance factor is at most 1.
Note — Height of empty tree defined as -1.
Balance Factor Examples:
TODO Drawing of AVL Tree
When adding a new node to a BST, we can balance the addition via one of the four rotations.
TODO I don’t really get this. Single rotations look normal. Double rotations are apparently two single rotations, but I can’t see it, it look like you’re just swapping nodes? Arghhhhh.
TODO Drawings and examples of every rotation
TODO Example of constructing an AVL tree from a list (5,6,8,3,2,4,7)
MST is a search tree that allows more than one key in the same node of the tree.
Lets us overcome some of the issues with binary search trees.
Search tree that may have 2-nodes and 3-nodes.
Insertion
Why? — Not as fast, but easier to keep balanced.
- In, you unbalance a binary tree when you add height to one path significantly more than other possible paths.
- On 2-3, you can only add height when creating a new root, which adds a unit of height to all paths simultaneously.
Analysis — Search, insertion, and deletion are \theta (\log n)
Heap: Binary tree with keys at its nodes (one key per node) such that it is essentially complete.
Definition: Essentially Complete: All levels full except possibly last level, where only some rightmost keys may be missing.
Remember — This is a binary tree, not a binary search tree! The property is parents are bigger than children, so it’s ordered top-down, but not left-right.
Important Properties:
- Root is the largest element.
- Any subtree is also a heap.
- Easily represented as array.
We can represent heaps as arrays by storing elements in level-order.
TODO Drawing
Accesses:
- Left Child of Node j: 2j
- Right Child of Node j: 2j + 1
- Parent of Node: \lfloor j/2 \rfloor
- Parental nodes are in the first half of the array.
Heaps are really good at representing priority queues.
TODO Definition
AVL and 2-3 we’ve done top-down, but heap we can do two methods.
Bottom-Up: Put everything in and then fix it.
Top-Down: Construct by successively inserting elements into an initially empty heap.
Which is Better? — Top-down is more straightforward, especially for inserting a new key; but bottom-up is more efficient.
TODO Pseudocode
Analysis — Parental node at level i does at most 2(h-i) comparisons. Thus, efficiency of bottom-up construction \theta(n)
Analysis — Although it looks more straightforward, efficiency ends up being \theta(n \log n).
Strategy:
Algorithm:
TODO I don’t understand how the root removal and fix-up works?
TODO Pseudocode
Analysis — Creation of heap is \Theta (n), root removal portion is \Theta( n \log n), so efficiency class is \Theta( n \log n)
Solve problem by transforming it into differnt problem for which an algorithm is already available.
Note — To be of practical value, the combined time of the transformation and solving of the reduced problem should be smaller than solving the problem as by another method.
Examples:
We know:
lcm(m,n) = \frac{m \times n}{gcd(m,n)}
TODO Example of Simple middle school math, 24 and 60.
Thus, we could just use an efficient method like Euclid’s algorithm to quickly get LCM.
TODO Something to do with nth power.