CS6515 - Algorithms- Exam 1
Nth roots of Unity
(1, 2*PI*j/n) for j = 0, 1, ..., n-1 *Around the Unit Circle!
what happens when you square ((ω_n)^(j))
- ((ω_n)^(j)) == (1, (2pie/n)*j)^2 - == (1,(2pie/(n/2))*j)) == (ω_(n/2))^j
- What is the radian coordinates which degree = 0? - How would we add 180 degrees to it?
- (r,theta) - (r, theta) * (1,pie)
How do you construct a matrix for an FFT
- Take A(1), A(W_n)... A((ω_n)^(n-1)) == - 1 for the top row and column - for each row, take that ω and square it - so (ω^2) == (ω^2, ω^4 ...ω^(2(n-1)) )
What should you remember about knapsack no repeat problem
- max of (up: the previous, or way back value + v_i)
How do you backtrack in edit distance?
-start at the end - if the index values are the same go diagonally - if they are not, select the min of the diagonal up and left and then move
logb(b)
1
- What numbers can be used when n = 4 for the roots of unity? - Can you describe why that is the case?
1,-1,i, -i
What does the following represent? 1.) A = M_n(ω_n)a = FFT(a,ω_n) 2.) how would you calculate the inverse? 3.) What would the function call be for the inverse?
1.) THis represents the result of a regular FFT function. You are trying to get the function A or values by using the FFT function with inputs the coefficients and ω_n 2.) a.) M_n(ω_n)^-1 *A = a b.) (1/2) * M_n((ω_n)^-1) 3.) a = (1/2)FFT(A, (ω_n)^n-1)
Consider the n-th roots of unity for n =16. WHat is ω_16 in polar coordinates?
At point 0, polar coordinates are (r, theta) == (1, 2pie/2), if n = 16, then it becomes - (1, 2pie/2)^16 == (1,2pie/16) == (1, pie/8)
Why is all pairs Dist(y,z) n^2?
Because it builds a two dim table!
FFT and Inverse FFT Formulas
FFT = Mn(w) x B Inverse FFT = 1/n Mn(w^-1) x B
longest common subsequence algo
LCS(X,Y) for i = 0 to n: L(i, 0) = 0 for j = 0 to n: L(0,j) = 0 for i = 1 to n for j = 1 to n if X_i == Y_j: L(i,j) = L(i - 1, j - 1) + 1 else: L(i,j) = max(L(i - 1, j),L(i,j-1) return(L(i,j)
Longest Increasing Subsequence
LIS(a_1.... a_n) for i = 1 to n L(i) = 1 for j = 1 to n -1 if a_j < a_i & L(i) < 1 + L(j) L(i) = 1 + L(j) max = 1 for i = 2 to n if L(i) > L(max) then max = i return(L(max))
what is Big oh of LCS?
O(n**2)
When the sequence of an FFT is being requested, what does that mean?
The inverse FFT is desired - recalculate your matrix (-i) typically
In the FFT algorithm, why does "j" in the last section only go to (n/2 - 1)?
This is because each portion performs half of the function, similar to the root unity circle traversal
what use "n" to describe the subscript of ω?
This is to help describe
Euler's Formula
e^ix = cosx + isinx
Knapsack without repetition
k(0) = 0 for w = 1 to W: if w_j >w: k(w,j) = k(w, j - 1) else: K(w,j) = max{K(w, j -1),K(w - w_j, j -1) + v_i}
knapsack with repetition
knapsack repeat(w_i....w_n, w_i... w_n, B) k(0) = 0 for i = 1 to n if w_i <= b & k(b) <v_i + K(b-w_i) then k(b) = v_i + K(b-w_i)
loga (uv)
loga (u) + loga (v)
loga (u / v)
loga (u) - loga (v)
what are the different types of filtering?
mean filtering = 1/(2m+1) gaussian filtering: f = 1/z(e^(-m^2), gaussian blur: 2-dim
loga (u)^n
n * loga (u)
If you are given: A(x) = 1 + 2x + 3x^2 B(x) = 2 -x + 4x^2 You need to get the polynomial multiply, what do you do?
simply multiply, and list from coefficient, to x ascending exponent
How do you convert the cartesian coordinates (a,b) to polar coordinates?
(a,b) = rcos(theta), rsin(theta)
Consider w_16. For what power k is (ω_16)^k = -1?
(ω_16)^8; This is because it adds 180*(7/8) to (w_16)^1 Also, zero exponent makes 0, that would be (w_16)^0
Steps to solve for FFT
1) Write out Matrix Coefficient Form based on n (size of input) Mn(w) = [ 1 1 ... 1 1 w ... w^n-1 ... 1 w^n-1 ... w^((n-1)*(n-1)) ] 2) Find value for w = e^(2*PI*i)/n, Substitute in Mn(w). 3) For the input coefficients into nx1 matrix. I.E. [4 0 1 1], let known as B. 4) Evaluate FFT: a) FFT of Input = Mn(w) x B b) Inverse FFT of Input = 1/n * Mn(w^-1) x B
Steps to solve a Dynamic Programming Problem
1. Define the Input and Output. 2. Define entries in table, i.e. T(i) or T(i, j) is... 3. Define a Recurrence relationship - Based on a subproblem to the main problem. (hint: use a prefix of the original input 1 < i < n). 4. Define the Pseudocode. 5. Define the Runtime of the algorithm. Use Time Function notation here => T(n) = T(n/2) + 1...
what are the two representations of A(x)? WHich is more convenient for multiplying polynomials?
1.) coefficients 2.) values: A(x_1), A(x_2)....A(x_n) Values are more efficient to do so
1.) what is the product of the roots of unity? 2.) What about the sum? 3.) What is the sum of Aeven? 4.) what is the sum of Aodd?
1.) if it is even, then it is == 1 if it is odd, then it is == -1 2.) The sum is == 0. This is because (ω_n)^0 == 1 and (ω_n)^(n/2) == -1. These cancel each other out. So if you go through the whole sequence.. it eventually becomes 0. 3.) (n/2) * 1 4.) (n/2) * -1
what is the opposite of (w_n)^j? For example, (w_16)^2
16/2 = 8; 2 + 8 = 10; therefore opposite is: (w_16)^10
How can you express the inner product of ω_n matrix and coefficients of "a"? How can it be represented for the inverse?
A = M_n(ω_n) * a == FFT(a,ω_n)
what is the base case for the bellman ford algorithm?
D(0,s,t)
WHat is the FFT algorithm of a single function?
FFT(A,ω) if n = 1 then return a_0 else let Aeven = [a_0, a_2... a_(n-2)] let Aodd = [a_1, a_3... a_(n-1)] FFT(Aeven, ω^2) = ...s_n FFT(Aodd, ω^2) = ...t_n for j = n/2 - 1 r_j = s_j + ω^j * t_j r_((n/2)+j) = s_j - w^j * t_j
Why are off diagonal entries not equal to one?
In the original, it is (ω_n)^k * (ω_n)^-k == (ω_n)^0 == 1 However, the off-diagonal entries are: (ω_n)^k * (ω_n)^-j == (ω_n)^(k-j); so result of 1 is definitely not guaranteed
When calling FFT with the "even" and "odd" partitions of your function (lets say "A"), what happens to "ω"?
It is squared
what is the running time of 2 dim matrix multiplication probelm
O(n**3)
FLoyd-Warshall run time?
O(n^3)
In edit distance, what does each of the 4 operations do?
T(i-1, j-1) + 1: diagonal, means editing T(i-1, j) + 1: up, means inserting T(i, j-1) + 1: left, means deleting
logb(b^x)
x
what is another way to express -ω_16??
ω^9_16, this is because it moves 1/16 into the lower left quartile
What is more efficient in terms of complexity? Polynomial Multiplication with 1.) values or 2.) coefficients
- values values == O(n) time (from 1 -> 2n) coefficients == O(n^2) time
what do you need to remember when it comes to lI subsequence?
- you are work with one array - you fill everything out with 1s first based on an array of same size - the value is compared as well as the index is compared that is why (a_
What is the difference between FFT of one item vs FFT multiplication of two functions?
- ω is not squared - there is no odd even separation - coefficients produced are n = 0 to 2_n -1, same for the only j loop - lastly, you multiply the coefficients together - then have to do the inverse FFT
what is meant by roots of unity?
1 to be exact
what degree is -1 in terms of polar coordinates? what are the polar coordinates?
180 degrees (positive starts on the right side) - (1, pie)
At what point does the roots of unity repeat: essential, what does n equal in order to go 360 degrees?
8, after 8 it repeats
How do you perform polynomial multiplication with: A(x) convolution B(x) in O(n^2) time?
C(x) = a_0 * b_(n-1) + a_1*b_(n-2)..... A_(n-1)*b_0 you have to loop through each each twice, therefore it becomes O(n^2)
DC: Arithmetic Series
Given d = common difference and a = first term in series => a + (a + d) + (a + 2d) + ... + (a + (n-1)d Sum = n/2 * [2*a + (n-1)d]
DC: Geometric Series
Given r = common ratio and a = first term in series => a + ar + ar^2 + ar^3 + ... + ar^(n-1) => a * [(1 - r^n) / (1-r)]
DC: Solving Recurrences - Master Theorem
If T(n) = aT([n/b]) + O(n^d) for constants a>0, b>1, d>=0: T(n) = { O(n^d) if d > logb(a) O((n^d)logn) if d = logb(a) O(n^(logb(a))) if d < logb(a) }
How to translate i^6 to i or 1?
If it is less than 4 remember the table: i, -1, -i, 1 if greater, do exponent mod 4, and whatever the remainder is
DP: Types of Subproblems (4)
Input = x1, x2, ..., xn 1) Subproblem = x1, x2, ..., xi ; O(n) 2) Subproblem = xi, xi+1, ..., xj ; O(n^2) Input = x1, x2, ..., xn; y1, y2, ..., ym 1) Subproblem = x1, x2, ..., xi; y1, y2, ..., yj ; O(mn) Input = Rooted Binary Tree 1) Subproblem = Smaller rooted binary tree inside the Input.
what is the derivation of the +/- property?
It is the opposing sides of the nth roots of unity (ω_n)^0 = -(ω_n)^(n/2) (ω_n)^1 = -(ω_n)^((n/2)+n) ... (ω_n)^((n/2)-1)= -(ω_n)^((n-1)
how to calculate longest increasing substring. Try it using the following: 541208563
It should only require two separate loops: one to loop to go through to calculate the values - then a separate loop to figure out what the max is... and then return the max
DC Algorithms and Runtimes (6)
MergeSort: O(nlogn) - Split the input array into two halves and recursively sort and merge at the end. QuickSort: O(nlogn) - Same splitting strategy as MergeSort. QuickSelect: O(n) BinarySearch(S, x): O(logn) - Split array into 2 sub-arrays, if x < current mid, recursively split and search the left sub-array. Otherwise, recursively split and search the right subarray. Median: O(nlogn) Selection(S, x): O(n) - Split the input array into 3 sub-arrays where elements are < x, > x and = x. Recurse until x is found or not.
what is big Oh of longest common substring?
O(mn)
What is the solve time for FFT
O(nlogn)
what is the run time of bellman ford algoirthm? How about if you had to do it for all edges?
O(nm) O(n^2m)
why is the point before ω^(n-1) right before w^n?
This describes the step (whether it is in 4ths, 8ths, or 16ths, right before reaching radian 2piei/n, whether is the degree of "0")
What is important to remember about the inverse of "ω"?
You are trying to figure out, what is required that.. if multiplied, would make your (ω) == 1. - In the case of (ω_8)^2, the inverse would be ω_8)^6 - In the case of (ω_8) (which exponent == 1), the inverse would be ω_8)^7
What are the different ways the following ω can be represented? a.) (ω_16)^4
a.) (ω_8)^2 or (ω_8)^2
in degrees, what are the values of the following: cos(0) cos(90) cos(180) cos(270) cos(360) Do the same for sin()
a.) 1 b.) 0 c.) -1 d.) 0 e.) 1
how does bellman ford and floyd differ when it comes to detecting negative weight cycles?
bellman == can only find it if it can be access from the "s" or start vertex
what is euler's formula?
cos(theta) + isin(theta) = e^(i*theta)
What is the inverse of cos(x) + isin(x)?
cos(x) - isin(x)
if you are presented with a coins (1,5,6,8) and your knapsack is k = 11, what is the minimum number of coins, what would be the computational time, and what is the recurrence?
https://www.youtube.com/watch?v=Y0ZqKpToTic
Imaginary Number Multiples
i = i, i^2 = -1, i^3 = -i, i^4 = 1 i = -i, i^2 = -1, i^3 = i, i^4 = 1
what takes more time, longest increasing subsequence or longest common subsequence?
increasing for sure!!! O(n**2)
What is the direction of roots of unity calculation with FFT vs inverse FFT?
inverse FFT == counterclockwise FFT == clockwise
what is the significance of z = a + bi
it can describe a point in a cartesian plane
Consider ω_16. For what power k is (ω_16)^k = -ω_16?
it is (ω_n)^(1+(n/2)), in the case of ω_16, n/2 = 8, (ω_16)^(1+8) = (ω_16)^9
what is the difference between: 1.) ((ω_n)^(j)) and 2.) ((ω_n)^((n/2)+j))
it is the oppose point. (ω_n)^(n/2) == -ω_n so (ω_n)^((n/2)+j == (-ω_n)^j
For (ω_n)^2, what is its multiplicative inverse? More precisely, for what power k is (ω_n)^k × (ω_n)^2 = 1?
k = (n-2) It is whatever it takes to get to 0, which becomes one so if applied: For (ω_16)^2 *(ω_16)^(16-2) == (ω_16)^16 == (ω_16)^0 == 1
if you want -ω_16 and you are currently at (w_16)^3, what is k in (w_16)^3+k?
k = (n/2) + 1 - 3 == 6, k = 6!!
Consider ω_16. For what power k (ω_16)^-1 = ω_16^k? In other words, for what k is ω_16 * (ω_16)^k = 1?
k == 15 This is because (ω_16)^16 == (ω_16)^0
For what power k is (ω_16)^k = (ω_8)^2?
k == 4
what is important to remember when calculating longest common subsequence as opposed to substring?
substring is very diagonal and plus 1 subsequence you have to use a max function
explain minimum distance algorithm: perform it using "abcdef" and "a3ced"
the minimum number of edits and deletes to have to two strings match - To figure it out you need to get the min{ T(i,j-1), T(i-1,j), and T(i-1, j-1)} + 1
why is omega increased in the exponent in order to traverse through the roots of unity?
theta = 2pie/n, and omega_n = (1, 2pie/n). Multiply them together to increase each step
Omega(w)
w = (1, 2*PI / n) = e^(2*PI*i/n)
what is the point on the roots of unity circle (using omega) where it is zero degrees?
w^0_n == 1
How do you tell if a graph has negative edges?
when fitting graph on a table, if the number of moves decrease the w() from edge to edge, then there is a negative edge; check from 1 to n
Why is the dynamic programming version of Fibonacci faster than the recursion?
with DP: after dealing with base cases 1 and 2, you have a linear path from 2 to n, which allows you to use previously complete parts for future problems
b^(logb(x))
x