Algorithms Flashcards
What is runtime
A function f: N -> N, which maps the input size to the number of elementary operations completed
How to compare asymptotic complexity of functions
What is the racetrack principle
How does Fast Peak Finding work
Imagine the array is sloping downwards, then there must be a peak to the left and vice versa; perform binary search to get the peak
Log laws
n = 2^log(n) (base 2)
Derivative of log(n) (base 2)
Sum of arithmatic sequence formula
Sum of geometric sequence formula
What is big O notation
Runtime complexity hierarchy
How to prove/disprove that a function is in O(n)
Composing two functions effect on O notation
Loop / multiplying functions effect on O notation
Loop / multiplying functions effect on O notation
What are the 5 steps of merge sort
1) If n = 1 return A
2) Recursively sort left sub-array
3) Recursively sort right sub-array
4) Merge together the two arrays
5) Return the sorted array
How does the tree diagram of merge sort look like
What are the steps of a divide and conquer algorithm
1) Divide into smaller subproblems
2) Conquer sub-problems
3) Merge back up the tree to find a general solution
What does it mean for an algorithm to be in-place
It means that no auxillary arrays are used. At most O(1) elements stored outisde array
What does it mean for an algorithm to be stable
The ordering of elements are not un-neededly swapped. Ex: If A[i] = A[i+1] they will not be swapped. Useful for satilite data
Is insertion sort stable and in-place
Insertion sort is both stable and in-place
Is merge sort stable and in-place
Merge sort is stable but not in-place since it uses auxillary arrays in the merge step
How does the merge step work in mergesort (4 steps)
Require both halves of A are sorted
1) Copy left half into auxillary array B
2) Copy right half into auxillary array C
3) Merge B and C in sorted order
4) Return result
How is a loop represented in sigma notation
What type of binary tree is mergesort
Complete binary tree
What is a full binary tree
Each element has either 0 or 2 children
What is the runtime of mergesort
O(nlogn)
What is the depth of the tree for a divide and conquer algorithm which splits in half at each step
log(n) (base 2)
What is the general runtime for most divide and conquer algorithms
O(nlogn)
How to determine the runtime of a divide and conquer algorithm
Look at how long each node takes to complete, the amount of nodes per level and the number of levels
What is the maximum subarray problem
What are the 3 cases for the maximum subarray problem
If it is included in L then can recursively solve, same for R. However if spans across the middle then will need to find the indices.
How to find the indices for i,j for when the maximum subarray crosses the midpoint
Can be broken into two subproblems, maximising A[i,n/2] and A[n/2,j], thus can just loop through all values to find which gives the highest result. Takes O(n) time.
What are the 5 steps for the maximum sub array problem d&c algorithm
1) If n=1 return A
2) Recursively compute the max subarr for L
3) Recursively compute max subarr for R
4) Compute the max subarr that crosses the midpoint
5) Compare all these arrays and return the greatest one
What is the definition of theta notation
What is the symetry of theta notation
How does theta notation relate to big-o
What is the definition of omega notation
How does omega notation relate to big-o
How does the RAM model work ( what can be done in a single time-step)
How does the RAM model work (memory-wise)
What are the two costs of an algorithm according to the RAM model
What does the RAM model mean about the O() for each operation
Every operation of our algorithm can be implemented in O(1) elementary operations in the RAM model
How are loops expressed as sums
Note: k would be the runtime of the inner loop
What is the formula for best case runtime
What is the formula for worst case runtime
What is the formula for average case runtime
What is the algorithm for linear search
What is the best and worst case runtime for linear search
Worst case: O(n)
Best case: O(1)
What is the average case runtime for linear search on a binary array
Average case O(1)
In O(1)