Algorithms Flashcards

1
Q

Define Big O notation

A
f(x) = O(g(x)) means:
there exists:
* some positive constant M
* some minimum x-value, x_o
Such that for all x > x_o
* f(x) <= M×g(x)

Basically: f(x) = O(g(x)) means
“f(x) is at least as fast as g(x)”

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

Define the Limit Theorem

A

Let f(x) & g(x) be real-valued functions

Let L = lim (f(x)/g(x)) lim of x->infinity

  1. If L = 0, f(x)=o(g(x)) (and thus Big O too)
  2. If L = infinity, g(x)=o(f(x)) (and thus Big O too)
  3. If 0 < L < infinity, f(x)=O(g(x)) and g(x)=O(f(x))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define L’Hopital’s rule

A
Condition:
* if lim f(x) and lim g(x) = 0 OR
* if lim f(x) and lim g(x) = infinity
Then:
lim (f(x)/g(x)) = lim (f'(x)/g'(x))   the derivative of each
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define Little O notation

A

f(x) = o(g(x)) means
* for EVERY positive ε
There exists a constant x_ε
Such that: f(x) <= ε*g(x) for all x>= x_ε

Basically, f(x) = o(g(x)) means:
g(x) grows at a much faster rate than f(x)

If f(x) = o(g(x)), then f(x) = O(g(x)) too

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

What is Big Omega (Ω)

A

Opposite of Big O

f(x) = Ω(g(x)) if g(x) = O(f(x))

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

What is Big Theta (Θ)

A

If two functions are Big O of eachother

f(x) = Θ(g(x)) if f(x) = O(g(x)) and g(x) = O(f(x))

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

What is a recurrence Relation

A

Is a mathematical equation
Is Not a code/algorithm.

Recursively defines a function’s values in terms of earlier values

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

How do you prove the runtime of a function?

A
  1. Write out its recurrence relation
  2. Turn it into closed form (by expanding the recurrences & finding a pattern)
  3. Do a formal proof to show the recurrence relation is equal to the closed form
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Master Method

A

T(n) = aT(n/b) + O(n^d)
a, b, d must be constants

  • if log_b(a) < d, then T(n) = O(n^d)
  • if log_b(a) = d, then T(n) = O(n^d * log(n))
  • if log_b(a) > d, then T(n) = O(n^(log_b(a)))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Binary Search FIX

A

O(log(n))

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

BubbleSort runtime?

A

Worst-case runtime: O(n^2)
* if array is in reverse order
Best-case runtime: O(n)
* if array is already sorted

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

How does BubbleSort work?

A
  1. Iterate through the list
  2. At each step, compare element i to i+1
  3. If they’re out of order, swap them
  4. Continue down the list & start over until no swaps are needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

InsertionSort runtime?

A

Worst-case: O(n^2)

Best-case: if already sorted
O(n) if backwards search,
O(n
log(n)) if binary search
* if array is already sorted

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

How does InsertionSort work?

A
  1. Iterate through array
  2. At each element, figure out where to place it in the elements before it (already sorted)
  3. Shift appropriate elements
  4. Move to next element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

SelectionSort runtime?

A

O(n^2)

For ALL situations

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

How does SelectionSort work?

A
  1. Find the smallest element in the list
  2. Swap it with the first element in the list
  3. Move up 1, repeat with second smallest, swap with second element
17
Q

MergeSort runtime?

A

O(n*log(n))

For ALL situations

Merge runtime = O(n)

18
Q

How does MergeSort work?

A
  1. Split array in half
  2. Split those halves recursively
  3. Merge the two halves with Merge
19
Q

QuickSort runtime?

A

Worst-case: O(n^2)

Average: O(n*log(n)) in expectation
* (If you split the array in half each time)

20
Q

How does QuickSort work?

A
  1. Select a pivot element
  2. Iterate through the array, smaller values go to left of pivot, bigger values go to right, duplicates of pivot go to a middle array
  3. Recurse for left & right sub-arrays
21
Q

Principle of Divide & Conquer

A
  1. Break problem into smaller subproblems
  2. Recursively solve the subproblems
  3. Combine the subproblem solutions to solve the OG problem
22
Q

Example of Divide and Conquer algorithms:

A
  • binary search
  • QuickSort
  • MergeSort