algorithms
bounds
- upper bound if grows as most as fast as
- lower bound if grows as least as fast as
- tight bound if grows as fast as
sum of theta
- if and , then
product of theta
- if and , then
linear search
- time complexity:
- space complexity:
constants in time complexity
- if we have and , then is worse for all but really large
arrays (python)
-
accessing element, need its address
-
to resize array, new chunk of memory needs to be found; old values copied over
-
np.append takes (better to pre-allocate)
linked list
- retrieve k-th elem: if you dont know address, if you do
- append/pop:
- insert/remove k-th elem: if you dont know address, if you do
- allocation not needed
dynamic array
- allocate memory for underlying array
- there is physical size and logical size
- when logical size is full, allocate new underlying array whose physical size is (growth factor) times the previous
- normally gamma is double
- the last append when physical size is full takes , otherwise takes
amortized analysis
-
spreading cost of something over time
-
in a sequence of n appends, how many caused physical size to grow?
- simplicity: assume n is such that n-th append causes physical size to grow
- x is number of resizes
- number of resizes is
-
time for jth resize: where c is constant that depends on
-
total time:
-
amortized time is
binary heaps
- binary tree normally used to implement priority queues
- full if every node has either 0 or 2 children
- perfect if all internal nodes have 2 children and all leaves are at the same level
- complete if all levels are filled except possibly the last, which is filled from left to right
- height of node is largest number of edges from root to leaf
- height of tree is height of root
- height of complete tree and complete binary tree is
binary max heap (min heap) is binary tree that:
-
each node has a key
-
shape: tree is complete
-
max heap: key of node is greater than or equal to keys of children
-
min heap: key of node is less than or equal to keys of children
-
.max
takes -
.insert
takes -
.delete
takes -
.pop_max_key
takes
online median
- given a stream of numbers one at a time, find the median of all numbers seen so far
- keep a max heap of the first half and a min heap of the second half
- may be unbalanced, so we need to rebalance
dynamic set
- similar to
dictionary
- store n elements in hash table
- initial cost:
- insert:
- search:
hashing
- collisions stored in same bin but in linked list
- good hash function should utilize all bins evenly
binary search tree
-
alternative way to dynamic sets
-
slower insert/deletion and query than dynamic set
-
preserves data in sorted order
-
BST is binary tree that satisfies following for any node x:
- left subtree of x contains only keys less than x
- right subtree of x contains only keys greater than x
- left and right subtrees must also be binary search trees
- Each element stored as a node at an arbitrary address in memory
- Each node has a key and pointers to left child, right child, and parent nodes (if they exist).
(diagram 1: tree representation)
(diagram 2: memory representation with pointers)
An array-like structure representing memory slots.
[ , l, 6, r, p, , l, 33, r, p, , , l, 12, r, p, , ]
-
pointers are shown with arrows:
- the 'l' (left child) pointer for node 6 points to node 12.
- the 'r' (right child) pointer for node 6 points to node 33.
- the 'p' (parent) pointer for node 12 points to node 6.
- the 'p' (parent) pointer for node 33 points to node 6.
- other pointers (like children of 12 and 33, and parent of 6) might be null or point elsewhere depending on the full structure.
-
in order traversal: - prints node values in ascending order
-
pre order traversal: - visits root, left subtree, right subtree
-
post order traversal: - visits left subtree, right subtree, then root
-
deleting node with no leaf is easy, if one child then easy
-
otherwise use the idea of rotating node to bottom, preserving BST, then when it is a leaf or has one child, delete it.
-
right rotation:
-
left rotation:
-
rotate takes
range queries in BST
-
ceiling of x is smallest key greater than or equal to x
-
sucessor of node u is smallest node greater than u
-
strat: find ceiling of a ([a,b] is range), repeatedly find sucessor until > b
BST : in general vs balanced vs unbalanced
- query, insertion/deletion: in general, in balanced, in unbalanced
balanced BSTs
- AVL trees
- red-black trees
- BST whose nodes are colored red and black
- leaf nodes are "nil"
- must satisfy:
- root nodes are black
- every leaf node is black
- if a node is red, both child nodes are black
- for any node, all paths fro node to leaf contain same number of black nodes
- n internal (non-nil) nodes = height is at most
- non-modifying operations:
- expected height of BST built by inserting random keys:
treaps
-
binary tree in which each node has both key and priority
- max heap with respect to priority
- BST with respect to key
-
insertion:
- using key, find place to insert node as if in BST
- while priority of node is more than parent, right rotate new node if it is left child, left rotate new node if it is right child
- repeat until heap property is satisfied
-
deletion:
- while node is not leaf:
- rotate it with child of highest priority
- once it is a leaf, delete it
- while node is not leaf:
-
if all keys and priorioties are unique, treap is unique.
randomized binary search trees
- when inserting node into treap, generate priority
- runtimes are normally but can be in the worst case (very rare)
citation: dsc190 advanced algos @ UCSD