Chapter 24 - Developing Efficient Algorithms Flashcards
What is the problem with measuring algorithm efficiency by testing the running time of the programs?
- Many tasks run concurrently on a computer. The execution time of a particular program depends on the system load.
- The execution time depends on specific input. Consider, for example, linear search and binary search. If an element to be searched happens to be the first in the list, linear search will find the element quicker than binary search.
What is “the big O notation”?
Computer scientists use the Big O notation to represent the “order of magnitude”. Using this notation, the complexity of the linear search algorithm is O(n), pronounced as “order of n”.
What do we measure algorithm efficiency by?
We measure their growth rates in relation to input growth/size.
What is best-case input and worst-case input?
An input that results in the shortest execution time possible is called the best-case input.
An input that results in the longest execution time possible is called the worst-case input.
What are best-case and worst-case analysis good for?
They are not representative, but worst-case analysis is very useful, because you can be assured that the algorithm will never be slower than the worst case.
What is average-case analysis?
Average-case analysis attempts to determine the average amount of time among all possible inputs of the same size. Average-case analysis is ideal, but difficult to perform, because for many problems it is hard to determine the relative probabilities and distributions of various input instances. Worst-case analysis is easier to perform, so the analysis is generally conducted for the worst case.
The linear search algorithm requires n comparisons in the worst case and n/2 comparisons in the average case if you are nearly always looking for something known to be in the list.
Using the Big O notation, what are the algorithmic complexity of n and n/2 respectively?
Both cases require O(n) time. The multiplicative constant (1/2) can be omitted. Algorithm analysis is focused on growth rate. The multiplicative constants have no impact on growth rates. The growth rate for n/2 or 100n is the same as for n.
When do we need algorithm analysis?
When there’s a possibility for large input sizes we should perform algorithm analysis. If the input size is small, there is no significance in estimating an algorithm’s efficiency.
What is the complexity of an algorithm that takes n - 1 time to execute?
The Big O notation allows you to ignore the nondominating part (e.g., -1 in the expression n - 1) and highlight the important part (e.g., n in the expression n - 1). Therefore, the complexity of this algorithm is O(n).
What is meant by “complexity of contant time”?
The Big O notation estimates the execution time of an algorithm in relation to the input size. If the time is not related to the input size, the algorithm is said to take constant time with the notation O(1). For example, a method that retrieves an element at a given index in an array takes constant time, because the time does not grow as the size of the array increases.
What is the algorithmic complexity of.
1+2+3+…+(n-2)+(n-1) = (n(n-1))/2
?
O(n^2)
Consider the time complexity for the following loop:
for (i = 1, i
It is a constant time, c, for executing
k = k + 5;
Since the loop is executed n times, the time complexity for the loop is
T(n) = (a constant c) * n = O(n)
What is a quadratic algorithm?
An algorithm with the O(n^2) time complexity is called a quadratic algorithm. The quadratic algorithm grows quickly as the problem size increases. If you double the input size, the time for the algorithm is quadrupled. Algorithms with a nested loop are often quadratic.
What is the time complexity of the following selection statement? if ( list.contains(e)) { - System.out.println(e); } else - for (Object t: list) { - System.out.println(t); - }
Suppose the list contains n elements. The execution time for list.contains(e) is O(n). The loop in the else clause takes O(n) time. Hence, the time complexity for the entire statement is
T(n) = if test time + worst-case time(if clause, else clause)
= O(n) + O(n) = O(n)
Consider the computation of a^n for an integer n. A simple algorithm would multiply a n times, as follows:
result = 1;
for (int i = 1; i
The algorithm takes O(n) time. Without loss of generality, assume n = 2^k. You can improve the algorithm using the following scheme:
result = a;
for (int i = 1; i