« 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
- scan sequence from beginning to end looking for desired target element:
- Binary search
- 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 where 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 and ) 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 is asymptotically less than or equal to () if and only if there exists a natural number such that for all
Conversely, we say is asymptotically greater than or equal to () .
Example
Let and :
- When ,
- Asymptotically, "grows faster" than , so ≥ when
- Given the definition above, we can say that
Big O
An upper bound on a function :
Precise Definition: is big oh of
if and only if there exists a constant
and a natural number
such that
≤
for all
Common Order of Dominance
- exponential
- factorial
- polynomial
- linear
- logarithmic
- constant
Big Ω
A lower bound on a function [1]:
Precise Definition: is big omega of
if and only if there exists a constant
and a natural number
such that
≥
for all
Note:
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.
Precise Definiton: is big theta (same order) of
if and only if there exists constants
and
and a natural number
such that
is between
and
for all
.
In other words,
In this case, for Big-Θ takes the larger value of the 's used in Big-O and Big-Ω.
Examples
Example 1
- Claim
- Choose witnesses and (can be derived mathematically to fit the form of the definition of Big-O: for all )
Example 2
When Joe implements algorithm A in Java and runs it on his home PC. Running time is
When Sue implements algorithm A in Fortran and runs it on dilbert.cs.tamu.edu, the running time is
Resulting speed of both algorithms is
Example 3
Example 4
(See Wikipedia:Binomial coefficient→)
- ↑ comparison-based sorting algorithms need at least 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: (better than )
Numerical analysis: Bisection Method
Where crosses -axis (solve for )
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
}
}
}