CS302 --- Analysis of Algorithms and Selection of Algorithms

Brad Vander Zanden

Analysis of Algorithms

We analyze algorithms to determine their cost in terms of time and storage. In this course, we will be concerned primarily with time.

Measures of Times


Quantifying Time


Typical Running Times of Algorithms


Common Recurrences

Recurrence Solution
T(N) = T(N/2) + d T(N) = O(log N)
T(N) = T(N/2) + N T(N) = O(N)
T(N) = 2T(N/2) + d T(N) = O(N)
T(N) = 2T(N/2) + N T(N) = O(N log N)
T(N) = T(N-1) + d T(N) = O(N)


Rough Estimates of Running Time


Considerations in Choosing among Competing Algorithms

1. Which is fastest?

Big-O notation and average case analysis only provide rough guides. One often has to implement the algorithms and test them with typical inputs to actually determine which algorithm is faster.

Example: Quicksort is typically faster than the other sorting methods. However, if the input sequences are typically almost sorted (e.g., some card games), insertion sort is faster because it is "almost" O(n)

2. Which is simplest to implement?

Often if two algorithms have almost comparable running times but one is far easier to implement and maintain, the simpler algorithm will be chosen, even if it is the slower of the two.

3. How often will the algorithm be executed?

If the algorithm will only be executed a few times, programmer time may be more important than computer time.

4. What is the size of the input?

If the input size is small, then an algorithm with a higher time complexity but smaller constants may be preferable.

Example: As a rough rule of thumb, selection sort is faster than quicksort for inputs of size less than 50

Strategy for Solving a Problem

Choose the simplest algorithm first and implement it. Consider a faster, but potentially more complicated, algorithm only if speed becomes a problem. Even if it is clear from the outset that a faster, more complicated algorithm is required, consider implementing a simpler algorithm as a way of checking the correctness of the more complicated algorithm.