Escolar Documentos
Profissional Documentos
Cultura Documentos
Review essential math Big O, Big Omega, Big Theta Analyzing algorithms
Based on: Data Structures and Algorithms by Clifford A. Shaffer (Virgina Tech Univ.)
Wednesday, June 20, 2012 1
More complex applications demand more calculations. Complex computing tasks are unlike our everyday experience.
Organizing Data
Any organization for a collection of objects can be searched, processed in any order, or modied. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
Efciency
A solution is said to be efcient if it solves the problem within its resource constraints of:
Space (amount of memory, RAM, used) Time
The cost of a solution is the amount of resources that the solution consumes.
Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-dened order, or is random access allowed?
Mathematical Background
Set concepts and notation Logarithms Recursion Induction Proofs Summations Recurrence Relations
10
Set
11
Logarithms
Denition: The logarithm is the inverse function of the function that raises a base to an exponent
Example: The inverse function of y=2x is x=log2 y
12
Summation
13
Mathematical Proof
14
Algorithm Efciency
There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program design are two (sometimes conicting) goals.
1. To design an algorithm that is easy to understand, code, debug. 2. To design an algorithm that makes efcient use of the computers resources.
15
16
Run program many times, on different inputs, measure the amount of time taken
for (int i=1; i<A.length; i++) if (A[indexOfLarge] < A[i]) indexOfLarge = i; // Remember pos return indexOfLarge; } // Return largest pos
18
Examples (cont)
Example 2: The assignment statement. Example 3: sum = 0; for (i=1; i<=n; i++) for (j=1; j<n; j++) sum++; }
19
21
While average time appears to be the fairest measure, it may be difcult to determine. When is the worst case time important?
22
23
25
Big-Oh Examples
Example 1: Finding value X in an array (average cost). Use Linear Search algorithm Asymptotic Analysis:
Then T(n) = csn/2. For all values of n > 1, csn/2 <= csn. Therefore, the denition is satised for f(n)=n, n0 = 1, and c = cs. Hence, T(n) is in O(n).
26
Big-Oh Examples
Example 2:
Suppose T(n) = c1n2 + c2n, where c1 and c2 are positive. c1n2 + c2n <= c1n2 + c2n2 <= (c1 + c2)n2 for all n > 1. Then T(n) <= cn2 whenever n > n0, for c = c1 + c2 and n0=1.
27
A Common Misunderstanding
The best case for my algorithm is n=1 because that is the fastest. WRONG! Big-oh refers to a growth rate as n grows to . Best case is dened for the input of size n that is cheapest among all inputs of size n.
28
Big-Omega
Denition: For T(n) a non-negatively valued function, T(n) is in the set (g(n)) if there exist two positive constants c and n0 such that T(n) >= cg(n) for all n > n0. Meaning: For all data sets big enough (i.e., n > n0), the algorithm always requires more than cg(n) steps.
Lower bound.
Wednesday, June 20, 2012 29
Big-Omega Example
T(n) = c1n2 + c2n. c1n2 + c1n >= c1n2 for all n > 1. T(n) >= cn2 for c = c1 and n0 = 1. Therefore, T(n) is in (n2) by the denition. We want the greatest lower bound.
30
Theta Notation
When big-Oh and coincide, we indicate this by using (big-Theta) notation. Denition: An algorithm is said to be in (h(n)) if it is in O(h(n)) and it is in (h(n)).
31
A Common Misunderstanding
Confusing worst case with upper bound. Upper bound refers to a growth rate. Worst case refers to the worst input from among the choices for possible inputs of a given size.
32
33
34
36
if statement
Take greater complexity of then/else clauses.
switch statement
Take complexity of most expensive case.
37
Binary Search
Search for an element with value k in sorted array How many elements are examined...
in the best case? worst case? average case?
38