Analysis of Algorithms I
Analysis of Algorithms I
Key topics:
* Introduction
* Generalizing Running Time
* Doing a Timing Analysis
* BigOh Notation
* BigOh Operations
* Analyzing Some Simple Programs no subprogram calls
* WorstCase and Average Case Analysis
* Analyzing Programs with NonRecursive Subprogram Calls
* Classes of Problems
Why Analyze Algorithms?
There may be several different ways to solve a particular problem. For example, there are
several methods for sorting numbers. How can you decide which method is the best in a
certain situation? How would you define "best" – is it the fastest method or the one that
takes up the least amount of memory space?
As mentioned earlier, an algorithm can be analyzed in terms of time efficiency or space
utilization. We will consider only the former right now. The running time of an
algorithm is influenced by several factors:
The first three items in the list are problematic. We don’t want to use an exact
measurement of running time: To say that a particular algorithm written in Java and
running on a Pentium IV takes some number of milliseconds to run tells us nothing about
the general time efficiency of the algorithm, because the measurement is specific to a
given environment. The measurement will be of no use to someone in a different
environment. We need a general metric for the time efficiency of an algorithm; one that
is independent of processor or language speeds, or compiler efficiency.
Finally, the last item in the list requires us to consider another aspect of the input, which
again is not part of the actual algorithm. To account for this, we express timing analyses
in terms of "worst case", "average case" or "best case" based on the organization of the
data, or the probability of finding an element quickly. For our purposes in the following
sections, we will assume a "worst case" organization (i.e., we will not worry about the
organization of the input for now).
Generalizing Running Time
The problem of generalizing a timing analysis is handled by not dealing with exact
numbers but instead with order of magnitude or rate of growth. In other words:
How does the execution time of an algorithm grow as the input size grows?
– 3 –
Do they grow together? Does one grow more quickly than the other – how much more?
The ideal situation is one where the running time grows very slowly as you add more
input. So, rather than deal with exact values, we keep it general by comparing the growth
of the running time as the input grows, to the growth of known functions. The following
functions are the ones typically used:
input
size
n (1) log n n n log n n2 n3 2n
5 1 3 5 15 25 125 32
10 1 4 10 33 100 103 103
100 1 7 100 664 104 106 1030
1000 1 10 1000 104 106 109 10300
10000 1 13 10000 105 108 1012 103000
Statements 1, 2, and 3 are each executed once. Statements 5, 6, and 7 are each executed
n times. Statement 4 (which controls the loop) is executed n + 1 times (one additional
check is required – why?), and statement 8 is executed once. This is summarized below:
3 1
4 n+1
5 n
6 n
7 n
8 1
Thus, the computing time for this algorithm in terms of input size n is:
T(n) = 4n + 5. We can see intuitively, that as n grows, the value of
this expression grows linearly. We say T(n) has an "order of
magnitude (or rate of growth) of n". We usually denote this using big-
Oh notation: T(n) = O(n), and we say that the algorithm has a
complexity of O(n). In some cases, we might also say the algorithms
has a "time complexity" of O(n) to distinguish the growth rate for the
running time of the algorithm from the amount of memory, or space,
that the algorithm would use during its execution. Of course, intuition
is not enough for us skeptical computer science types – we must be
able to show mathematically which of the standard functions given in
the table above indicates the correct rate of growth. We more
formally present the meaning of big-Oh notation below.
Big-Oh Notation
Definition 1: Let f(n) and g(n) be two functions. We write:
f(n) = O(g(n)) or f = O(g)
(read "f of n is big oh of g of n" or "f is big oh of g") if there is a
positive integer C such that f(n) <= C * g(n) for all positive integers n.
The basic idea of bigOh notation is this: Suppose f and g are both realvalued functions
of a real variable x. If, for large values of x, the graph of f lies closer to the horizontal
axis than the graph of some multiple of g, then f is of order g, i.e., f(x) = O(g(x)). So, g(x)
represents an upper bound on f(x).
Example 1
Suppose f(n) = 5n and g(n) = n. To show that f = O(g), we have to show the existence of a
constant C as given in Definition 1. Clearly 5 is such a constant so f(n) = 5 * g(n). We
could choose a larger C such as 6, because the definition states that f(n) must be less than
or equal to C * g(n), but we usually try and find the smallest one. Therefore, a constant C
exists (we only need one) and f = O(g).
– 5 –
Example 2
In the previous timing analysis, we ended up with T(n) = 4n + 5, and we concluded
intuitively that T(n) = O(n) because the running time grows linearly as n grows. Now,
however, we can prove it mathematically:
Example 3
Say f(n) = n2: We will prove that f(n) ≠ O(n). To do this, we must
show that there cannot exist a constant C that satisfies the big-Oh
definition. We will prove this by contradiction.
Example 4
Suppose f(n) = n2 + 3n - 1. We want to show that f(n) = O(n2).
f(n) = n2 + 3n - 1
< n2 + 3n (subtraction makes things smaller so
drop it)
<= n2 + 3n2 (since n <= n2 for all integers n)
= 4n2
– 6 –
Example 5
Show: f(n) = 2n7 - 6n5 + 10n2 – 5 = O(n7)
nj <= nd if j <= d
we can change the exponents of all the terms to the highest degree (the original function
must be less than this too). Finally, we add these terms together to get the largest constant
C we need to find a function that is an upper bound on the original one.
example, any algorithm with m inputs and n outputs that uses all the inputs to generate
the output would require at least Ω(m + n) work. In a rough sense, bigOmega notation
tells us that an algorithm requires at least this much time to run, hence it is a lower bound
on the running time or alternatively can be thought of as the best case running time for
the algorithm.
One last variation on this theme is bigTheta (Θ) notation. BigΘ bounds a function from
both above and below, so two constants must be provided rather than one if we are doing
formal proofs of the bounds of a given function.
Many "Divide and Conquer" algorithms solve a problem by dividing it into 2 smaller
problems, then into 2 even smaller problems. You keep dividing until you get to the point
where solving the problem is trivial. This constant division by 2 suggests a logarithmic
running time.
Thus, since log(1) = 0, there is no constant C such that 1 <= C * log(n) for all n. Note,
however, that for n >= 2, it is the case that 1 <= log(n) and so the constant C = 1 works for
sufficiently large n (larger than 1). This suggests that we need a stronger definition of
bigOh than given previously.
Definition 2: Let f(n) and g(n) be two functions. We write:
f(n) = O(g(n)) or f = O(g)
if there are positive integers C and N such that f(n) <= C * g(n) for all
integers n >= N.
Using this more general definition for bigOh, we can now say that if we have f(n) = 1,
then f(n) = O(log(n)) since C = 1 and N = 2 will work.
With this definition, we can clearly see the difference between the three types of notation:
– 8 –
In all three graphs above, n0 is the minimal possible value to get valid bounds, but any
greater value will work.
Example 6
Show: f(n) = 3n3 + 3n - 1 = Θ (n3)
First, we show (i), using the same techniques we've already seen for
big-Oh. We consider N = 1, and thus we only consider n >= 1 to show
the big-Oh result.
f(n) = 3n3 + 3n - 1
– 9 –
< 3n3 + 3n + 1
<= 3n3 + 3n3 + 1n3
= 7n3
thus, with C = 7 and N = 1, we have shown that f(n) = O(n3).
Next, we show (ii). Here we must provide a lower bound for f(n).
Here, we choose a value for N, such that the highest order term in f(n)
will always dominate (be greater than) the lower order terms. We
choose N = 2, since for n >=2, we have n 3 >= 8. This will allow n3 to
be larger than the remainder of the polynomial (3n - 1) for all n >= 2.
So, by subtracting an extra n3 term, we will form a polynomial that will
always be less than f(n) for n >= 2.
f(n) = 3n3 + 3n - 1
> 3n3 - n3 since n3 > 3n - 1 for any n >= 2
= 2n3
BigOh Operations
For the much of the remainder of our discussion on computational
complexity, we focus on big-Oh bounds, since they are the primarily
used method for characterizing algorithm running times (especially in
the worst case). In our use of big-Oh, we are generally looking for a
tight bound (analogous to big-Θ, but without having to prove the lower
bound (big-Ω) explicitly). Thus, we usually try to find the smallest
valid function to characterize the running time of an algorithm. So,
while, technically, a function that is O(n) is also O(n2), we will always
characterize such a function as O(n) and would consider it
inappropriate to characterize the function as O(n2). It’s just not as
useful as a tight bound.
Summation Rule
Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)). Further, suppose that f2 grows no faster
than f1, i.e., f2(n) = O(f1(n)). Then, we can conclude that T1(n) + T2(n) = O(f1(n)).
More generally, the summation rule tells us O(f1(n) + f2(n)) = O(max(f1(n), f2(n))).
Proof:
– 10 –
Suppose that C and C' are constants such that T1(n) <= C * f1(n) and
T2(n) <= C' * f2(n).
Let D = the larger of C and C'. Then,
T1(n) + T2(n) <= C * f1(n) + C' * f2(n)
<= D * f1(n) + D * f2(n)
<= D * (f1(n) + f2(n))
<= O(f1(n) + f2(n))
Product Rule
Suppose T1(n) = O(f1(n)) and T2(n) = O(f2(n)). Then, we can
conclude that
T1(n) * T2(n) = O(f1(n) * f2(n)).
Analyzing Some Simple Programs (with No Subprogram Calls)
General Rules:
2) The time to execute a loop is the sum, over all times around the
loop, of the time to execute all the statements in the loop, plus the
time to evaluate the condition for termination. Evaluation of basic
termination conditions is O(1) in each iteration of the loop.
Example 7
Compute the bigOh running time of the following C++ code segment:
The number of iterations of a for loop is equal to the top index of the
loop minus the bottom index, plus one more instruction to account for
the final conditional test. Note: if the for loop terminating condition is i
<= n, rather than i < n, then the number of times the conditional test
is performed is:
((top_index + 1) – bottom_index) + 1)
Complexity problems may ask for "number of instructions executed" which means you
need to provide an equation in terms of n of the precise number of instructions executed.
Or, we may just ask for the complexity in which case you need only provide a bigOh (or
bigTheta) expression.
Example 8
Consider the sorting algorithm shown below. Find the number of instructions executed
and the complexity of this algorithm.
Statement 1 is executed n times (n 1 + 1); statements 2, 3, 8, and 9 (each representing
O(1) time) are executed n 1 times each, once on each pass through the outer loop. On
the first pass through this loop with i = 1, statement 4 is executed n times; statement 5 is
executed n 1 times, and assuming a worst case where the elements of the array are in
descending order, statements 6 and 7 (each O(1) time) are executed n 1 times.
On the second pass through the outer loop with i = 2, statement 4 is executed n 1 times
and statements 5, 6, and 7 are executed n 2 times, etc. Thus, statement 4 is executed (n)
– 12 –
+ (n1) +... + 2 times and statements 5, 6, and 7 are executed (n1) + (n2) + ... + 2 + 1
times. The first sum is equal to n(n+1)/2 1, and the second is equal to n(n1)/2.
= 5n - 5 + (4n2 - 2n) / 2
= 5n - 5 + 2n2 - n
= 2n2 + 4n - 5
= O(n2)
– 13 –
Example 9
The following program segment initializes a twodimensional array A (which has n rows
and n columns) to be an n x n identity matrix – that is, a matrix with 1’s on the diagonal
and 0’s everywhere else. More formally, if A is an n x n identity matrix, then:
A x M = M x A = M, for any n x n matrix M.
What is the complexity of this C++ code?
A program such as this can be analyzed in parts, and then we can use
the summation rule to find a total running time for the entire program.
Line 1 takes O(1) time. The instructions in lines 5 and 6 are executed
O(n) times. The instructions in lines 3 and 4 are executed n times
every time we execute this loop. The outer loop of line 2 is executed n
times, yielding a time complexity of O(n2), due to the inner loop begin
executed O(n) times. Thus the running time of the segment is O(1) +
O(n2) + O(n). We apply the summation rule to conclude that the
running time of the segment is O(n2).
WorstCase and Average Case Analysis
Thus far, we have been analyzing programs considering an input of size n, i.e., the
maximum number of input items. This is worstcase analysis; we are looking at the
largest number of execution steps required and determining the running time from that.
There may be situations where you want to know how long the algorithm runs in the
average case, not necessarily the worst case. Averagecase analysis is much more
involved than worstcase.
What you have to do is determine the probability of requiring a particular number of steps
(given a particular input) to solve a given problem. You also have to determine the
distributions of the various data values. For example, if you are writing a searching
algorithm, how often will the value you are looking for be the 1st, 10th or 1000th element
of the array you are searching? Using these probabilities, you can compute an average
case analysis, but it is totally dependent on the assumptions that you have made to set the
– 14 –
probabilities. That is always the hardest part of average case analysis. Because of these
complications, worstcase analysis is far more common (but less accurate).
Example 10
Here is a simple linear search algorithm that returns the index location of a value in an
array.
1 + 3 + 5 + ... + (2n - 1) / n
= (2 (1 + 2 + 3 + ... + n) - n) / n
We know that 1 + 2 + 3 + ... + n = n (n + 1) / 2, so the average number of lines executed
is:
[2 [n (n + 1) / 2] – n] / n
= n
= O(n)
Notice we are assuming that x is equally likely to be at any position in the array.
Analyzing Programs with NonRecursive Subprogram Calls
The first step is to analyze each individual function to determine its running time. We
begin with the functions that do not call any other functions. Then, we evaluate the
running times of the functions that call previously evaluated functions. Suppose we have
determined the running time of function P to be O(f(n)). Therefore, any statement
anywhere in the program that calls P also has a running time of O(f(n)). We just include
this time in our analysis of any section of the program where P is called.
– 16 –
1) while/repeat: add f(n) to the running time for each iteration. We then multiply that
time by the number of iterations. For a while loop, we must add one additional f(n)
for the final loop test.
2) for loop: if the function call is in the initialization of a for loop, add
f(n) to the total running time of the loop. If the function call is the
termination condition of the for loop, add f(n) for each iteration.
3) if statement: add f(n) to the running time of the statement.
int a, n, x;
void main(void) {
7) n = GetInteger();
8) a = 0;
9) x = foo(a, n)
10) printf("%d", bar(a, n))
}
We begin analyzing bar since it does not call any other subprograms.
The for loop adds each of the integers 1 through n - 1 to x. Thus, the
value that is returned by bar(x, n) = x + n(n - 1)/2. The running time
of bar is analyzed as follows: Line 1 iterates n times, and line 2 iterates
– 17 –
Finally, we look at main. Lines 7 and 8 each take O(1). The call to foo
on line 9 takes O(n2). The printf of line 10 takes O(1) plus a call to bar
which gives us O(1) + O(n). Thus, the total running time for main is
O(1) + O(1) + O(n2) + O(n) = O(n2).
Here is the body of a function:
sum = 0;
for (i = 1; i <= f(n); i++)
sum += i;
where f(n) is a function call. Give a bigoh upper bound on this
function if the running time of f(n) is O(n), and the value of f(n) is
n!:
Classes of Problems
Imagine that you have decided to ski across the Sierras over winter break. Since there are
no McDonalds between Yosemite and Lee Vining, you will need to bring all your food
with you in your pack. On this trip, taste is a secondary consideration. What matters is
how much your food weighs and how many calories it contains. After raiding your
kitchen, you discover that you have roughly 200 different food items. Each item has a
certain weight and contains a certain number of calories:
Given this selection of food, your task is to find the best subset that maximizes the
number of calories, but falls within the weight limit you can carry in your pack (say
15kg). One algorithm that definitely produces a solution is to simply try every possible
– 18 –
combination. Unfortunately, there are 2200 such combinations, and the Sierras would
erode away before that algorithm terminates.
Is there another algorithm that runs in reasonable (i.e., polynomial) time? If there is, no
one has found it yet. This might make you suspect that no polynomial time solution
exists, yet no one has ever been able to prove that either. This problem, known as the
Knapsack Problem, is an example of a general class of problems called the NPcomplete
problems. These are problems for which no polynomial time solution exists, but it is
unproven whether the problems necessarily require exponential time.
Problems, Problems, Problems...
In studying algorithms, we have seen functions and bigoh notation used to characterize
the running time of algorithms. There are some important terms used to classify
problems, based on the running time of the algorithms that solve those problems. First,
we review the different categories of functions for bigO notation:
The ones that involve n as an exponent (n! or nn) are called exponential functions.
Functions whose growth is <= nc for some relatively small constant c are polynomial
functions. Linear functions have growth proportional to n. Sublinear functions have
growth proportional to log n. A constant time function has growth independent of n (do
you know of any algorithms with running time of constant order?)
These descriptions of functions are frequently used to characterize different sets of
problems. The general categories are:
Intractable/Exponential: These are problems that require exponential time.
Moreover, it has been proven that a solution in less than exponential time is
impossible. For example, consider the problem of listing all possible committees
of size one or more that could be formed from a group of n people. There are 2n
1 such committees so any algorithm that solves this problem must have at least 2n
1 steps, and thus a running time proportional to 2n. This problem cannot
possibly have a polynomial solution.
Polynomial: These are problems for which sublinear, linear or polynomial
solutions exist. Insertion sort, quicksort, mergesort and other sorting algorithms
– 19 –
are good examples, as well as finding the smallest or largest values in a list. Any
algorithm whose running time grows proportional to a polynomial function can
solve reasonablesize problems if the constant in the exponent is small.
NPComplete: No polynomial solution has been found, although exponential
solutions exist. However, it has not been proven that a polynomial solution could
not be found.
Some famous examples:
a) Generalized Instant Insanity: Given n cubes, where each face of the
cube is painted in one of n colors, is it possible to stack the cubes so that
each of the n colors appears exactly once on each side of the column?
b) Traveling Salesperson (TSP): Given a map of cities and a cost of travel
for each pair of cities, is it possible to visit each city exactly once and
return home for less than k dollars?
c) nbookshelf packing: You are given k books to put on n shelves. Will
they fit? (the books are all of different sizes). This is another way of
stating the Knapsack problem.
This group of problems is very interesting for a number of reasons. First of all,
the "NP" means "nondeterministic polynomial". Nondeterministic does not mean
that we have not yet been able to determine if the problem is polynomial. It
means that the algorithm that we might define to solve one of these problems is
nondeterministic, i.e., there is an element of chance in the algorithm. For
example, we might write a solution as follows for the Traveling Salesperson
problem:
Choose one of the possible paths, and compute its total
cost.
If this cost is no greater than the allowable cost,
then declare a success
else declare nothing.
Someone must choose one of the possible paths (this is the nondeterministic part).
If we choose the right one, the problem is solved quickly; if not, we learn nothing.
– 20 –
We define the running time of an NP algorithm as the time required to execute the
algorithm if we make the "right" choice, meaning the choice that would lead to a
solution in the optimal amount of time. So we are trying to measure how good the
algorithm is, not how good our choices are.
According to this definition, the Traveling Salesperson problem has a polynomial
solution. If we make the right choice, we just have to compute the distance for n
cities which is proportional to n.
Of course, the class of NP problems includes the truly polynomial problems. If
we can design a deterministic (no choices) polynomial solution for a problem,
then we surely can design a nondeterministic one. In addition, the Traveling
Salesperson problem suggests that there might be NP problems that do not have
polynomial solutions. In other words, there appear to be problems that can be
solved in polynomial time by nondeterministic algorithms, but that might require
exponential time by a deterministic algorithm. But no one has been able to prove
this. In particular, no one has been able to show that there does not exist a
polynomial solution to the Traveling Salesperson problem. This is still one of the
open questions in theoretical computer science (but most computer scientists are
of the opinion that a polynomial solution does not exist for TSP).
So we have the following problem classification:
NPComplete?
Let’s look at some important NPcomplete problems in more detail to see if we
can better understand these problems.
Two Famous Problems
– 21 –
1. Satisfiability : The problem is to determine whether any given logical formula is
satisfiable, i.e., whether there exists a way of assigning TRUE and FALSE to its
variables so that the result is TRUE.
Given a formula
• composed of variables a, b, c, ... and their logical complements, ~a, ~b, ~c...
• represented as a series of clauses, in which each clause is the logical OR (v) of
variables and their logical complements
• expressed as the logical AND (^) of the clauses
Is there a way to assign values to the variables so that the value of the formula is TRUE?
If there exists such an assignment, the formula is said to be satisfiable.
Is: (a) ^ (b v c) ^ (~c v ~a) satisfiable? ________________________________
Is: (a) ^ (b v c) ^ (~c v ~a) ^ (~b) satisfiable? ___________________________
2. Knapsack : The name of the problem refers to packing items into a knapsack. Is there
a way to select some of the items to be packed such that their “sum” (the amount of
space they take up) exactly equals the knapsack capacity? We can express the
problem as a case of adding integers. Given a set of nonnegative integers and a target
value, is there a subset of integers whose sum equals the target value?
Formally, given a set {a1, a2, ..., an} and a target sum T, where ai >= 0, is there a
selection vector V = [v1, v2, ..., vn] each of whose elements is 0 or 1, such that
n
Σ (ai * vi) = T
i = 1
For example, the set might be {4, 7, 1, 12, 10}. A solution for target sum T = 17 exists
when V = [1, 0, 1, 1, 0] since 4 + 1 + 12 = 17. No solution is possible if T = 25.
Characteristics of NPComplete Problems
The preceding examples are reasonable representatives of the class of NPComplete
problems. They have the following characteristics:
– 22 –
• Each problem is solvable, and a relatively simple approach solves it (although
the approach may be very timeconsuming). For each of them, we can simply
enumerate all the possibilities, all ways of assigning the logical values of n
variables, all subsets of the set S. If there is a solution, it will appear in the
enumeration; if not, we will discover this as well.
• There are 2n cases to consider if we use the enumeration approach. Each
possibility can be tested in a relatively small amount of time, so the time to
test all possibilities and answer “yes” or “no” is proportional to 2n.
• The problems are apparently unrelated coming from logic, number theory,
graph theory, etc.
• If it were possible to guess perfectly, we could solve each problem in a very
small amount of time. The verification process could be done in polynomial
time, given a good guess.
How Can You Tell If a Problem is NPComplete?
Suppose you are working on some problem p, and you just can't seem to find a solution
that runs in polynomial time. In addition, you have tried to prove that the problem
requires exponential time and have failed at that as well. Does that mean that p is an NP-
Complete problem? Unfortunately, it's not that easy. It could very well be that there is a
simple solution that you haven't found yet, or it could be that the problem is intractable but
you just can't see the proof.
Convert_To_P2 p1 = ... /* Takes an instance of p1 and converts
it to an instance of P2 in
polynomial
time. */
Solve_P2 p2 = ... /* Solves problem P2 */
Solve_P1 p1 = Solve_P2(Convert_To_P2 p1);
– 23 –
Given the above definition of a transformation, these theorems should not be very
surprising:
If Π1 ∝ Π2 then
Π2 ∈ P → Π1 ∈ P
If Π1 ∝ Π2 then
Π2 ∉ P → Π1 ∉ P
To Prove Π ∈ NP:
1) Find a known NPcomplete problem ΠNP
2) Find a transformation such that ΠNP ∝ Π
3) Prove that the transformation is polynomial.
The results of Cook and Karp include the converse: if for even one of
these problems (or any NP-Complete problem) it could be shown that
there is no deterministic, polynomial time algorithm to solve it, then
no deterministic, polynomial time algorithm can exist for any of them.
Bibliography
Historical Notes
The origin of Big-Oh notation is attributed to Paul Bachmann (1837-1920) in his
number theory text written in 1892. The actual O symbol is sometimes called a
Landau symbol after Edmund Landau (1877-1938) who used this notation
throughout his work. The study of the running time of programs and the
computational complexity of problems was pioneered by Hartmanis and Stearns
(see above). Donald Knuth's series of books established the study of running time
of algorithms as an essential ingredient of computer science. The class of
polynomial problems was introduced in 1964 by Alan Cobham ("The Intrinsic
Computational Difficulty of Functions" in Proceedings of the 1964 Congress for
Logic, Methodology, and the Philosophy of Science), and, independently by Jack Edmonds in
1965 ("Paths, Trees and Flowers” in Canadian Journal of Mathematics, 17).