CS1: Design & Analysis of Algorithms Flashcards
Algorithm
Insertion Sort
for j = 1 to A.length - 1 key = A[j+1] i = j while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key
Knowledge
Insertion Sort Running Time
O(n²)
Knowledge
Divide and Conquer
4 points
- Divide a problem into a problems whose size is original size divided by b > 1
- Recursion stops when problems small enough to be solved by brute force
- Solutions combined to build a solution of original solution
- Work done in three places: dividing the problem, solving subproblems and combining their solutions
Theorem
The Master Theorem
Suppose T(n) ≤ aT(⌈n/b⌉) + O(nᵈ) for some constants a > 0, b > 1 and d ≥ 0
Then T(n) = O(nᵈ) if d > log_(b)a, T(n) = O(n^(log_(b)a)) if d < log_(b)a or T(n) = O(nᵈlog n) if d = log_(b)a
Proof
(The Master Theorem)
Suppose T(n) ≤ aT(⌈n/b⌉) + O(nᵈ) for some constants a > 0, b > 1 and d ≥ 0
Then T(n) = O(nᵈ) if d > log_(b)a, T(n) = O(n^(log_(b)a)) if d < log_(b)a or T(n) = O(nᵈlog n) if d = log_(b)a
3 points
- Recursion tree has branching factor a and depth log_b n
- Formulate expression for upper bound of T(n) containing a geometric series
- Consider different cases and the sum of the geometric series
Algorithm
Merge Sort
MERGE-SORT(A, p, r) if r > p + 1 q = ⌊(p + r)/2⌋ MERGE-SORT(A, p, q) MERGE-SORT(A, q, r) MERGE(A, p, q, r)
Proof
The running time of every comparison-based sorting algorithm is Ω(n log n)
6 points
- Every leaf of the decision tree is a permutation of {a₁, a₂, …, aₙ}
- The decision tree has n! leaves
- Every binary tree of depth d has at most 2ᵈ leaves
- In the worst case, the algorithm explores d nodes
- n! ≤ 2ᵈ
- Hence running time is Ω(log n!) = Ω(n log n)
Knowledge
The Selection Problem
2 points
- Input: A set of n (distinct) numbers and a number i, with 1 ≤ i ≤ n
- Output: The iᵗʰ-order statistic of the set
Knowledge
Selection Algorithm Running Time
O(n)
Algorithm
Integer Multiplication
3 points
- Split x = 2^(n/2) x₁ + x₂, y = 2^(n/2) y₁ + y₂
- x y = 2ⁿ x₁ y₁ + 2^(n/2)(x₁ y₂ + x₂ y₁) + x₂ y₂
- Computing x₁ y₁, x₂ y₂, (x₁ + x₂)(y₁ + y₂) suffice
Knowledge
Integer Multiplication Running Time
3 points
- Additions and 3 multiplications of (n/2)-bit numbers are required
- T(n) = 3 T(n/2) + O(n)
- T(n) = O(n^(log₂ 3))
Knowledge
Strassen’s Algorithm Running Time
4 points
- Split matrices X, Y into 8 (n/2)×(n/2) submatrices
- Calculate XY by making additions and 7 multiplications of submatrices
- T(n) = 7 T(n/2) + O(n²)
- T(n) = O(n^(log₂ 7) ≈ O(n^2.81)
Knowledge
MAX-HEAPIFY
2 points
- Input: Assume left and right subtrees of i are max-heaps
- Output: Subtree rooted at i is a max-heap
Algorithm
MAX-HEAPIFY
MAX-HEAPIFY(A, i) n = A.heap-size l = 2i r = 2i + 1 if l ≤ n and A[l] > A[i] largest = l else largest = i if r ≤ n and A[r] > A[largest] largest = r if largest ≠ i exchange A[i] with A[largest] MAX-HEAPIFY(A, largest)
Knowledge
MAX-HEAPIFY
Running Time
4 points
- Θ(1) to find largest among A[i], A[2i] and A[2i + 1]
- Subtree rooted at a child of node i has size upper bounded by 2n/3
- T(n) ≤ T(2n/3) + Θ(1)
- T(n) = O(log n) by the Master Theorem
Knowledge
MAKE-MAX-HEAP
2 points
- Input: An (unsorted) integer array A of length n
- Output: A heap of size n
Algorithm
MAKE-MAX-HEAP
MAKE-MAX-HEAP(A) A.heap-size = A.length for i = ⌈(n + 1)/2⌉ - 1 downto 1 MAX-HEAPIFY(A, i)
Knowledge
MAKE-MAX-HEAP
Running Time
3 points
- Number of nodes of height h is upper bounded by n/2ʰ
- Running time of
MAX-HEAPIFY
on a node of height h is O(h) - T(n) ≤ Σ (n/2ʰ)O(h) = O(n)
Algorithm
Heapsort
HEAPSORT(A) MAKE-MAX-HEAP(A) for i = A.heap-size downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 MAX-HEAPIFY(A, 1)
Knowledge
Heapsort Running Time
O(n log n)
Definition
Priority Queue
An abstract data structure for maintaining a set of elements, each with an associated value called a key, which either gives priority to elements with larger or smaller keys
Knowledge
Operations Supported by a Max-Priority Queue
4 points
INSERT(S, x, k)
MAXIMUM(S)
EXTRACT-MAX(S)
INCREASE-KEY(S, x, k)
Algorithm
EXTRACT-MAX
Priority Queues
HEAP-EXTRACT-MAX(A) if A.heap-size < 1 error "heap underflow" max = A[1] A[1] = A[A.heap-size] A.heap-size = A.heap-size - 1 MAX-HEAPIFY(A, 1) return max
Knowledge
EXTRACT-MAX
Running Time
Priority Queues
O(log n)
Algorithm
HEAP-INCREASE-KEY
HEAP-INCREASE-KEY(A, i, key) if key < A[i] error "new key is smaller than existing key" A[i] = key while i > 1 and A[Parent(i)] < A[i] exchange A[i] with A[Parent(i)] i = Parent(i)
Knowledge
HEAP-INCREASE-KEY
Running Time
O(log n)
Knowledge
Dynamic Programming
5 points
- Define a sequence of subproblems with the following properties:
1. The subproblems are ordered from smallest to largest
2. The largest problem is the original problem
3. The optimal solution of a subproblem can be constructed from the optimal solutions of smaller subproblems (Optimal Substructure property) - Solve the subproblems from smallest to largest, storing each solution
Knowledge
Divide-and-Conquer vs Dynamic Programming
5 points
- DP is an optimization technique whereas D&C is not normally used to solve optimization problems
- Both split the problem into parts, find solutions to the parts and combine them into a solution of the larger problem
- In D&C, the subproblems are significantly smaller than the original problem and do not overlap (share sub-subproblems)
- In DP, the subproblems are not significantly smaller and overlap
- In D&C, the dependency of the subproblems can be represented by a tree whereas in DP, it can be represented by a directed path from the smallest to largest subproblem
Knowledge
The Change-Making Problem
2 points
- Input: Positive integers 1 = x₁ < x₂ < … < xₙ and v
- Output: Given an unlimiteed supply of coins of denominations x₁, …, xₙ, find the minimum number of coins needed to sum up to n
Knowledge
The Knapsack Problem
3 points
- A knapsack holds a total weight of at most W kg
- There are n items, of weight w₁, …, wₙ ∈ ℕ and value v₁, …, vₙ ∈ ℕ
- Find the most valuable combination of items, which can fit into the knapsack without exceeding its weight
Knowledge
The Edit Distance Problem
4 points
- An edit is an insertion, deletion or character substitution
- The edit distance between two words is the minimum number of edits needed to transform one to the other
- Input: Two strings x[1..m] and y[1..n]
- Output: The edit distance between x and y
Knowledge
Depth-First Search
2 points
- Input: A graph G = (V, E)
- Output: A backpointer π[v], discovery time d[v] and finish time f[v] for each v ∈ V