Complexity Analysis: Data Structure and Algorithm in Java
Complexity Analysis: Data Structure and Algorithm in Java
Complexity Analysis: Data Structure and Algorithm in Java
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Algorithms Definition
3
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Algorithm Properties
4
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Representations of Algorithms
5
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Representations of Algorithms
6
Examples – nature language
find max(a,b,c)
1. Assign x = a
2. If b great than x then assign x=b
3. If c great than x then assign x=c;
4. Result: x => max(a,b,c)
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Representations of Algorithms
7
Examples - alias programming language
Algorithm Max(a,b,c)
Input: int a, b, c
Output: Max(a,b,c)
1. x = a;
2. if b > x then x = b;
3. if c > x then x = c;
4. return x;
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Representations of Algorithms
8
Examples - Diagram
begin x := a
True
b>x x := b
False
c>x
True True
end x := c
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Complexity analysis
9
Need of algorithm analysis
A problem can be solved by various algorithms
with different efficiencies.
Example:
Problem of sorting n of integer number. Suppose:
Executing on the same computer, time per one
operation is 1/1000000 s
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Complexity analysis
10
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Complexity analysis
11 Calculate the execution time of a program in Java
import java.util.Calendar;
public class GioHeThong {
public static void main(String[] args) {
long beginTimes =
Calendar.getInstance().getTimeInMillis();
long a;
long n = 10000;
for (long i=0; i<n;++i)
for(long j =0; j<n; ++j)
a = 1000;
long endTimes =
Calendar.getInstance().getTimeInMillis();
System.out.println("The times in ms for run the
program are:");
System.out.println(endTimes - beginTimes);
}
}
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Complexity analysis
12
Categories of analysis of algorithm
Precision:
Proved by mathematics
Implementation and test
Simple and public
Effective:
Run time
Space (in memory)
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Complexity analysis
13
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Computational and
Asymptotic Complexity (continued)
16
Figure 2-1 The growth rate of all terms of function f (n) = n2 + 100n +
log10n + 1,000
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Big-O Notation
17
Figure 2-2 Different values of c and N for function f (n) = 2n2 + 3n + 1 = O(n2)
calculated according to the definition of big-O
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Big-O Notation (continued)
18
Fact 1 (transitivity)
If f (n) is O(g(n)) and g(n) is O(h(n)), then f(n) is
O(h(n))
Fact 2
If f (n) is O(h(n)) and g(n) is O(h(n)), then f(n) + g(n)
is O(h(n))
Fact 3
The function ank is O(nk)
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Properties of Big-O Notation
20
Fact 4
The function nk is O(nk+j) for any positive j
Fact 5
If f(n) = cg(n), then f(n) is O(g(n))
Fact 6
The function loga n is O(logb n) for any positive
numbers a and b ≠ 1
Fact 7
loga n is O(lg n) for any positive a ≠ 1, where lg n
= log2 n
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Ω and Θ Notations
21
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Ω and Θ Notations (continued)
22
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Ω and Θ Notations (continued)
23
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Examples of Complexities
24
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Examples of Complexities
25
=> O(n2)
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Finding Asymptotic Complexity
30
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Finding Asymptotic Complexity
31
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Finding Asymptotic Complexity
32
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
The Best, Average, and Worst Cases
33
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Amortized Complexity
34
Amortized analysis:
Analyzes sequences of operations
Can be used to find the average complexity of a worst
case sequence of operations
By analyzing sequences of operations rather than
isolated operations, amortized analysis takes into
account interdependence between operations and
their results
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Amortized Complexity (continued)
35
Worst case:
C(op1, op2, op3, . . .) = Cworst(op1) + Cworst(op2) + Cworst(op3) + . . .
Average case:
C(op1, op2, op3, . . .) = Cavg(op1) + Cavg(op2) + Cavg(op3) + . . .
Amortized:
C(op1, op2, op3, . . .) = C(op1) + C(op2) + C(op3) + . . .
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
NP-Completeness
36
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
NP-Completeness (continued)
37
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Summary (continued)
39
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
Summary (continued)
40
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java
References and Readings
41
________________________________________________________________________________
CS103 - Data Structure and Algorithm in Java