Docsity

Analysis of Algorithm Complexity: O Notation and Recursive Algorithms - Prof. Vicki H. All, Study notes of Computer Science

Toggle sidebar
bg1
Algorithm Analysis CS 2420 Page 1
O Notation (Big Oh)
• We want to give an upper bound on the amount of time it takes to solve a problem. Why
would we settle for an upper bound instead of a tight bound?
• Definition:
∃
↔Ο= ))(()( nfnv constant c and 0
nsuch that )()( nfcnv ≤ whenever
0
nn ≥.
o The growth rate of )(NT is less than or equal to that of )(NF
• Is an )( 2
nΟalgorithm always better than an )( 3
nΟalgorithm?
o No, it depends on the specific constants. For small n, c may dominate.
o But if n is large enough, an )(3
nΟalgorithm is always slower than an )(2
nΟ
algorithm. Can you prove that?
• The different complexity classes are (in order): )1(
Ο
(constant), )(logn
Ο
(logarithmic),
)(nΟ (linear), )log( nnΟ (n log n), )(2
nΟ (quadratic), )(3
nΟ (cubic),
)2( n
Ο(exponential), )!(nΟ(factorial).
• Intractable – all known algorithms to solve a
problem are of exponential complexity.
• What kinds of problems have an exponential
solution? What about the knapsack problem?
Given a set of items, each with a cost and a
value, then determine the number of each item to
include in a collection so that the total cost is
less than some given cost and the total value is
as large as possible.
For iterative algorithms, it is often easy to see the
complexity – but recursive algorithms are a bit more
difficult.
Recursive Algorithms often have the same general forms
)()/()( k
nbnaTnT Ο+= - each call reduces the problem size by a factor of b and makes “a”
recursive calls. The last term indicates the amount of work per call. (as a function of a power of
n). Note, in terms of complexity, we don’t care whether the work is done before or after (or in
between) the recursive calls.
void doit(int n)
{ if (n==1) return;
for (i=0; i<n; i++)
x = x + i;
doit(n/2);
doit(n/2);
}
void doit(int n)
{ if (n==1) return;
for (i=0; i<n; i++)
x = x + i;
doit(n/2);
}
void doit(int n)
{ if (n==1) return;
x++;
doit(n/2);
doit(n/2);
}
void doit(int n)
{ if (n==1) return;
for (i=0;i<n; i++)
x = x + i;
doit(n-1);
}
pf2

Partial preview of the text

Download Analysis of Algorithm Complexity: O Notation and Recursive Algorithms - Prof. Vicki H. All and more Study notes Computer Science in PDF only on Docsity!

Algorithm Analysis CS 2420 Page 1

O Notation (Big Oh)

  • We want to give an upper bound on the amount of time it takes to solve a problem. Why would we settle for an upper bound instead of a tight bound?
  • Definition: v ( n )= Ο( f ( n ))↔∃constant c and n 0 such that v ( n )≤ cf ( n ) whenever n ≥ n 0. o The growth rate of T ( N )is less than or equal to that of F ( N )
  • Is an Ο( n^2 ) algorithm always better than an Ο( n^3 ) algorithm? o No, it depends on the specific constants. For small n , c may dominate. o But if n is large enough, an Ο( n^3 )algorithm is always slower than an Ο( n^2 ) algorithm. Can you prove that?
  • The different complexity classes are (in order): Ο( 1 )(constant), Ο(log n )(logarithmic), Ο( n )(linear), Ο( n log n )(n log n), Ο( n^2 ) (quadratic), Ο( n^3 )(cubic), Ο( 2 n )(exponential), Ο( n !)(factorial).
  • Intractable – all known algorithms to solve a problem are of exponential complexity.
  • What kinds of problems have an exponential solution? What about the knapsack problem? Given a set of items, each with a cost and a value, then determine the number of each item to include in a collection so that the total cost is less than some given cost and the total value is as large as possible.

For iterative algorithms, it is often easy to see the complexity – but recursive algorithms are a bit more difficult.

Recursive Algorithms often have the same general forms

T ( n )= aT ( n / b )+Ο( nk ) - each call reduces the problem size by a factor of b and makes “a”

recursive calls. The last term indicates the amount of work per call. (as a function of a power of n). Note, in terms of complexity, we don’t care whether the work is done before or after (or in between) the recursive calls.

void doit(int n) { if (n==1) return; for (i=0; i<n; i++) x = x + i; doit(n/2); doit(n/2); }

void doit(int n) { if (n==1) return; for (i=0; i<n; i++) x = x + i; doit(n/2); }

void doit(int n) { if (n==1) return; x++; doit(n/2); doit(n/2); }

void doit(int n) { if (n==1) return; for (i=0;i<n; i++) x = x + i; doit(n-1); }

Algorithm Analysis CS 2420 Page 2

A Formula Approach

  • Mathematicians have developed a formula approach to determine complexity.
  • Theorem: Assume T ( n )= aT ( n / b )+Ο( nk )is the time function. o If a > bk , the complexity is Ο( n log^ ba ). o If a = bk , the complexity is Ο( n k log n ). o If a < bk the complexity is Ο( nk ).
  • Nice as it reinforces what we compute by drawing AND solves additional problems.
  • Note, only divide and conquer type of problems work for this method.

Determining Complexity from Experimental Evidence

n T(n) 2 10 4 10 8 10 16 11 32 8

n T(n) 2 10 4 17 8 32 16 66 32 130

n T(n) 2 10 4 10 8 15 16 20 32 25

n T(n) 2 8 4 32 8 500 16 65, 32 4 x10^9

n T(n) 2 2 4 8 8 25 16 64 32 161

  • How do you figure out the complexity from experimental data when you know neither the constant nor the complexity? I “eyeball it” to come up with a good guess. Then I figure out the constant for one entry. Next I see if that constant works for all the entries. This is basically my “approximate-then-finalize” approach. How do I guess? o Ο( 1 )is basically constant. o Ο(log n )grows slowly, by a constant between entries. o Ο( n )doubles between entries. o Ο(^ n log n )slightly more than doubles between entries. o Ο( n^2 )quadruples between entries. o Ο( 2 n )grows exponentially.

Another way to check if the complexity is correct is to divide the experimental time by the complexity. Below represents three attempts at finding the complexity. The first is an overestimate. Notice how the ratio goes to zero. The second is correct, showing the constant is approximately 4.3. The third is an underestimate. Notice how the ratio increases.

n Actual time n 3 ratio n 2 ratio n log n ratio 4 75 64 1.17 16 4.7 8 9. 8 219 512 0.43 64 3.4 24 9. 16 1104 4096 0.27 256 4.3 64 17. 32 4398 32768 0.13 1024 4.3 160 27. 64 17632 262144 0.07 4096 4.3 384 45.