Jump to content

Course:CPSC 320/Midterm 1 Reference Sheet

From UBC Wiki

CPSC 320 2009W2 Exam Reference Sheet

Do not remove: This reference sheet is the appendix for Midterm #1. Only the first 4 printed pages will be used; so be compact!

  • f(n)O(g(n)) iff c𝐑+,n0𝐙+,n𝐙+,nn0f(n)cg(n)
  • f(n)Ω(g(n)) iff g(n)O(f(n)), c𝐑+,n0𝐙+,n𝐙+,nn00cg(n)f(n)
  • f(n)Θ(g(n)) iff f(n)O(g(n)) and g(n)O(f(n)), c1,c2𝐑+,n0𝐙+,n𝐙+,nn00c1g(n)f(n)c2g(n)
  • f(n)o(g(n)) iff c𝐑+,n0𝐙+,n𝐙+,nn0f(n)<cg(n)
  • f(n)ω(g(n)) iff g(n)o(f(n)), c𝐑+,n0𝐙+,n𝐙+,nn00cg(n)<f(n)

Also, if limnf(n)g(n)=

  • , then f(n)ω(g(n))
  • a non-zero constant, then f(n)Θ(g(n))
  • 0, then f(n)o(g(n))

(Plus bear in mind that (1) L'Hopital's rule may be handy, and (2) the limit is not always well-defined!)

L'Hopital's Rule:

If limnf(n)g(n)=00 or limnf(n)g(n)=, then limnf(n)g(n)=f(n)g(n)

Analogy to Inequalities

  • f(n)=O(g(n)) if and only if g(n)=Ω(f(n))
  • f(n)=o(g(n)) if and only if g(n)=ω(f(n))

It follows, where af(n) and bg(n):

  • f(n)=O(g(n))ab
  • f(n)=Ω(g(n))ab
  • f(n)=Θ(g(n))a=b
  • f(n)=o(g(n))a<b
  • f(n)=ω(g(n))a>b

Caution: For all a,b,𝐑 exactly one must hold: a<b,a=b or a>b. Not all functions are asymptotically comparable.

Master Theorem

If T(n)=aT(n/b)+f(n) and a constant in the base case, where n/b can be either n/b or n/b, a>=1 and b>1 then:

  • If f(n)O(nlogba) for some >0, then T(n)Θ(nlogba)
    • Dominated by leaf cost
  • If f(n)Θ(nlogba) then, T(n)Θ(nlogbalgn)
    • Balanced cost
  • If f(n)Ω(nlogba+) for some >0 and if af(n/b)cf(n) for some constant c<1 and sufficiently large n, then T(n)Θ(f(n))
    • Dominated by root cost - It is important in this case to check that f(n) is well behave. More specifically,
    • For sufficient large n, a*f(n/b) <= c*f(n)

The following equations cannot be solved using the master theorem:

  • T(n)=2nT(n2)+nn
    a is not a constant
    T(n)=2T(n2)+nlogn
    non-polynomial difference between f(n) and nlogba
    T(n)=0.5T(n2)+n
    a<1 cannot have less than one sub problem
    T(n)=64T(n8)n2logn
    f(n) is not positive
    T(n)=T(n2)+n(2cosn)
    case 3 but regularity violation.

Also, Case 3 always hold when f = n^k and If f(n)Ω(nlogba+)

Log Laws

For all real a > 0, b > 0, c > 0, and n,

  • a=blogba
  • logcab=logca+logcb
  • logban=nlogba
  • logba=logcalogcb
  • logb1a=logba
  • logba=1logab
  • alogbc=clogba
  • log1=0

Summation

  • Arithmetic Series: k=1n(k)=1+2++n=12n(n+1)
  • Sum of Squares: k=0n(k2)=n(n+1)(2n+1)6
  • Sum of Cubes: k=0n(k3)=n2(n+1)24
  • Geometric Series: k=0n(xk)=1+x+x2++xk=xn+11x1, for real x1
  • Infinite decreasing: k=0(xk)=11x,for|x|<1
  • Telescoping: k=1n1(akak+1)=a0an

