Docsity

Analyzing Algorithms, Lecture notes of Computer Science

Toggle sidebar
bg1
Analyzing Algorithms
1. Running time
a. How fast it is
2. Space usage
a. Memory usage
Asymptotic Analysis
- How running time grows as input size increases
- Experimental analysis
oWrite the code and test it on a computer
oGood computers can make slow algos look good
- Theoretical analysis (preferred)
oUse pseudocode
oUses Random Access Machine model
Primitive Operations
Instead of seconds we count steps
oAssigning value to variable
oCalling method
oArithmetic operation
oIndexing
oComparison
oWorst case vs best case
Worst: The numbers are in increasing order (1, 2, 3, 4).
The if statement is always true, so we do the maximum
amount of work.
= 5n
pf3
pf4
pf5
pf8

Partial preview of the text

Download Analyzing Algorithms and more Lecture notes Computer Science in PDF only on Docsity!

Analyzing Algorithms

  1. Running time a. How fast it is
  2. Space usage a. Memory usage Asymptotic Analysis
  • How running time grows as input size increases
  • Experimental analysis o Write the code and test it on a computer o Good computers can make slow algos look good
  • Theoretical analysis (preferred) o Use pseudocode o Uses Random Access Machine model  Primitive Operations  Instead of seconds we count steps o Assigning value to variable o Calling method o Arithmetic operation o Indexing o Comparison o Worst case vs best case  Worst : The numbers are in increasing order (1, 2, 3, 4). The if statement is always true, so we do the maximum amount of work.   = 5n

Best: The first number is the biggest (10, 1, 2, 3). We never update currentMax inside the loop. This is roughly 5n operations.  = 7n- o Big Oh notation  We don't usually say "This algorithm takes 7n - 2 steps." We simplify it to its most important part. As n gets very large (millions or billions), the "-2" and the "7" don't matter as much as the “n."  If an algorithm's time is proportional to n, we say it is O(n) (Linear time).If it has nested loops (a loop inside a loop), it is often O(n^2) (Quadratic time).

  • Big Oh family o We proved: o Big-Oh (O):  The Upper Bound. "This algorithm will be at most this slow." It’s the pessimistic view we usually care about.  At most it is 7n-2, roughly n. o Big-Omega (Omega):  The Lower Bound. "This algorithm will be at least this fast."  At least 5n, roughly n o Big-Theta (Theta):  The Tight Bound. The true growth rate.  If big Oh and Omega grow same, we can declare tight match

Theorem 1.

  1. Constant scaling a. If d(n) is O(f(n)), then for any positive constant a, a·d(n) is also O(f(n)).
  2. Sum Rule a. d(n)+e(n) is O(f(n)+g(n)) i. n² + n log n is O(n² + n log n) = O(n²)

R 1. Order the following list of functions by the big-Oh notation. Group together (for example, by underlining) those functions that are big-Theta of one another.

  1. Decreasing / inverse growth

a. 1/n,

  1. Constant a. 2^

3. Logarithmic (o nly place n appears is inside a logarithm , (logn)^k)

a. loglogn, b. Sqrt(logn) c. log^2n

  1. Sublinear polynomial (power of n, but less than 1) a. n^0. b. Ceil(Sqrt(n)) c. 3n^0.
  2. Linear a. 2^logn i. = n b. 5n
  3. Linear × logarithmic (near-linear) a. nlog4n b. 6nlogn c. 2nlog^2n
  4. Polynomial a. 4n^3/ b. 4^log2n i. = n^log2(4) ii. = n^ c. n^2logn d. n^
  5. Exponential a. 2^n b. 4^n
  6. Super exponential (faster than c^n) a. 2^(2^n) R 1. Bill has an algorithm, find2D, to find an element x in an n x n array A. The algorithm find2D iterates over the rows of A and calls the algorithm arrayFind, of Algorithm 1.12, on each one, until x is found or it has searched all rows of A. What is the worst-case running time offind2D in terms of n? Is this a linear-time algorithm? Why or why not?

We say f(n) is O(g(n)) if two constants

  • c > 0
  • n0 >= 1 (starting point)
  • .. such that f(n) <= c * g(n) for all n >= n = n^5 +5n^4+10n^3 +10n^2+5n+ Choose n = 1, c = 35 = 1 + 5 + 10 + 10 + 5 = 31 For c = 32 and n0 = 1, fn <= c * g(n) for all n >= n To be safe can choose 35. Thus since n^5 +5n^4+10n^3 +10n^2+5n+1 <= c*(n^5) for c = 35 and n >= 1, (n+1)^5 = O(n^5) R 1. 2^(1+1) = 2^2 = 4 < 3(2^1)

So for c = 3, and n0 = 1, since 2^(n+1) <= c (2^n) for all n >= 1, then 2^(n+1) = O(2^n) R 1. So big O is upper bound,

  • Given c and n0, fn <= c(g(n)) for n > n Omega flips this
  • Given c and n0, fn >= c(g(n)) for n > n
  • n^3 logn >= cn^3 (divide both sides by n^3)
  • logn >= c o so value for logn being a constant o n = 2 o log2(2) = 1
  • n^3logn >= n^3 for n > 2 (n0 = 2), and c = 1