Algorithms-sheet
Algorithms-sheet
Stable/Inplace:
InsertionSort: IP=T, S=T Worst,Avg=Theta(n^2) Red-Black Trees:
MergeSort: IP=F, S=T, Worst=Theta(nlogn)
RandquickSort: IP=T, S=F, Worst=Theta(n^2)(When random picks 1. Root is black. 2. Each internal node is black or red.
the smallest elem repeatedly), Avg=Theta(nlogn) 3. Each internal node has two children. 4. Each leaf is an empty
node, black by default. 5. Path from root to any leaf have same no.
When memory is limited, RandQuick Sort would be better, as it
Binary Search: BinarySearch(A,v)_S=1_e=n_while s <= e__m= is in place. || || When previous ordering matters, RandQuick Sort black nodes (black height). 6. No consecutive red nodes.
L(s+e)/21_if A[m] == v then__return m\\else if A[m]<v then_S=m+1 is problematic because it is unstable.
ANY COMPARISON BASED ALGO IS big-omega(nlogn) in the
Insertion: node v to be inserted. W is parent. X is
\\else_e=m-1\\ \\return null. RUNTIME: O(logn) worst case.A decision tree represents all possible comparison grandparent.
Min in rotated sorted array: Use BS-compare mid elem with
right/leftmost elem. If mid>rightmost, search right half, else left half sorts. Non-leaf nodes show comparisons between elements. Leaf
nodes show final permutations. Each sorting path goes from root
Two-sum-prob: in-sorted array of integers and a target int v. to leaf. The tree must have n! leaf nodes for all permutations. A 1. W is black. Set
Out- yes if there is a and b such that a+b=v. No, otherwise. binary tree with h levels has at most 2^(h-1) leaves. This means n!
≤ 2^(h-1). Taking log of both sides: log(n!) ≤ h-1. We know log(n!) = v to red.
Brute-force: O(n^2) || Better - fix a and use binary search for b. Ω(n log n). Therefore h-1 = Ω(n log n). This proves
Repeat until True - O(nlogn) comparison-based sorting needs Ω(n log n) comparisons worst
Best - Linear algorithm (O(n)) — int s = 1_int e = n_while s <= case.
2. W is red. Set v as red.(If v < w < x or v > w > x.)
2.1 s is black(symmetric)
2.2 s is red.
2.1.1 (as shown)
2.1.2: If x’s parent is
black, done.
Otherwise, recolour 3.2 p = black. Keep repairing double black node.
recursively Starting from S[0, 0], we
trace the pointer; each
diagonal move means a new
character is added to the
final LCS.
Jump Game: compute min no.jumps needed to reach the end of
the array.i.e.,n-1.
4. P,q,r = black. S=red. Reduces to 1,2 or 3.1
3.1 If s is black.
3.1.1 (as shown)
3.1.2 Tree on right is Heaps: Greedy algorithms only need insert, find-min and
same as 2.1 extract-min.
Binary-heap (find-min - O(1); insert & extract-min - O(logn))
Perfect Tree, satisfies heap property(the key of the parent is less
than the key of any of its child)
3.2 If s is red Written as array- Has index from 0. Two children of the node at
index i = 2i+1 && 2i+2
3.2.1 (as shown) Heapify(O(logn)): if key value is larger than smallest value of
3.2.2 If x’s parent is children, exchange node until heap property is satisfied.
black, done. Decrease key(O(logn)): when a key is inserted or updated. Do the
same as heapify.
Otherwise, recolour x Fibonacci-heap(find-min & insert - O(1); extract-min- O(logn)).
recursively. Instead of maintaining one tree, multiple non-binary trees are
maintained with heap property in every tree.(not perfect tree)
Each node has at most logn children. If a node has k children, then
Deletion: the size(subtree rooted at that node) is atleast Fibonacci(k+2)
1. V is red and children = empty nodes. Delete. GOAL: have small no of trees (find-min is fast) and small heights
2. V is black and children = empty nodes. (Double black) of trees(decrese-key, insert, extract-min is fast)
3. V is black and Hashtables: ata with key k will be stored at index h(k)
has exactly one Modulo arithmetic: a = qm+r (1000 = 34 X 29 + 14)(1000 = 14
non-empty (mod 29)). Add, subtract, multiply.
h(k) = ak +b (mod m) m=prime no, 1<=a<m, 0<=b<m
child.Child S pairwise collision occurs when two different keys hash to the
must be red. same value. (PWC = n(n-1)/2)
4. V is red and Collision Resolution: Each cell in the hash table points to a linked
list and can store multiple values with the same hash value.
has exactly one (Search,insert and delete = O(ni)) where i=h(k).
non-empty Observation 1: The sequence h(0), h(1), h(2), . . . , h(u − 1) has cycle
of period m. In other words, for any k, h(k) = h(k + m) = h(k + 2m) = . .
Observation 2: h(0), h(1), h(2), . . . , h(m − 1) are all different.Hence,
each of 0, 1, 2, . . . , m − 1 appears once.
α = n/m(the load factor.)
child.(Impossible)
5. V has two non-empty children. (y == succ)
5.1 y is red. Then y must have two empty-node children. Universal Hashing: if we pick h uniformly randomly from H,
the probability that h(k1) = h(k2) is at most 1/m.