Algorithms Analysis
Data Structure
Algorithms Analysis
Algorithm
Define: A finite, clearly specified sequence of instructions to be followed to solve a problem.(not a program)
Five features
- Finiteness
- Definiteness
- Effectiveness
- Input
- Output
Design Requirements
- Correctness
- Readability
- Robustness
- High efficiency and low memory capacity
Algorithm Analysis
Computing time and memory space are two important resources.
Running time Analysis
The estimation of the running time of algorithms.
The running times of algorithms can be changed because of the platform, the properties of the computer, etc.
Time complexity
We use asymptotic notations($O,\Omega,\theta$)
- compare relative growth
- compare only algorithms
$f(n) \rightarrow Frequency\ Count$
$Then\ T(n) = O(f(n))$
$if\ T(n) = O(f(n)),then\ T(n) \leq C*f(n), C \in R$
Constants can be ignored!!!
Lower order terms are ignored!!!
Example
|
|
$f(n) = 1 + n + n + 1 = 2n + 2$
$T(n) = O(f(n)) = O(2n + 2) = O(n) \rightarrow Linear\ order$
Time complexity is related with input Example:
|
|
$T(n)\ can\ be\ O(n), but\ also\ can\ O(n^2)$
Relations
$O(1) < O(\log n) < O(n) < O(n\log n) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)$
$if\ T1(n) = O(f(n))\ and\ T2(n) = O(g(n))\ then$
a) $T1(n) + T2(n) = max (O(f(n)), O(g(n)))$
b) $T1(n) * T2(n) = O(f(n)* g(n))$
About Homework
analysis of the running time
|
|
analysis code block
|
|
$so\ the\ step = 1 + 1 + 1 + i + 1 + 1 + … + 2i + 1 + 1 + … + i * i$ $= i^2 + i(1 + 2 + 3 + … + i) = i^2 + i^3 = O(i^3)$
so the code became
|
|
$so\ T(n) = O(n^4)$
Give an efficient algorithm to determine if there exists an >integer i such that $a_i = i$ in an array of integers $a_1< a_2 < a_3< . . . < a_n$. What is the running time of your algorithm?
Traverse:
|
|
$T(n) = O(n)$
Binary Search
|
|
$T(n) = O(\log n)$