Midterm 2 Flashcards

1
Q

In the master theorem (DP), what are the 3 cases?

A

*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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is important to take into consideration for complete search algorithms?

A
  1. Make sure your search space if Correct & Efficient
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are generators vs filters in Complete Search?

A

Generators: no false start, they go directly to correct answers

Filters: test candidate solutions with possible false starts and backtracking

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 3 ways of solving a DP recurrence?

A
  1. Substitution method
  2. Recurrence tree
  3. Master theorem → T(n) = a*T(n/b) + D(n) + C(n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the values of the sum of geometric series when r < 1, when r > 1, when r = 1?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the “karatsuba trick”?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

CLOSEST POINTS

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What algorithm is used for matrix multiplication? how does it work?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What algorithm allows to find the maximum weight subset of mutually compatble activities in a given schedule?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type of algorithm is FFT?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What type of algorithm is the Knapsack problem?

A

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…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between memoization and tabulation?

A

*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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Does a top down approach to solve an algorithm increase or decrease time and space complexity?

A

Memoization stores previously calculated values → Decreases time complexity, but increases space complexity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you solve the knapsack problem using a greedy approach?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the running times of the Huffman encoding vs decoding algorithms?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the Quick Sort algorithm? Algorithm Paradigm does it relate to?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the time complexity of an algorithm in which each element can be included or not?

A

O (2^n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

True of False?
In a DP solution, the space requirement is always at least as big as the number of it’s unique subproblems.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

True or False?
A greedy algorithm always finds the optimal solution for every problem.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the greedy choice in the scheduling problem seen in class?

A

Chose the earliest finishing time activity that is non-overlapping

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

A divide-and-conquer strategy to multiply two n-bytes integers is always more efficient (faster) than the brute force approach. True or False?

A

B. False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
The Karatsuba algorithm to multiply 2 integers is asymptotically optimal. True or False?
False It is better than the traditional method going from O(n^2) → O(n^1.585) But not the optimal solution, there exists faster algorithms
26
Let T(n)=2*T(n/5)+n^3 be a recursive equation expressing the running time T(n) of a divide-and- conquer algorithm. What is the height of the recursion tree? In words, what does this recurrence equation mean?
C) log_5(n) Describes a divide-and-conquer process where a problem of size n is recursively broken into two smaller subproblems, each of size n/5. The additional term n^3 represents the work done outside of the recursive calls at each level.
27
Given a tree, find the size of the Largest Independent Set (LIS). A set of nodes is an independent set if there are no edges between the nodes. What algorithm paradigm allows to solve this?
Dynamic programming (trees) 1. Original problem: LIS(r) = size of the largest independent set in the tree with root at r 2. Sub-problems: LIS(v) denotes the size of the largest independent set in the subtree rooted at v 3. Decision to make: Include node → exclude its children OR exclude node → can include children or not MIS(v) = max { Sum the children MIS(w), 1+ Sum all the grandchildren MIS(x)) } - w is a child or v - x is grandchildren of v - Sum of grandchildren is double sum → of children of children 4. Store the memory into a tree - Post-order traversal (visit every node before its parent?) Can make this algorithm even simpler by setting 2 functions MISyes and MISno which includes or not the node Time complexity = O(n) Brute force would be O(2^n)
28
What are the MISyes and MISno functions of the Largest Independent Set (in a tree) problem?
*DP problem MISyes(v) = 1 + Sum MISno(children) MISno(v) = Sum max {MISyes(w), MISno(w)}
29
What is the appropriate greedy choice to find minimum number of classrooms to schedule all lectures so that no two lectures occur at the same time in the same room?
Choose non-conflicting activities with the earliest finishing time
30
In what order is a greedy problem solved? What might be required before solving it? What is the greedy-choice property?
Solve TOP-DOWN You may have to preprocess input to put it into greedy order (ex: sorting activities by finishing time) *Greedy requires and optimal substructure + Greedy-choice property!! Greedy-choice property = can build a globally optimal solution by making a locally optimal (greedy) choice.
31
What is the loop invariant for proving correctess of MergeSort?
- At the start of each iteration of the for loop, subarray A[p..k – 1] contains the k – p smallest elements of L and R in sorted order. - L[i] and R[j] are the smallest elements of L and R that have not been copied back into A. (Sentinels) Initialization: Before the first iteration: - A[p..k – 1] is empty, k = p => k – p = 0. - i = j = 1. - L[1] and R[1] are the smallest elements of L and R not copied to A. Maintenance: Case 1: L[i] <= R[j]: - By LI, A contains k – p smallest elements of L and R in sorted order. - By LI, L[i] and R[j] are the smallest elements of L and R not yet copied into A. - Line 13 results in A containing k – p + 1 smallest elements (again in sorted order). Incrementing i and k reestablishes the LI for the next iteration. Case 2: Similar arguments with L[i] > R[j] Termination: - On termination, k = r + 1. By LI, A[p..k-1], which is A[p..r], contains k-p=r–p+1 smallest elements of L and R in sorted order. - LandRtogethercontainn1 +n2 +2=r–p+3 elements including the two sentinels. All but the two largest (i.e., sentinels) have been copied in A.
32
What is the running time of Binary Search?
Divide and Conquer → O(log n) Recurrence = T(n) = T(n/2) + 1
33
What is the recurrence equation for MergeSort?
Base case → T(n) = O(1) for n = 1 Otherwise → T(n) = 2T(n/2) + O(n) + O(1) T(n) = O(n long n) *O(1) = time to divide O(n) = time to merge n elements 2T(n/2) = conquer the 2 subproblems
34
In a recursion tree that follows the following recursive relation, how many levels are there? T(n) = 2T(n/2) + cn
log n + 1 levels - height of log base 2 of n because counts the edges - Every level requires cn work cn * (log 2 n + 1) → T(n) = cn*log n + cn → O(n log n) *This assumes n is a power of 2
35
In a Divide & Conquer algorithm represented by the recurrence T(n) = a*T(n/b) + c*n^d What do a, n/b and c*n^d represent? How many leaves does this tree have?
a = # subproblems (bad as it increases with each recursive call) n/b = size of each subproblem (good because shrinks with each recursive call) n^d = work done outside of recursive calls Tree has a^(log base b of n) leaves Height = log base b of n
36
How long would a method calculating the power of x^n take?
We can calculate power using divide and conquer in O(log n) time
37
How does Linear Scan with Pairwise Comparison work and what is its running time?
It can process elements in pairs, comparing them with each other first and then comparing the smaller one with the current minimum and the larger one with the current maximum. This reduces the total number of comparisons to about 3/2 (n-1) in the worst case - 1 comparison/pair for which is largest and which is smallest - 2 comparions/pair with the min or max respectively - n/2 pairs - If n is odd, the last element is compared with the max and min adding 2 comparisons
38
What is the trick for solving the following recursion? T(n) = 2*T(sqrt(n)) + 1 Ex2: T(n) = 2*T(sqrt(n)) + log n
Let n = 2^m T(2^m) = 2* T( 2 ^ m/2 ) + 1 Let S(m) = T(2^m) S(m) = S(m/2) + 1 Ex2: T(2^m) = 2* T( 2 ^ m/2 ) + m S(m) = S(m/2) + m DON'T FORGET TO SWITCH BACK TO N (log n)
39
Which of the following problems is NOT solved using dynamic programming? a) 0/1 knapsack problem b) Matrix chain multiplication problem c) Edit distance problem d) Fractional knapsack problem
The fractional knapsack problem is solved using a greedy algorithm
40
A greedy algorithm always finds the optimal solution for every problem.
FALSE It always finds the locally optimal solution, but this does not mean it is the globally optimal solution
41
What type of algorithm paradigm relates to the following problems? Knapsack problem Generate permutations Huffman coding Quicksort
Knapsack problem → DP Generate permutations → Complete search Huffman coding → Greedy Quicksort → Divide & Conquer
42
What would an algorithm generating all possible permutations be like?
Recursive backtracking: iterating through each element in the set, adding it to a current permutation, then recursively calling the function to generate permutations with the remaining elements, and finally "backtracking" by removing the element and trying the next option at the current position before moving on to the next position in the permutation Base Case = all elements have been added to the current permutation, store the permutation as a solution and return Recursive step: For each element in the remaining set: Add the element to the current permutation. Recursively call the function with the updated permutation and the remaining set excluding the chosen element. Backtrack by removing the element from the current permutation to explore other possibilities.
43
There is a problem wanting to generate all subsets Time complexity?
Complete Search: 1. Start with an empty subset. 2. At each step, decide whether to include the current element. 3. Recursively explore both choices (include or exclude the element). 4. Backtrack by removing the last added element to explore new possibilities. Time complexity: - Each element can be included or not → 2^n subsets - Copying a subset takes O(n) Total = O(n*2^n) DP approach: For every subset of already existing in dp, create a new additional subset in which you just add the next element and store it in dp. Time Complexity: - 2^N subsets in total - For each element, we iterate of all existing subsets → O(n* 2^n) Space complexity → O(2^n)
44
What are the seps for solving the placing queens problem?
Complete Search: base case → row == n → all the queens have been placed → add it to the solutions You are going row by row and trying the different columns for every row If valid solution → SolveQueen (row + 1, board, solutions) Backtrack (remove queen from the current board) O(n!)
45
Why does the Master Theorem not apply to this recurrence relation? T(n) = 8* T (n - srqt(n)/4 ) + n^2?
Non-exponential reduction of problem size: The problem decreases very slowly in size, at rate n - Sum from i = 0 to k (sqrt(n^i)/4 ) About sqrt(n) level in that tree (LOTS)
46
Given a staircase with n steps, you can climb either 1 step or 2 steps at a time. How many distinc ways can you reach the top? Which of the following is the correct bottom-up recurrence relation for solving this using DP? What are time and space complexities?
f(n) = f(n - 1) + f(n - 2) Base cases → f(1) = 1 // f(2) = 2 Code: 1. Check base case 2. if memo.containsKey(n) → return memo.get(n) 3. result = f(n - 1) + f(n - 2) 4. memo.put(n, result) 5. return result Time complexity = O(n) (each subproblem computed once) Space complexity = O(n) (memoization table) Iterative solution: Set an array dp at which every index stores the number of ways to go up to the n level for (i = 3; i < dp.length; i++) { dp[i] = dp[i-1] + dp[i-2] } return dp[n] If we were not using DP, every step makes 2 recursive calls and recursion deapth ~ n → O(2^n)
47
Given an integer n, the task is to find the total number of unique Binary Search Trees (BSTs) that can be made using values from 1 to n. Which of the following is the correct recurrence relation to compute this using DP?
T(n) = Sum from i = 0 to n [ T(i-1)xT(n-i) ] - Every number can be the root - All possibilities of right AND left subtrees with that root Base Case T(0) = 1
48
In the longest subsequence array problem. A) If we know that the input is an array which is strictly decreasing, what would be the time complexity of the solution? B) If we know that the input is an array which is strictly decreasing, what would be the time complexity of the solution?
A) O(1) B) O(n) → the full array
49
In the longest subsequence array problem. What would be the time complexity of a brute force approach to this problem?
A brute force approach would involve checking every single possible subsequence and returning the maximum length subsequence. 2 choices: - Include the number in the subsequence (if maintains the increasing order) → add and move to next index - Exclude the number → just move to next index Return maximum of both choices Time complexity → O(2^n) because choose for each index *Not most efficient!!
50
What is the time complexity of a brute force approach to finding a path so that the knight visits all slots of a chess board?
O (8^N) - 8 possible moves for each position - Explore up to N^2 positions as the board is NxN - Usually runs faster due to pruning (backtracking when valid move available) Check base case → return true For each position, for (i = 0; i < 8; i++) → try all 8 possible moves Call recursively → if it returns true, it leads to a solution Backtrack the current board Return false if not valid move is found O(8 ^ N^2) is better than O(N^2 !) which is the case if you take all possible paths and check that they are possible knight paths
51
Given the root of a balanced binary tree, return all root-to-leaf paths in any order. What is the algorithm paradigm used? what is the time complexity?
- N elements are visited exaclty once (n/2 leaves) - In the worst case, each element must concat with all log(N) elements before it O (n log n) *For an unbalanced tree → O(n^2) the total work done is O(n) * O(log n) = O(n log n) if we were explicitly storing each path as a string, but counting the numbers of root-to-leaf paths is O(n) Brute force = start at the root and rcursively try all paths DP = NO OVERLAPPING SUBPROBLEMS
52
How can Divide and Conquer be used to for matrix multiplication? What is the time complexity?
Divide each NxN matrix into 2 N/2 x N/2 matrices T(n) = 8 T(n/2) + O(n^2) - O(n^2) → work to combine the subproblems - Master's Theorem → O(n^3), not better than normal method Strassen's method: T(n) = 7T(n/2) + O(n^2) → avoid 1 multiplication
53
The Coin Row Problem: Suppose you have a row of coins with values that are positive integers c1, · · · , cn. These values might not be distinct. Your task is to pick up coins have as much total value as possible, subject to the constraint that you don’t ever pick up two coins that lie beside each other. How would you solve this using dynamic programming? What is the time complexity?
Recurrence relation: f(n) = max {cn + f(n-2), f(n-1)} base cases: f(1) = c1 // f(0) = 0 // f(2) = max{c1, c2} Time complexity: For every coin you have 2 choices → O(2^n) But DP avoids redundant computations, so it only traverses the array once and stores in memory → (n) (each computation takes O(1))
54
What case of the master's theorem applies if n^log base b of a = sqrt(n) and f(n) = log n
sqrt(n) > log n → Case 1 Worst case = leaves → O( n^log base b of a) → O( sqrt(n) )
55
Write a recurrence that describes its worst-case running time of the quicksort algorithm. Write a recurrence that describes the average case of the quicksort algorithm.
T(n) = T(n − 1) + T(0) + O(n) Worst case: pivot is the largest/smallest element at every recursion T(n-1) → recrusviely sorting n-1 element as they were all sent to the same side of the pivot O(n) → Partition step takes linear time in this case **O(n^2) Average case: T(n) = 2T(n/2) + O(n) → O(n log n)
56
How is the following recurrence solved? T(n) = T(n-1) + O(n) vs T(n) = T(n-1) + 1
T(n) = T(n-2) + O(n-1) + O(n) ... T(n) = O(1) + O(2) + ... O(n) Sum from i=1 → n = n(n+1)/2 = O(n^2) T(n) = T(n-1) + 1: T(n) = T(n-2) + 1 + 1 ... T(n) = T(1) + (n-1)*1 = x + (n-1) T(n) = O(n)
57
How many unique solutions exist of placing 8 queens in an 8x8 board? What is the running time of a naive backtracking approach
12 unique solutions (generator) Naïve backtracking = O(N!) → doesn't consider symmetry of the board
58
Complete search inolves an exhaustive search of all possible solutions, and there are no optimization that can be made. True or False?
False - Optimize the search space
59
In a DP solution, the space requirement is always at least as big as the number of unique subproblems. True or False?
False Ex: Fibonnacci → you replace the slots when not useful anymore F(n) = F(n-1) + F(n-2) Ex: Could rearrange 1D rolling array for knapsack/subsets instead of 2D array
60
In a proof of correctness of a recursive algorithm, the base case is equivalent to the termination condition.
False Base case = simplest input Termination condition = when the recursion stops