general Flashcards

1
Q

Does the Random Contraction Algorithm always gives you the minium cut?

A

No this is a random algorithm. It will return the minimum cut solutions only on average!

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

What is the complexity of addition of 2 array of n-digits N+N?

A

It is O(n) or O(logN) where N is the actual value of the number. Indeed the number of digits n is n= is log10(N)

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

What is the fastest you can be in sorting an array (theoretical bound)?

A

Nlogn you can never ever get linear time in sorting! So you should love merge sort quick sort etc!

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

What is the complexity of a simple sorting algorithm? name a few of them

A

They are all n^2 like insertion bubble sort etc. Merge sort a recursive algorithm is actually a n*logn +n algorithm

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

High-level description of a partition with linear time and no memory loss (in place)

A

See image below, go through the swapping in your mind

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

For an array n what is the maximum number of inversions?

A

eVERY COUPLE you peek would bein the wrong order with i>j so it is the number of paris (n 2) n(n-1)/2

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

What is the maximum and the minimum number of edges in a graph connected undirected with no parallel edges?

A

Minumum is n-1 maximum is n(n-1)/2

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

What is the worst case time for a random selection algorithm if the pivot is chosen poorly?

A

It is O(n^2) like for quicksort. The size of the array you re recursing on reduce as n, n-1, n-2 .. and every time you do a partition which is an O(n) algorithm

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

How does merge sort work?

A

Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following C implementation for details.

MergeSort(arr[], l, r)

If r > l

  1. Find the middle point to divide the array into two halves: middle m = (l+r)/2
  2. Call mergeSort for first half: Call mergeSort(arr, l, m)
  3. Call mergeSort for second half: Call mergeSort(arr, m+1, r)
  4. Merge the two halves sorted in step 2 and 3: Call merge(arr, l, m, r)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Karatsuba multiplication: simple way to see the formual and a bit of details

A

it is a n^log_2(3) n^1.6 algorithm compare to the simple quadratic one. Just write x= 10^n/2 a +b y= 10^n/2 c +d and go from there

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

What is the best case scenario (in the choice of the pivot) in random selection? what is its time?

A

As usual, you want to reduce the size of the problem. So best case scenario is in balance, split the array in equal sizes sooo… the median

if you do that then each iteration

T(n)<t></t>

<p>note now is T(n/2) not 2*T(n/2) like in quicksort. We only need to look at one sort of the subarray</p>

<p>from master method --&gt;<strong> linear time O(n)</strong></p>

</t>

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

Counting inversions: given array count how many object i,j with i>j not adjiacent!! Speed, use etc

A

with divide and conquer you can get to n*log(n) (as usual we want to have a almost linear algorithm) This is good for similarities say you rank movies and want to check how much 2 lists are close (zero inversion mean very close.)

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

High level description of quick sort?

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

Partition (around a pivot): what is the running time? what about memory

A

They are linear O(n) and need no memory needed just swap!

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

What is the complexity of merge sort?

A

n*logn +n

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

Quick sort worst and best case behaviour?

A

n^2 for the worst n logn for best case scenario but on average very close to best behaviours (decomposition principle)

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

How many different minimum cuts are there in a tree with n nodes (ie. n−1 edges)

A

Any edge is a minimum cut if you think about it cause every vertex is connected to the graph by one edge

so there are n - 1 minimum cuts.

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

What is the key of the so called decomposition principle for a randomized algorithm?

A
  1. Identify what is the random variable Y you are really interested in
  2. Express that as a sum of simpler (indicator i.e variables that takes the )
  3. Apply linearity of expectation E[sum(x)] = sum(E(x)) that works even for dependent variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the pivot element in quick sort?

A

It is an element of the array you pick such that all the elements to the left are smaller than the pivot while all the element on the right are bigger (note they are not ordered!)

Also, note the pivot ends up being in the right place (the finally place it will be in the array after sorting)

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

What is the best way to analyze a recursive algorithm?

A

Use a recursive tree, find the number of levels (depth) it is usually log(n) at each level j you have 2^j cases that operate on array of size n/2^j then you only have to know the complexity/time of level j (hopefully it is independent of the level)

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

What is O() of nested loops if for i to n; for j i+1 ..n ?

A

The same still O(n^2) you just save a factor of 2 or so

22
Q

Can integers overflow in python?

A

Short answers: No if the operations are done in pure python, because python integers have arbitrary precision Yes if the operations are done in the pydata stack (numpy/pandas), because they use C-style fixed-precision integers

23
Q

Results of master method!!

A

if the recursive time at one step T(n) is < aT(n/b) + O(n^d) i.e the recursive plus the merging step time. Now we have three cases

  • 1) a=b^d time is n^d log n
  • 3) ad we have O(n d)
  • 2) a>b^d here how many recursion is crucial time is n^(log_b(a))
24
Q

Minimum cut graph random contraction algorithm!

A

First, Random sampling very efficient for sorting and graph problem.

Pick a random edge uniformly. Fuse the 2 vertexes into one. You can create parallel loops (keep) or self loop(kill them).

Once you get the last 2 vertices that is the cut: the one represented by the vertices fused with 2 vertices.

25
Q

What is the key to count inversion fast?

A

To also merge sort we want to sort and count!

26
Q

How many times would 2 objects of an array be compared in the quicksort algorithm?

A

0 or 1 no more!

because when comparing one is the pivot. Then that number the pivot is removed from the future recursive call

27
Q
A
28
Q

What is O() of a loop?

A

O(size of the loop) it is a linear algorithm, like searching if a number is in an array

29
Q

Outline the algorithm for randomized selection!

A

It is a reduction (reduce a problem to one you already know how to solve) of quick sort.

say you look for the j-th statistics

you partition with a pivot that ends up in ith position

