Algorithms and Data Structures Flashcards
Name three desirable properties of a good algorithm.
Algorithm should be:
- Correct
- Efficient
- Easy to implement
These goals might not be and often are not simultanously achievable.
How is mergesort algorithm implemented?
- Based on divide-and-conquer approach.
- Breaks array into two halves and sorts both halves
- Once subproblem is 2 element array it starts merging back arrays so that they are sorted during merge. After bubbling up the recursion array is sorted.
- Needs an additional temporary array for merging.
How is quicksort algorithm implemented?
- Divide and conquer approach through partitioning.
- Taking an element and partitioning the array to “lower than” and “greater than”.
- Recursively repeating partitioning on subarrays
- When subarrays have 1 element, sorting is done.
On which algorithm is shell sort based?
- It is based on insertion sort algorithm.
How is shell sort algorithm implemented?
- It uses so cold h-sort approach to introduce partial order in the array.
- Value h is first calculated “while (h < N/3) h = 3*h + 1”
- In every loop value h = h/3 - at the end we have h = 1 and that is regular insertion on partially sorted array.
- For wrongly set h can be slow
Running time properties of mergesort algorithm?
- Mergesort is optimal compare-based sorting algorithm.
- Average, worst and best case run in O(NlogN) complexity
- Needs auxilliary array. Additional space O(N)
- For already sorted is O(N) if there is a check for if array is already sorted.
Running time properties of quicksort algorithm?
- Average O(NlogN)
- Worst O(N2) when array is already sorted. Randomization before sorting is done to prevent.
- Can be improved by cutoff to insertion sort for smaller arrays
- For arrays with lot of duplicates can be significantly improved by partitioning to “smaller than”, “equal”, “larger than”.
Ways to improve default quicksort approach?
- Cutoff to insertion sort for 15 element subarrays
- Picking the partition element. Median of three.
- Partitioning to “lower than”, “equal”, “greather than” for arrays with repeateable keys.
What are optimizations for the mergesort algorithm?
- Cutoff to insertion sort
- Check for already sorted arrays.
What are run-time properties of selection sort algorithm?
- Average, best, worst time: O(N2)
- Approx N2/2 compares and N swaps
- Doesn’t need additional memory
- Running time is not sensitive to input
What are running time properties of insertion sort algorithm?
- Average case is O(N2) with N2 swaps for unsorted array
- Worst case is O(N2) with N2 swaps for random array
- Best case is O(N) with 1 swap for already ordered array.
- It doesn’t need additional memory
- Dependent on properties of input
What are running time properties of shell sort algorithm?
- It is in practice O(N4/3), O(N5/4) … but no proof is given
- In practice used due to its simplicity and acceptable speed for moderately large arrays.
Define:
heuristic
Heuristic is a technique designed for solving a problem more quickly when classic methods are too slow, or for finding an approximate solution when classic methods fail to find any exact solution. In a way, it can be considered a shortcut.
In general, every type of problem will have its own set of heuritics.
Give one example of heuristics?
An example of heuristics is using Manhattan distance heuristic function in the problem of finding the shortest path in order to decide on the next step among the list of options.
What is the difference between the algorithmic problem and an instance of an algorithmic problem.
Algorithmic problem is specified by describing the complete set of instances it must work for and produce valid solution to all of them.
Sorting is an algorithmic problem. Sorting of an array of integers is an instance of sorting problem.
This is important since recognizing the general problem in an instance is the first step to solving it.
Fundamental difference between algorithm and heuristic?
There is a fundamental difference between algorithms, which always produce correct result, and heuristics, which may usually do a good job but without providing any guarantee.
Give one example where
heuristic yields very unoptimal solution.
If points we are trying connect are lying on a flat line and we start in the middle then it will, instead of goinglinearly, it will jump towards the outside which is very unoptima.
In general, if distances between nearest neighbours become bigger and bigger towards the end of execution, it is most probably unoptimal
Which are three forms of algorithmic notation?
Three forms of algorithmic notation (going from informal to formal):
- English
- Pseudo code
- Implementation in a particular programming language.
Common mistake is to use more formal structure to express not so formal level of communication. E.g. english explanation presented through pseudo code structure.
Which are two parts of algorithmic problem specification?
Two parts of algorithmic problem specification are:
- Set of allowed input instances.
- Required properties of the algorithm output.
What can be done with set of allowable instances in the specification of an algorithmic problem in order to make it easier to find a correct solution?
Narrowing down problems to a more simple allowable instances is a standard way to simplify reasoning about algorithms.
Example would be reasoning about a tree structure instead of full graph or single dimensional problem instead of 2-dimensional one.
What is the best way to prove one algorithm incorrectness?
The best way to prove incorrectness of an algorithm is by finding a counter-example for which algorithm is not correct.
Counter example is the case of input sequence for which correct solution exists but algorithm doesn’t find it.
Which are two important properties of counter-examples?
Two properties of a good counter-example are:
- Verifiability
- Simplicity
Which are good approaches in finding counter-examples?
- Think small.
- Think exhaustively
- Hunt for the weakness
- Go for a tie - when algorithm has to decide on two options.
- Seek extremes
Inductive proof steps are?
- Prove the idea for the basic case n = 1, n = 2
- Assume the idea is valid until the “n - 1” case
- Prove that it is still valid for the “n” case.