Exponents

  • a0=1
  • a1=a
  • a1=1a
  • (am)n=amn
  • am×an=am+n

Decision Tree-Related Notes

  • for a list of n elements, there's n! permutations
  • a binary tree of height d has at most 2d leaves
  • therefore, a binary tree with at least n leaves must have height at least lgn
  • to get the height of a tree: the longest path (say we divide by 3/2 at each level) finishes when (23)kn=1k=log32n, so the height is log32n
  • lg(n!)Θ(nlgn), which we can establish by proving big-O and big-Ω bounds separately (pumping "up" or "down" the values of the terms in the factorial and the overall number of terms as needed)

Stirling's Approximation

  • lnn!nlnnn .

Derivatives

  • ddxaf(x)=af(x)*d(f(x))dxln(a)
  • ddxln(x)=1x,x>0
  • ddxloga(x)=1xln(a)

The Silicon Downs : Furlongs of Asymptotic Complexity

"NEWS FLASH: Mounties Find Silicon Downs Fixed!"

  • Constant O(1)
  • Logarithmic O(logn) ie. logkn,logn2O(logn)
  • Poly-Log O(logkn)
  • Linear O(n)
  • Log-Linear O(nlogn)
  • Superlinear O(n1+c) where: c is a constant > 0
  • Quadratic O(n2)
  • Cubic O(n3)
  • Polynomial O(nk) where: k is a constant, "tractable"
  • Exponential O(cn) where: c is a constant > 1, "intractable"

Stable Marriage Notes

Algorithm:

initialize all men in M and all women in W to unengage
while (an unengaged man with at least one woman on his preferance list remains)
     do choose such a man m (element of) M
       propose to the next woman w(element of)W on his preference list
     if( w is unengaged)
          then engage m to w
     else if w prefers m to fiance m'
           then break engagement of m' to w
          engage m to w
      cross w off m's preferance list
report set of engaged pairs as final matching.


Notes:

  1. All executions of the stable marriage sort produce the same matching, and all men end up with their "best possible partner".
  2. In the stable matching set each woman is paired with her worst valid partner.
  3. This algorithm terminates after at most n2 iterations of the while loop.

Skyline Problem & Algorithm

For input B[1..n] buildings as triples (left, height, right), we get output of tuples (pos of height change, new height):

SKYLINE-ALG(B) {
  If there is one building (x1,h,x2)
    output (x1,h),(x2,0)
  Else
    divide into B1[1..floor(n/2)] and B2[floor(n/2)+1..n]
    out1=SKYLINE-ALG(B1)
    out2=SKYLINE-ALG(B2)
    OUTPUT=MERGE(out1,out2)
}

The merge step is as follows:

MERGE(out1,out2) {
  h1=h2=0, currx=0,curry=0
  While out1 or out2 is not empty do {
    If out1 and out2 start at the same x-coord
      currx=first x-coord in out1
      h1=first y-coord in out1
      h2=first y-coord in out2
      DELETE first entries in out1 and out2
    Else If out1 has smallest first x-coord
      currx=first x-coord in out1
      h1=first y-coord in out1
      DELETE first entry in out1
    Else
      currx=first x-coord in out2
      h2=first y-coord in out2
      DELETE first entry in out2
    If max(h1,h2)!=curry
      APPEND (currx,max(h1,h2)) to output
  }
  APPEND (currx,0) to output
}

Proving a Loop Invariant

  1. induction variable: Number of times through the loop
  2. base case: Prove the invariant true before the loop starts
  3. inductive hypothesis: Assume the invariant holds just before begininng some (unspecified) iteration of the loop.
  4. inductive step: prove the invariant holds at the end of that iteration (right before the next iteration)
  5. extra-bit: make sure the loop will eventually terminate.