if i=j you stop

if i>j you look at the left array for a j statistics

i<j>
</j>

30
Q

What are some benefit of insertion sort?

A

Efficient for (quite) small data sets, much like other quadratic sorting algorithms

More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort

efficient for data sets that are already substantially sorted: the time complexity is O(kn) when each element in the input is no more than k places away from its sorted position

Stable; i.e., does not change the relative order of elements with equal keys

In-place; i.e., only requires a constant amount O(1) of additional memory space

Online; i.e., can sort a list as it receives it

31
Q

What is the meaning of being O(f(x))

A

for some large value x, you are bounded below by c*f(n)

32
Q

What is a common way to speed up exponential time algorithm 2^N for example?

A

Memorization, you want to save on disk previous results. It will increase space complexity but it will reduce your running time. One example is the Fibonacci series where you save the previous results on disk

33
Q

Pretty good way to check if a number is a power of two or not.

A

def is_power2(num): ‘states if a number is a power of two’ return num != 0 and ((num & (num - 1)) == 0) https://stackoverflow.com/questions/8556206/what-does-mean-in-python

34
Q

What is O() of nested loops?

A

O(size of the loop*size of the loop2) it is quadratic algorithm, like finding a common number in 2 arrays

35
Q

What makes a pivot in quicksort a good pivot?

A

You want it to split the array almost in half the most balanced you can!

I.e the median of the array!

36
Q

Describe selection sort

A

In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(n2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity and has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

arr[] = 64 25 12 22 11

// Find the minimum element in arr[0…4]

// and place it at beginning 11 25 12 22 64

// Find the minimum element in arr[1…4]

// and place it at beginning of arr[1…4] 11 12 25 22 64

// Find the minimum element in arr[2…4]

// and place it at beginning of arr[2…4] 11 12 22 25 64 //

Find the minimum element in arr[3…4]

// and place it at beginning of arr[3…4] 11 12 22 25 64

37
Q

Matrix multiplication! How fast is a naive implementation?

A

It is n^3 you have a n^2 number in the results and those come from a row*column multiplication so n operations!. Note that you can never do better than the input size, we have n^2 elements so if they are independent (worst case scenario) that is the best we can do

38
Q

What are the 2 structures you can use to store graph? describe them

A

1) an adjacency matrix. With 1 or -,1 or x in i,j for simple direct or weighted graph when vertices i and j are connected. It is an O(n^2) structure which is optimal only for dense graph where you have n^2 link
2) adjacency list: there are various representations but Each list describes the set of neighbors of a vertex in the graph.

39
Q

What is the minimum cut problem?

A

You want to compute the cut, partition graph in A, B so that we have the minimum number of crossing edges

40
Q

Do you have a faster than cubic matrix multiplication algorithm?

A

yes, the Strassen algorithm. Remember where that comes from divding the matrix in blocks and magically using only 7 multiplication instead of 8

41
Q

How many cuts (partition) does a graph of N vertices have?

A

2^n it is a partition into 2 so every element can either stay in A or B, so 2 possibilities than raised for the N elements. 2^n -2 (to remove the emptyy set)

42
Q

What is the minimum connectivity of a vertex in a graph which minimum cut solution has k crossing edges?

A

K otherwise I can just cut that only vertex and I will have a partition with a smaller number of crossing edges!

43
Q

What is the maximum number of minimum cuts in a graph?

A

(n

2)

n chose 2. This is bigger than a tree (n-1) and smaller than the total number of cuts you have which is 2^n

44
Q

How would you choose the pivot in the quicksort algorithm?

A

You would use random algorithm!

This makes quicksort a randomized algorithm a very successful type of algorithm.

45
Q

How does insertion sort work? What is time and space complexity

A

Swapping all the element till sorted. For all element it as to go trough the array so O(n^2) but can be doone in place so space is O(1)

Function to do insertion sort

def insertionSort(arr):

Traverse through 1 to len(arr)

for i in range(1, len(arr)):

key = arr[i]

Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position

j = i-1

while j >=0 and key < arr[j] :

arr[j+1] = arr[j]

j -= 1

arr[j+1] = key

Driver code to test above

arr = [12, 11, 13, 5, 6]

insertionSort(arr)

print (“Sorted array is:”)

for i in range(len(arr)):

print (“%d” %arr[i])

46
Q

Time of quicksort on an already sorted array if we pick the pivot as always the first element!?

A

It is quadratic!! O(n^2) this is a very bad choice of pivot!

47
Q

Do you get any gain in attach recursively the integer multiplication?

A

Only if you use the Gauss tricks otherwise if you compute all the 4 products you do not gain anything.

48
Q

What is the master method or master theorem?

A

A quick and efficent way to get the running time in recursive algorithms. It has some assumptions: the most important is that you are breaking the size equally at every recursion

49
Q

Guiding principle to analyze algorithm?

A

1) worst case analysis (think about the worst case given a lenght n) other options are average case and benchmark but you have to know what is the distributions of your input 2) do not care about small constants etc only O() we do this cause we care about big problems! Also they would depend a lot on the implementation!!!!

50
Q

Find an unbiased estimator for the mean ?

How can you get the variance

A

It does not exist.

The intuition is that the median can stay fixed while we freely shift probability density around on both sides of it, so that any estimator whose average value is the median for one distribution will have a different average for the altered distribution, making it biased.

If you can sample on it you can do bootstrapping. Divinfg the dataset in batches you can get the variance as well

51
Q

Is the sample of the median unbiased?

A

Only if the distribution is simmetric.

The sample median is an unbiased estimator of the population median when the population is normal. However, for a general population it is not true that the sample median is an unbiased estimator of the population median. The sample mean is a biased estimator of the population median when the population is not symmetric.