Midterm 1 Flashcards
What is the inductive hypothesis when trying to prove loop invariance of the binary search algorithm?
A[lo pointer index] <= target <= A[hi pointer index]
What must be shown about loop invariant? (to prove correctness of an algorithm)
- Initialization
- Maintenance
- Termination
*Show the inductive hypothesis is true for all 3 of these
What is the exact definition of an asymptotic upper bound?
There exists an n0 such that, for all n <= n0, f(n) > g(n)
*Use substitution!!!
What does O(1) mean?
We say t(n) is O(1), if there exists 2 positive constants n0 and c such that for all n >= n0, t(n) <= c
So, it means that t(n) is bounded
What is the definition of big Theta?
Let f(n) and g(n) be 2 functions, where n >= 0.
We say t(n) is big Theta of g(n) if there exists 3 positive constants n0, c1 and c2, such that for all n >= n0,
c1 * g(n) <= f(n) <= c2 * g(n)
What is the definition of a collision?
When 2 keys have the same Hash codes
What operations do right shift and left shift convert to?
Right shift (») = division by 2^k
Left shift («) = multiplication by 2^k
How to convert binary to base 10 and inversely:
10111
10111 = (1x2^4) + (0x2^3) + (1x2^2) + (1x2^1) + (1x2^0)
How to quickly compute 10111 mod 8?
8 = 2^3 → return the last 3 bits
10111 mod 8 = 111
What is a heap vs a BST?
Heap:
- Complete binary tree
- Heap property: value of a node >= value of its children (maxHeap)
- root has max value in a (maxHeap)
- represents priority queues
BST:
- each node has max 2 children
- v >= v of left subtree
- v <= v of right subtree
- worst case, tree is unbalances → O(n)
What is running time of union-find?
It is quasi-constant
m union or find operations take O(m a(n) )
*Union-find = union by size + path compression
What index in an array do the children of a node take in a complete binary tree?
parent node = floor (k/2)
children = 2k and 2k+1
What is the size of the largest possible subtree of a complete binary tree of size n?
2n/3 → if the last row is exactly 1/2 full
In a complete binary tree, what nodes would be found at level L?
What would be the total number of nodes in the tree
Nodes 2^l → 2^(l+1) - 1
Total of nodes = 2^(h+1) - 1
What is the exact height of a complete binary tree expressed in terms of the number of nodes?
h = log (n + 1) - 1
What is the running time of 1 iteration of maxHeapify?
T(n) = O(log n)
T(n) <= T(2n/3, size of largest subtree you can call maxHeapify on) + O(1)
What algorithm allows to take an array and build a maxHeap out of it?
n = length[A]
for (i = floor(n/2) → down to i = 1)
call maxHeapify (A, i)
*not down to i = 0, because the floor of 1/2 is already 0
This starts at the lowest node that is node a leaf, checks that it is larger that the root of its 2 subtrees (2 leafs in this case) by calling maxHeapify.
- It does the same for every node until the root
What is the loop invariant property of the algortihm allowing to build a heap (to prove its correctness)?
At the start of each iteration of the for loop, each node i+1, i+2, …, n is the root of a max-heap.
Show maintenance:
- By this LI, subtrees at children of node i are max heaps.
- MaxHeapify(i) renders node i a max heap root (while preserving the max heap root property of higher-numbered nodes).
- Decrementing i reestablishes the loop invariant for the next iteration. (for loop)
Termination:
- Loop is bounded by number of calls to MaxHeapify
- At termination, i =0. Then, each node 1,2,…,n is the root of a max-heap. In particular, node 1 is.
What is the running time of Build max heap?
Loose and Tight upper bounds?
running time = cost of maxHeapify (O (log n) ) * # calls to maxHeapify (O(n))
Loose upper bound = o (n log n)
- Cost of maxHeapify = O (log n)
- # calls to maxHeapify = O (n)
Tight upper bound = O (n)
- cost of maxHeapify is only O (log n) for the root
- # nodes with height h <= ceiling(n/ 2^h+1 )
- maxheapify
What is the running time of heap sort?
O (n log n)
What is in order traversal? What type of tree is it used with?
Print the nodes in the left subtree (A), then node x, and then the nodes in the right subtree (B) → useful for binary search trees as it prints the nodes in increasing order of value
inordertraversal (treeNode x)
if x != nil
inorderTraversal (x.leftChild);
print x.value;
inorderTraversal (x.rightChild);
What is the definition of an AVL BST?
BST such that the heights of the two child subtrees of any node differ by at most one.
*To satisfy the definition, the height of an empty subtree is -1
What are worst case examples of AVLs
AVL trees with a minimum number of nodes are the worst case examples.
- every node’s subtrees differ in height by one
- we cannot make these trees any worse / any more unbalanced without breaking the AVL property (if we change something, we either get a more balanced tree of break the AVL property)
*If we can bound the height of these worst case examples of AVL trees, then we’ve pretty much bounded the height of all AVL trees
Use this to prove that the max height is O (log n)
How do AVL ensure insertion conserves the AVL property?
What is the running time?
- Add as in any BST
- Restore AVL depending on which case occurs
Case 1: The INSERTION was “outside” → 1 rotation
- To the left subtree of the left child
Case 2: The INSERTION was “inside” → break zigzag → final rotation
running time = insertion time (O (h)) + rotations (O (1)) → O(log n)