Course 1 week 1 Flashcards

1
Q

3-way-Merge Sort : Suppose that instead of dividing in half at each step of Merge Sort, you divide into thirds, sort each third, and finally combine all of them using a three-way merge subroutine. What is the overall asymptotic running time of this algorithm? (Hint: Note that the merge step can still be implemented in O(n)O(n) time.)

A

nlog(n)

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

You are given functions ff and gg such that f(n)=O(g(n))f(n)=O(g(n)). Is f(n)log_2(f(n)^c) = O(g(n)log_2(g(n)))f(n)∗log
2
​ (f(n)
c
)=O(g(n)∗log
2
​ (g(n))) ? (Here cc is some positive constant.) You should assume that ff and gg are nondecreasing and always bigger than 1.

A

That’s correct! Roughly, because the constant c in the exponent is inside a logarithm, it becomes part of the leading constant and gets suppressed by the big-Oh notation.

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

Assume again two (positive) nondecreasing functions ff and gg such that f(n)=O(g(n))f(n)=O(g(n)). Is 2^{f(n)}=O(2^{g(n)})2
f(n)
=O(2
g(n)
) ? (Multiple answers may be correct, you should check all of those that apply.)

A

Yes if f(n) \le g(n)f(n)≤g(n) for all sufficiently large nn

Sometimes yes, sometimes no (depending on ff and gg)

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

What does the statement f(n) = O(g(n)) mean? What are some examples that hold true only after a higher constant.

A

f(n) is bounded by g(n)
There exists a set of constants c and n0, after which f(n) <= c*g(n) for all n > n0.

example : f(n) = 2n+100, g(n) = n**2
**Need more examples* what is n0 and c here?

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

k-way-Merge Sort. Suppose you are given kk sorted arrays, each with nn elements, and you want to combine them into a single array of knkn elements. Consider the following approach. Using the merge subroutine taught in lecture, you merge the first 2 arrays, then merge the 3^{rd}3
rd
given array with this merged version of the first two arrays, then merge the 4^{th}4
th
given array with the merged version of the first three arrays, and so on until you merge in the final (k^{th}k
th
) input array. What is the running time taken by this successive merging algorithm, as a function of kk and nn? (Optional: can you think of a faster way to do the k-way merge procedure ?)

A

O(nk2)

For the upper bound, the merged list size is always O(kn)O(kn), merging is linear in the size of the larger array, and there are kk iterations. For the lower bound, each of the last k/2k/2 merges takes \Omega(kn)Ω(kn) time.

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

Arrange the following functions in increasing order of growth rate (with g(n)g(n) following f(n)f(n) in your list if and only if f(n)=O(g(n))f(n)=O(g(n))).

a)2^{\log(n)}2
log(n)

b)2^{2^{\log(n)}}2
2
log(n)

c)n^{5/2}n
5/2

d)2^{n^2}2
n
2

e)n^2\log(n)n
2
log(n)

A

aecbd

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

Exponents and Logarithms - TBD

A

http://www.wou.edu/mathcenter/files/2015/09/Exponents-and-Logarithms.pdf

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

karathsuba-code-TBD organize

A

http://www.keithschwarz.com/interesting/code/karatsuba/Karatsuba.python.html

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

columbia - asymptotic notation - TBD

A

http://www.columbia.edu/~cs2035/courses/ieor6614.S11/algal.pdf

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

karatsuba - implementation notes (good code)

A

http://www.keithschwarz.com/interesting/code/karatsuba/Karatsuba.python.html

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

Asymptotic Notation in Seven Words

A

suppress constant factors | {z} too system-dependent and lower-order terms | {z} irrelevant for large inputs

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

Add typical algorithms and their python implementation run time

A

TBD

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

Input: array A of n integers, and an integer t.
Output: Whether or not A contains t.
for i := 1 to n do if A[i] = t then return TRUE return FALSE

What is the asymptotic run time

A

O(n)

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

Input: arrays A and B of n integers each, and an integer t. Output: Whether or not A or B contains t.

What is the asymptotic run time

A

O(n) -> as we can check each array one at a time

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

Checking for a Common Element
Input: arrays A and B of n integers each.
Output: Whether or not there is an integer t contained in both A and B.

for i := 1 to n do for j := 1 to n do if A[i] = B[j] then return TRUE return FALSE

A

O(n**2)

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

Checking for Duplicates
Input: array A of n integers.
Output: Whether or not A contains an integer more than once.

for i := 1 to n do
for j := i + 1 to n do if A[i] = A[j] then
return TRUE
return FALSE

A

O(n**2)

17
Q

Big-O Notation

A

Big-O Notation (Mathematical Version) T(n) = O(f(n)) if and only if there exist positive constants c and n0 such that T(n)  c · f(n) (2.1) for all n $n0.

18
Q

Big Omega notation

A

Big-Omega Notation (Mathematical Version) T(n) = ⌦(f(n)) if and only if there exist positive constants c and n0 such that T(n) $c · f(n) for all n $n0.

19
Q

pictures of big-omega, big-theta from Tim Roughgarden’s book or course on algorithms

A

Big-Omega Notation (Mathematical Version) T(n) = ⌦(f(n)) if and only if there exist positive constants c and n0 such that T(n) $c · f(n) for all n $n0.

20
Q

Big-Theta Notation (Mathematical Version) T(n) = big-theta(f(n)) if and only if ….

A

there exist positive constants c1, c2, and n0 such that c1 · f(n)  T(n)  c2 · f(n) for all n $n0.

21
Q

Problem 2.1 (S) Let f and g be non-decreasing real-valued functions defined on the positive integers, with f(n) and g(n) at least 1 for all n $1. Assume that f(n) = O(g(n)), and let c be a positive constant. Is f(n) · log2(f(n)c) = O(g(n) · log2(g(n)))?

A

?

22
Q

Problem 2.2 Assume again two positive non-decreasing functions f and g such that f(n) = O(g(n)). Is 2f(n) = O(2g(n))? (Multiple answers may be correct; choose all that apply.)

A

?

23
Q

Problem 2.3 Arrange the following functions in order of increasing growth rate, with g(n) following f(n) in your list if and only if f(n) = O(g(n)).

A

?

24
Q

Problem 2.5 (S) Arrange the following functions in order of increasing growth rate, with g(n) following f(n) in your list if and only if f(n) = O(g(n)).

A

?

25
Q

What are the steps in a Divide and Conquer Paragidm:

A

The Divide-and-Conquer Paradigm 1. Divide the input into smaller subproblems. 2. Conquer the subproblems recursively. 3. Combine the solutions for the subproblems into a solution for the original problem.

26
Q

What is the usually complex step in Divide and Conquer algorithms

A

In MergeSort and many other algorithms, it is the last step that requires the most ingenuity. There are also divide-and-conquer algorithms in which the cleverness is in the first step (see QuickSort in Chapter 5) or in the specification of the recursive calls (see Section 3.2).

27
Q

Counting inversions

Problem: Counting Inversions Input: An array A of distinct integers.
Output: The number of inversions of A—the number of pairs (i, j) of array indices with i A[j].

What is the run time using the brute force method

A

O(n**2)

28
Q

What is the largest-possible number of inversions a 6-element array can have?.

A

15

29
Q

Another way to think about collaborative filtering is…

A

Index one of the users and count the number of inversions with the other user.