Exam 1 Flashcards

1
Q

Master Theorem (Book)

A

if d>log_b(a): O(n^d)
if d=log_b(a): O(n^d log(n))
if d < logb(a): O(n^log_b(a))

T(n) = aT(⌈n/b⌉) + O(n^d)

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

Binary Search

A

requires a list be sorted.

BinarySearch(sortedArray):

mid = (high - low) / 2
if(mid = goal):
return mid
if(midgoal):
BinarySearch(low,mid-1)

return -1

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

1 + 2 + 3 + 4 + … + (n-1) = ?

A

( (n-1) * n ) / 2

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

Longest Increasing Subsequence (LIS) Recurrence

A

T(i) =1
If a[j] < a[i]:
T(i) = max{ T(i), T(j) + 1) }for 1<=i<=n and 1<=j

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

Longest Common Subsequence (LCS) Recurrence

A
T(i,0) = 0 for 0\<=i\<=n 
T(0,j) = 0 for 0\<=j\<=m 

If (x[i] != y[i]):
T(i,j) = max{ T(i-1,j), T(j,i-1) }
else:
T(i-1,j-1) + 1

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

Edit Distance Recurrence

A
T(i,0) = 0 for 0\<=i\<=n 
T(0,j) = 0 for 0\<=j\<=n 

T(i,j) = min{ 1+T(i,j-1), 1+T(i-1,j), diff(i,j) + T(i-1,j-1)}

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

Knapsack (no repeats) Recurrence

A
T(b,0) = 0 for 0\<=b\<=B 
T(0,i) = 0 for 0\<=i\<=n 

if(w[i] <=b):
T(b,i) = max{ T(b,i-1), T(b-w[i],i-1) + v[i] }
else:
T(b,i) = T(b,i-1)

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

Knapsack (with repeats) Recurrence

A
T(b,0) = 0 for 0\<=b\<=B 
T(0,i) = 0 for 0\<=i\<=n 

if(w[i] <=b):
T(b,i) = max{ T(b,i-1), T(b-w[i],i) + v[i] }
else:
T(b,i) = T(b,i-1)

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

Fast Matrix Multiplication Runtime

A

O(n^3)

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

Difference between Bellman-ford and Floyd-Warshall algorithms.

A
  • Bellman-ford: single-source shortest path. If the negative weight cycle is not reachable from the start vertex, it won’t find them. It finds negative weight cycles reachable from a single source. Runs in O(nm) where n=vertices and m=edges.
  • Floyd-Warshall: all-vertices shortest path. Finds negative weight cycles anywhere in the graph. Runs in O(n^3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Runtime of Dijkstra’s algorithm

A

O((n+m) log(n)) where n is vertices and m is edges

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

Does Dikjstra’s algorithm work if negative weights are allowed

A

No. It requires all edge weights are positive. It’s not guaranteed to produce the right output when the edge weights can be positive.

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

Fibonacci’s Numbers Recurrence

A
T(0) = 0 
T(1) = 1 
T(n) = T(n-1) + T(n-2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dictionary Lookup (Sequence of words?) Recurrence

A
T(0) = True
T(i) = False
T(i) = T(i) or ( T(j-1) and dict(j...i) ) for 1\<=i\<=n and 1\<=j\<=i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Longest Common String Recurrence

A
T(i,0) = 0 for 0\<=i\<=n 
T(0,j) = 0 for 0\<=j\<=m 

If x[i] = y[j]:
T(i,j) = T(i-1,j-1) + 1
else:
T(i,j) = 0

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

Inputs to FFT and IFFT

A

FFT: (A,w_n^1) where A = n-sized matrix of polynomial coefficients.
IFFT: (A’,w_n^-1) * 1/n where A’ = n-sized matrix of values representing polynomial A.

17
Q

What is the sum of the nth roots of unity for even n?

A

0

18
Q

What is the product of the nth roots of unity when n is odd and when n is even?

A
Even = -1
Odd = 1
19
Q

How do you determine what n-roots of unity to use in FFT when using it for polynomial multiplication: A * B?

A

a = degree of polynomial A
b = degree of polynomial B
n = 2^{ ceil(log_2(a+b)) }
This the power of 2 closest to a + b.

20
Q

What nth root of unity should you use in FFT for a polynomial of power n

A

The power of 2 closest to the (n+1)th root of unity.

21
Q

What is the geometric series 1 + w_n + w_n^2 + … + w_n^{n-1} equal to?

A

1 * (1-w_n^n) / (1-w_n)
Note: w_n^n = 1 so this equals 0.

22
Q

What is the recurrence for Floyd-Warshall?

A

min { D(i-1,s,t), D(i-1,s,i)+D(i-1,i,t) }

23
Q

What is the runtime of fast-multiply?

A

O(n^{log_2(3)})

24
Q

Describe Recursion for FastMultiply

A

A = FastMultiply(xl,yl)
B = FastMultiply(xr,yr)
C = FastMultiply(xl+xr,yl+yr)
z = 2^n*A + 2^{n/2}(C-A-B) + B
Return(z)

25
Q

What runtime can you find the median using DP?

A

Linear: O(n)

26
Q

Bellman-Ford Recurrence

A

for i=1->n-1:
for all z in V:
for all yz in E:
D(i,z) = D(i-1,z)
if D(i,z) > D(i-1,z) + w(y,z):
D(i,z) = D(i-1,z) + w(y,z)
return D(n,:) (whole row)

27
Q

Maximum sum recurrence

A

T(i) =ai+ max (0,T(i−1)) for 1<=i<=n