CSCE 121 Algorithm

From Notes
Jump to navigation Jump to search

« previous | Friday, December 3, 2010 | next »

Set of steps that define how a task is to be performed.

Pseudocode

Generalization of a programming language to jot down main idea of an algorithm

  • Assignment (typically written :=)
  • Common control structures (if-then-else, while, for, repeat)
  • English phrases when convenient:
    • If we already know how to do something, or
    • we haven't figured out how to do something yet

Finding Algorithms

No automatic way

Requires:

  • trial and error
  • familiarity
  • insight and creativity

General approaches:

reduction
find similar problem and adapt it to this problem
step-wise refinement / divide and conquer
break problem down
recursion often used to solve subproblems

Example: Search

Given a sequence of elements, is a particular element actually in the sequence?

Sequential search O(n)
scan sequence from beginning to end looking for desired target element:
Binary search O(log2n)
check middle element of sorted sequence.
If target is smaller, it will be in first half, if larger it will be in second half.
Recurse

Which is better (faster)?

depends on hardware, but more importantly the underlying (abstract) algorithm that program implements

Asymptotic Analysis

Describe running time as limnf(n) where n is input size;

Can also be described as the number of basic steps of the algorithm according to pseudocode description

All of the definitions of Big-O, Big-Ω, and Big-Θ below have something to do with existentially (∃) bound constants (generally C and n0) that make definition true. These constants are referred to in the discrete mathematics textbook as witnesses.

Definition of Dominance

Also referred to as asymptotic comparison

In general, we say f is asymptotically less than or equal to () g if and only if there exists a natural number n0 such that f(n)g(n) for all n>n0

fgn0 n(nn0f(n)g(n))

Conversely, we say g is asymptotically greater than or equal to () f.

Example

Let f(n)=5n and g(n)=n2:

  • When n<5, f(n)>g(n)
  • Asymptotically, g "grows faster" than f, so g(n)f(n) when n>5
  • Given the definition above, we can say that 5nn2


Big O

An upper bound on a function f:

f(n)O(n)f(n)Cnf(n)n
Precise Definition: f(n) is big oh of g(n) if and only if there exists a constant C and a natural number n0 such that |f(n)|C|g(n)| for all n>n0
f(n)O(g(n))C n0 n(nn0|f(n)|C|g(n)|)

Common Order of Dominance

  1. O(nn) exponential
  2. O(n!) factorial
  3. O(n2) polynomial
  4. O(nlogn)
  5. O(n) linear
  6. O(n)
  7. O(logn) logarithmic
  8. O(1) constant


Big Ω

A lower bound on a function f [1]:

f(n)Ω(n)f(n)Cnf(n)n
Precise Definition: f(n) is big omega of g(n) if and only if there exists a constant C and a natural number n0 such that |f(n)|C|g(n)| for all n>n0
f(n)Ω(g(n))C n0 n(nn0|f(n)|C|g(n)|)
Note: f(n)=Ω(g(n))g(n)=O(f(n))


Big Θ

Means that function has same asymptotic growth as another function up to multiplication by constants. Similar to squeeze theorem in Calculus for proof of convergence.

f(n)Θ(g(n)) L<(limn|f(n)||g(n)|=L)
Precise Definiton: f(n) is big theta (same order) of g(n) if and only if there exists constants L and U and a natural number n0 such that |f(n)| is betweenL|g(n)| and U|g(n)| for all n>n0.
f(n)Θ(g(n))L,U n0 n(nn0L|g(n)||f(n)|U|g(n)|)

In other words,

f(n)Θ(g(n))(f(n)O(g(n))f(n)Ω(g(n)))

In this case, n0 for Big-Θ takes the larger value of the n0's used in Big-O and Big-Ω.


Examples

Example 1

  • Claim 5n=O(n2)
  • Choose witnesses C=5 and n0=1 (can be derived mathematically to fit the form of the definition of Big-O: |5n|5|n2| for all n1)
  • {5n<5n2|5n|5|n2|} true  n1


Example 2

When Joe implements algorithm A in Java and runs it on his home PC. Running time is

f1(n)=7n+52

When Sue implements algorithm A in Fortran and runs it on dilbert.cs.tamu.edu, the running time is

f2(n)=2n+25

Resulting speed of both algorithms is O(n)


Example 3

7n2+6n+2=O(n2)n33n+2=O(n3)(7n2+6n+2)(n33n+2)=O(n2n3)=O(n5)


Example 4

(See Wikipedia:Binomial coefficient→)

(n2)=n(n1)2=n22n2=n22+O(n)=O(n2)

Footnotes

  1. comparison-based sorting algorithms need at least Ω(nlogn) comparisons.


Example: Sort

Sorting Algorithm Animations

Merge sort

sort(A, start, end) {
  if A.size = 1 then return A
  mid = (end-start)/2;
  A1 = sort(A, start, mid);
  A2 = sort(A, mid+1, end);
  A3 = merge(A1, A2);
  return A3;
}

Speed: O(nlogn) (better than O(n2))


Numerical analysis: Bisection Method

k=?

Where f(x)=x2k crosses x-axis (solve for f(x)=0)

sqrt2() {
  // choose starting interval [x1, x2] carefully
  x1 := 0
  x2 := 2

  // evolving estimate of sqrt(2)
  x3 := 0

  // desired error
  e = .000001

  while(abs(x1-x2) >= e) {
    x3 := (x1+x2)/2
    if (f(x1) * f(x3) < 0) {  // opposite signs
      x2 := x3
    } else {
      x3 := x2
    }
  }
}