Introduction to Algorithm
Some Notes of Introduction to AlgorithmPermalink
Fiboniacci NumberPermalink
Order StatisticsPermalink
Given n elements in an array, find smallest element.
- Quick Select
- Expected running time
- Worse case
- Worse-case linear time order statistics
Select(i, n) 1. Divide n elements into [n/5] groups of 5 elements each. Find the median of each group. O(n) 2. Recurrsively select the medium x of the [n/5] group medians. T(n/5) 3. Partition with x as pivot, let k = rank(x). O(n) 4. if i==k then return x if i<k then recurrsively select ith smallest element in left part else then recurrsively select (i-k)th smallest element in upper part
Hash FunctionsPermalink
Division MethodPermalink
pick to be prime and not too close to power of or .
Multiplication MethodPermalink
» , < <
Universal HashingPermalink
Let be a universe of keys, and let be a finite colleciton of hash functions mapping to {}.
is if
i.e. if is chosen randomly from , the probability of collision between and is .
Perfect HashingPermalink
Given keys, construct a static hash table of size such that searching takes time in the worst case.
Idea: 2 level scheme with universal hashing at both levels and NO collisions at level 2.
if items that hashes to level 1 slot , then use slots in the level 2 table .
Augmented Data StructuresPermalink
Dynamic Order StatisticsPermalink
Supports: Insert
, Delete
, Search(x)
, Select(i)
, Rank(x)
.
Idea: use a R-B tree while keeping sizes of the subtree.
Select(root, i):
k = size[left(x)] + 1 // k = rank(x)
if i == k then return x
if i < k then return Select(left(x), i)
else return Select(right(x), i - k)
Interval TreePermalink
Supports: Intert
, Delete
, Interval-Search
: Find an interval in the set that
overlaps a given query interval.
Idea: use a R-B tree while keeping the largest value in the subtree.
Interval-Search(i) // finds an interval that overlaps i
x = root
while x != nil and (low[i] > high[int[x]] or low[int[x]] > high[i]) do // i and int[x] don't overlap
if left[x] != nil and low[i] <= m[left[x]] then x = left[x]
else x = right[x]
return x
Amortized AnalysisPermalink
Potential MethodPermalink
Framework:
- Start with data structure
- operation transforms
- cost of the operation is
- Define a potential function:
- Amortized cost with respect to is
- Total amortized cost of n operations is
Competitive AnalysisPermalink
An online algorithm A is - if such that for any sequence of operations ,
where is the optimal, off-line, “God’s” algorithm.
Karp-Rabin Algorihm: Find s in tPermalink
Rolling Hash ADT:
r.append(c)
: r maintains a string x where , add char c to the end of xr.skip()
: delete the first char of x. (assume it is c).
Then just use ADT to “roll over” t to find s.
Note: If their hashes are equal, there is still a probability that they are actual not the same string.
To implement ADT: use hash simple hash function where is a random prime
We can treat as a multidigit number in base , where is just the alphabet size.
So:
- stores and , (really ), not .
r.append(c)
u = u * a + ord(c) mod m
= [(u mod p) * a + ord(c)] mod m
= [r() * a + ord(c)] mod m
r.skip(c) // assume char c is skipped
u = [u − ord(c) * (pow(a, |u| - 1) mod p)] mod p
= [(u mod p) − ord(c) * (pow(a, |u| - 1) mod p)] mod p
= [r() − ord(c) * (pow(a, |u| - 1) mod p)] mod p
Comments