Binomial heap
optimization of max heap
priority queue review: max heap
- insert: #card=math&code=O%28log%20n%29)
- removeMax: #card=math&code=O%28log%20n%29)
Question: How to optimize? (make insert #card=math&code=O%281%29))
- Hard if every operation #card=math&code=O%281%29)
- Possible if “amortized” #card=math&code=O%281%29) (solution similar to extendable array)
Solution
- cheap insertion usually insertion to “small” tree usually
- expensive insertion sometimes insertion to “big tree” sometimes
- This structure is called “forest”
binomial tree
structure:
||1 node||
|——|——|——|
||2 nodes|2|
||4 nodes|2|
||8 nodes|2|
|…|…|…|
|| nodes|2|summarize
Change the bionomial tree to a heap
binomial tree multi-tree
- max-tree property
bionomial forest + max-trees
- at most one per each k
can represent any :
example: n=10 () + 2()
- at most one per each k
- binomial heap
- Analysis
insert n nodes:
n insert #card=math&code=O%28n%29)
merge
merge #card=math&code=O%28n%29)
merge /
amortized complexity: #card=math&code=O%281%29)
search comlexity: #card=math&code=O%28log%20n%29)
explanation: binary system representation
Hash Map
Definition
priority queue:
- A.removeMax() fast
- A.insert(key,data) fast
- category: heap, binomial heap,…
map
- A.insert(key, data) fast
- A.remove(key) fast
- key:ordered/unordered (int/string)
How to realize Hash map
- linked list: (key, value) | | insert | get | | —- | —- | —- | | unordered key | #card=math&code=O%281%29) | #card=math&code=O%28n%29) | | ordered key | #card=math&code=O%28n%29) | #card=math&code=O%28n%29) |
- array | | insert | get | | —- | —- | —- | | unordered key | #card=math&code=O%281%29) | #card=math&code=O%28n%29) | | ordered key | #card=math&code=O%28n%29) | #card=math&code=O%28log%20n%29) |
- key (small)integer
key as array index, value as array data | | insert | get | | —- | —- | —- | | key | #card=math&code=O%281%29) | #card=math&code=O%281%29) |
non-small integer key need super big array(space)
- key convert: string to int
- key compress: reduce space
- A.insert(key, value) A[h(key)] = value
- v = A.get(key) v = A[h(key)]
A: bucket hash
h: array function
In most circumstance, keys > K
- two keys of same
- collision
Hash Code
str “apple”
- HashCode(str) = str[0] -‘a’
HashCode(“apple”) = HashCode(“act”)
- HashCode(str) = str[0] -‘a’
- HashCode(str) = #card=math&code=%5Csum_%7Bi%7D%20%28str%5Bi%5D-%27a%27%29)
%100 (compress)
- HashCode(str) = #card=math&code=%5Csum_%7Bi%7D%20%28str%5Bi%5D-%27a%27%29)