Midterm 2 Flashcards
In the master theorem (DP), what are the 3 cases?
*a = # subproblems (>=1, cste), n/b = size of subproblem (b>1)
T(n) = n^d * SUM(a/b^d) ^ i
f(n) assymptotocally positive
1) n^(log b a) > f(n) → total work increases at every levels → worst case = leaves → O(n ^ log base b of a)
2) n^(log b a) = f(n) → total work stays the same → O(n^d log n) → work n^d for every node at level n
3) n^(log b a) < f(n) → total work decreases at every level → worst case = root → O(n^d)
*Regularity condition: af(n/b) <= c f(n) for some c <=1
What is important to take into consideration for complete search algorithms?
- Make sure your search space if Correct & Efficient
What are generators vs filters in Complete Search?
Generators: no false start, they go directly to correct answers
Filters: test candidate solutions with possible false starts and backtracking
What are the 3 ways of solving a DP recurrence?
- Substitution method
- Recurrence tree
- Master theorem → T(n) = a*T(n/b) + D(n) + C(n)
What are the values of the sum of geometric series when r < 1, when r > 1, when r = 1?
r < 1 → Sn = SUM (from k=0 → inf.) r^k = 1/ 1 - r
r > 1 → Sn = SUM (from k=0 → n) r^k = 1 - r(n+1) / 1 - r
What is the “karatsuba trick”?
The “Karatsuba trick” for multiplication is a divide-and-conquer algorithm that allows you to multiply two large numbers by splitting them into smaller parts, performing only three multiplications on those smaller parts, and then cleverly combining the results to get the final product, significantly reducing the number of calculations needed compared to standard multiplication methods.
1234x5678
Without the trick: 1234x5x10^3 + 1234x6x10^2 + 1234x7x10^3 + 1234x8x10^0
T(n) = n^2 (not recursive)
With the trick: (12+34)x(56+78) + 12x56 + 34x78
T(n) = 3 t(n/2) + cn
(c is larger because more additions)
What algorithm paradigm do these examples relate to?
a) Karatsuba trick
b) Game Trees
c) Fibonacci
d) Matrix multiplication
e) Placing 8 queens on an 8x8 chess board
f) Closest points
g) Master’s Theorem
h) MergeSort
Placing 8 queens on an 8x8 chess board → Complete Search
Game Trees → Complete search/recrusive backtracking
MergeSort → Divide & Conquer
Master’s Theorem → Divide & Conquer
Karatsuba trick (multiplication of integers) → Divide & Conquer
Closest points → Divide & Conquer
Matrix multiplication → Divide & Conquer (Strassen’s trick)
Fibonacci → Dynamic programming (overlapping sub-problems)
CLOSEST POINTS
What algorithm is used for matrix multiplication? how does it work?
Strassen’s trick → uses Divide & Conquer
Strassen’s “trick” for matrix multiplication is a divide-and-conquer algorithm that reduces the number of necessary multiplication operations by cleverly combining submatrices of the original matrices, allowing for faster matrix multiplication compared to the standard method, especially for large matrices; essentially, instead of performing 8 multiplications for 2x2 matrices, Strassen’s method achieves the result with only 7 multiplications by strategically adding and subtracting submatrices before multiplying them
O(n^3) → O(n^2.81)
What algorithm allows to find the maximum weight subset of mutually compatble activities in a given schedule?
Dynamic programming:
1. Sort activities by finishing time
2. Make the p(j) be the largest index i < j such than that activity i is compatble with activity j
OPT(j) is the optimal solution (set of activities giving max total weight)
Case 1: OPT selects activity j → Add weight j + can’t use incompatible activities → call on p(j)
Case 2 Opt does not select activity j → call on j-1
Base case = 0, if j = 0
OPT(j) = max {wj + OPT( p(j) ) , OPT (j-1) } Otherwise
*Overlapping problems = OPT(4), OPT(3) etc.
Running time = O(n) of jobs are already sorted by finishing time, O(n log n) otherwise
What type of algorithm is FFT?
Fast Fourier Transforms = divide and conquer algorithm
FFT speeds up multiplication of large numbers and polynomials from O(n^2) → O(nlogn) using the Convolution Theorem.
What type of algorithm is the Knapsack problem?
Dynamic Programming
Context → Each objects has a weight and a value, want to fill the knapsack (fill the max weight) to maximize the total value
Subproblems = Include or reject a specific object
After an intem n is included in the solution, a weight of wn is used up and there is W - wn available weight left
Optimal substructure = best set of item with weight W - wn
OPT(i, w) = max OPT(i - 1, w) and v + OPT(i-1, w - wi)
2D memory storage…
What is the difference between memoization and tabulation?
*Dynamic Programming
Tabulation:
- Bottom-up
- All possibilities are explored
- Iterative
- Precomputed values
- Explicit table
Memoization:
- Top-down
- ONLY necessary possibilities are explored
- Recursive
- Computed on demand
- No explicit table
Does a top down approach to solve an algorithm increase or decrease time and space complexity?
Memoization stores previously calculated values → Decreases time complexity, but increases space complexity
How can you solve the knapsack problem using a greedy approach?
knapsack problem using the greedy method, calculate the “value per unit weight” (value/weight) for each item, then sort the items in decreasing order of this ratio and add items to the knapsack as much as possible, prioritizing those with the highest value per unit weight until the knapsack capacity is reached
What are the running times of the Huffman encoding vs decoding algorithms?
Encoding → maintain the tree in priority queue ordered by weight → O(C log C)
Decoding →
Both Huffman encoding and decoding algorithms have a time complexity of O(n log n), where “n” represents the number of unique symbols in the input data; meaning they essentially take the same amount of time to run, with the primary difference being the operations performed during each step of the process
During encoding, the algorithm traverses the constructed Huffman tree to generate the variable-length code for each symbol, which takes O(log n) time per symbol due to the tree structure.
Decoding is also O(n log n) as it involves traversing the Huffman tree based on the received bitstream, where each bit read corresponds to a decision on which branch to follow in the tree
*The space complexity of the Huffman algorithm is also O(n) as it needs to store the Huffman tree which has a maximum of 2n-1 nodes
What is the Quick Sort algorithm? Algorithm Paradigm does it relate to?
QuickSort and MergeSort are both Divide and Conquer
QuickSort partitions the array around a pivot element → all smaller elements go to the left and all larger elements go to the right → do this recursively
Worst case = O(n^2) ex: reverse sorted array
Best and Average cases = O(n log n)
*Compared to merge sort, no aditionnal memory is require (in mergeSort, additional memory to combine results of subarrays)
*Worst case of MergeSort = O(n log n)
Which of the following is not true about the merge sort algorithm?
A) Merge sort has a complexity of O(n log n) in all cases.
B) Quick sort has a slower worst-case time complexity than merge sort.
C) Merge sort is a comparison based sorting algorithm which partitions the array into two equal subarrays.
D) In merge sort, the entire algorithm is in-place.
D) In merge sort, the entire algorithm is in-place.
MergSort is out of place → requires additional space when combining subarrays → O(n) space complexity
QuickSort is in place → not additional space → O(log n) space complexity
What is the time complexity of an algorithm in which each element can be included or not?
O (2^n)
True of False?
In a DP solution, the space requirement is always at least as big as the number of it’s unique subproblems.
Space Optimization:
In some problems, you can reduce the space complexity by recognizing that not all previous subproblems need to be stored at once. For example, if the solution to a problem at step n only requires the solutions from the previous step (or a small subset of previous steps), you can overwrite old results and reduce space usage.
Example: For the Fibonacci sequence, a simple DP solution uses only two variables (storing the current and previous values) instead of an entire table.
True or False?
A greedy algorithm always finds the optimal solution for every problem.
A greedy algorithm does not always find the optimal solution for every problem. It works by making the locally optimal choice at each step in the hope that these choices will lead to a globally optimal solution. However, this approach does not always guarantee the best overall result.
Greedy makes the locally optimal choice in the hope that the choice will lead to a globally optimal solution
- Never have to reconsider our previous choices
- Similar to divide and conquer in a way
The greedy choice is a property that enable us to make a locally optimal choice at each step of the algorithm. Which of the following assertions are true?
A) It requires to define optimal sub-structures.
B) The algorithm is usually fast.
C) It always guarantees to return an optimal solution for any problem where it is applied.
All of them are true:
A) It requires to define optimal sub-structures.
B) The algorithm is usually fast.
C) It always guarantees to return an optimal solution for any problem where it is applied.
What is the greedy choice in the scheduling problem seen in class?
Chose the earliest finishing time activity that is non-overlapping
A divide-and-conquer strategy to multiply two n-bytes integers is always more efficient (faster) than the brute force approach. True or False?
B. False