Docsity

Asymptotic Analysis: Understanding Function Dominance and Complexity, Slides of Data Structures and Algorithms

Partial preview of the text

Download Asymptotic Analysis: Understanding Function Dominance and Complexity and more Slides Data Structures and Algorithms in PDF only on Docsity!

Program Analysis

Problem of the Day

Find two functions f (n) and g(n) that satisfy the following relationship. If no such f and g exist, write ”None”.

  1. f (n) = o(g(n)) and f (n) 6 = Θ(g(n))
  2. f (n) = Θ(g(n)) and f (n) = o(g(n))
  3. f (n) = Θ(g(n)) and f (n) 6 = O(g(n))
  4. f (n) = Ω(g(n)) and f (n) 6 = O(g(n))

Implications of Dominance

  • Exponential algorithms get hopeless fast.
  • Quadratic algorithms get hopeless at or before 1,000,000.
  • O(n log n) is possible to about one billion.
  • O(log n) never sweats.

Testing Dominance

f (n) dominates g(n) if limn→∞ g(n)/f (n) = 0, which is the same as saying g(n) = o(f (n)). Note the little-oh – it means “grows strictly slower than”.

Dominance Rankings

You must come to accept the dominance ranking of the basic functions:

n!  2 n^  n^3  n^2  n log n  n  log n  1

Advanced Dominance Rankings

Additional functions arise in more sophisticated analysis than we will do in this course:

n!  cn^  n^3  n^2  n1+^  n log n  n 

n 

log^2 n  log n  log n/ log log n  log log n  α(n)  1

Selection Sort

selection sort(int s[], int n) { int i,j; int min; for (i=0; i<n; i++) { min=i; for (j=i+1; j<n; j++) if (s[j] < s[min]) min=j; swap(&s[i],&s[min]); } }

Worst Case Analysis

The outer loop goes around n times. The inner loop goes around at most n times for each iteration of the outer loop Thus selection sort takes at most n × n → O(n^2 ) time in the worst case.

Logarithms

It is important to understand deep in your bones what logarithms are and where they come from. A logarithm is simply an inverse exponential function. Saying bx^ = y is equivalent to saying that x = logb y. Logarithms reflect how many times we can double something until we get to n, or halve something until we get to 1.

Binary Search

In binary search we throw away half the possible number of keys after each comparison. Thus twenty comparisons suffice to find any name in the million-name Manhattan phone book! How many time can we halve n before getting to 1? Answer: dlg ne.

Logarithms and Bits

How many bits do you need to represent the numbers from 0 to 2 i^ − 1? Each bit you add doubles the possible number of bit patterns, so the number of bits equals lg(2i) = i.

Logarithms and Multiplication

Recall that

loga(xy) = loga(x) + loga(y)

This is how people used to multiply before calculators, and remains useful for analysis. What if x = a?

Federal Sentencing Guidelines

2F1.1. Fraud and Deceit; Forgery; Offenses Involving Altered or Counterfeit Instruments other than Counterfeit Bearer Obligations of the United States.(a) Base offense Level: 6 (b) Specific offense Characteristics (1) If the loss exceeded $2,000, increase the offense level as follows: Loss(Apply the Greatest)(A) $2,000 or less Increase in Levelno increase (B) More than $2,000(C) More than $5,000 add 1add 2 (D) More than $10,000(E) More than $20,000 add 3add 4 (F) More than $40,000(G) More than $70,000 add 5add 6 (H) More than $120,000(I) More than $200,000 add 7add 8 (J) More than $350,000(K) More than $500,000 (^) add 10add 9 (L) More than $800,000(M) More than $1,500,000 add 11add 12 (N) More than $2,500,000(O) More than $5,000,000 add 13add 14 (P) More than $10,000,000(Q) More than $20,000,000 add 15add 16 (R) More than $40,000,000(Q) More than $80,000,000 add 17add 18

Make the Crime Worth the Time

The increase in punishment level grows logarithmically in the amount of money stolen. Thus it pays to commit one big crime rather than many small crimes totalling the same amount.