CSCE 121 Algorithm
« 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 :
Common Order of Dominance
- exponential
- factorial
- polynomial
- linear
- logarithmic
- constant
Big Ω
A lower bound on a function [1]:
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.
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 Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O\left(n\right)}
Example 3
Example 4
(See Wikipedia:Binomial coefficient→)
Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} \binom{n}{2}&=\frac{n\left(n-1\right)}{2}\\ &=\frac{n^2}{2}-\frac{n}{2}\\ &=\frac{n^2}{2}+O\left(n\right)\\ &=O\left(n^2\right) \end{align}}
Footnotes
- ↑ comparison-based sorting algorithms need at least Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \Omega(n\log{n})} comparisons.
Example: Sort
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: Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O\left(n\log{n}\right)} (better than Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle O\left(n^2\right)} )
Numerical analysis: Bisection Method
Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sqrt{k}=?}
Where Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f\left(x\right)=x^2-k} crosses Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} -axis (solve for Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f\left(x\right)=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
}
}
}