Exam 1 Flashcards
Master Theorem (Book)
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)
Binary Search
requires a list be sorted.
BinarySearch(sortedArray):
mid = (high - low) / 2
if(mid = goal):
return mid
if(midgoal):
BinarySearch(low,mid-1)
return -1
1 + 2 + 3 + 4 + … + (n-1) = ?
( (n-1) * n ) / 2
Longest Increasing Subsequence (LIS) Recurrence
T(i) =1
If a[j] < a[i]:
T(i) = max{ T(i), T(j) + 1) }for 1<=i<=n and 1<=j
Longest Common Subsequence (LCS) Recurrence
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
Edit Distance Recurrence
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)}
Knapsack (no repeats) Recurrence
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)
Knapsack (with repeats) Recurrence
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)
Fast Matrix Multiplication Runtime
O(n^3)
Difference between Bellman-ford and Floyd-Warshall algorithms.
- 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)
Runtime of Dijkstra’s algorithm
O((n+m) log(n)) where n is vertices and m is edges
Does Dikjstra’s algorithm work if negative weights are allowed
No. It requires all edge weights are positive. It’s not guaranteed to produce the right output when the edge weights can be positive.
Fibonacci’s Numbers Recurrence
T(0) = 0 T(1) = 1 T(n) = T(n-1) + T(n-2)
Dictionary Lookup (Sequence of words?) Recurrence
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
Longest Common String Recurrence
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