Ch02 AlgorithmComplexity PDF
Ch02 AlgorithmComplexity PDF
Contact: [email protected]
1. Algorithm Eciency
2. Big-O notation
4. P and NP Problems
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 28
Outcomes
solutions.
solving.
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 28
Algorithm Eciency
Algorithm Eciency
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 28
Algorithm Eciency
General format
eciency = f(n)
n is the size of a problem (the key number that determines the size of
input data)
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 28
Linear Loops
f(n) = n
f(n) = n/2
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 28
Linear Loops
time f (n) = n
f (n) = n/2
n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 28
Logarithmic Loops
Multiply loops
i = 1
while ( i <= n )
// a p p l i c a t i o n code
i = i x 2
end while
Divide loops
i = n
while ( i >= 1 )
// a p p l i c a t i o n code
i = i / 2
end while
f(n) = log 2 n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 7 / 28
Logarithmic Loops
time
f (n) = log2 n
n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 8 / 28
Nested Loops
Example
i = 1
while ( i <= n )
j = 1
while ( j <= n )
// a p p l i c a t i o n code
j = j * 2
end while
i = i + 1
end while
f (n) = n log2 n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 28
Nested Loops
n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 28
Quadratic Loops
Example
i = 1
while ( i <= n )
j = 1
while ( j <= n )
// a p p l i c a t i o n code
j = j + 1
end while
i = i + 1
end while
f (n) = n2
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 28
Dependent Quadratic Loops
Example
i = 1
while ( i <= n )
j = 1
while ( j <= i )
// a p p l i c a t i o n code
j = j + 1
end while
i = i + 1
end while
1 + 2 + . . . + n = n(n + 1)/2
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 28
Quadratic Loops
time f (n) = n2
n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 28
Asymptotic Complexity
eciency.
eliminated.
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 28
Big-O notation
Big-O notation
Example
f (n) = c.n ⇒ f (n) = O(n)
f (n) = n(n + 1)/2 = n2 /2 + n/2 ⇒ f (n) = O(n2 )
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 28
Standard Measures of Eciency
n = 10000
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 28
Standard Measures of Eciency
time n2 n log n
2
n
log2 n
n
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 28
Big-O Analysis Examples
r = r + 1
end
return matrix3
End addMatrix
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 28
Big-O Analysis Examples
D
Y
f (S) = O( Si )
i=1
f (size) = O(size2 )
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 28
Time Costing Operations
memory/storage.
Comparisons
Arithmetic operations
Assignments
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 20 / 28
Problems and common
complexities
Binary search
Recurrence Equation
An equation or inequality that describes a function in terms of its value
on smaller input.
1 2 3 5 8 13 21 34 55 89
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 28
Binary search
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 28
Sequential search
8 5 21 2 1 13 4 34 7 18
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 28
Quick sort
19 8 3 15 28 10 22 4 12 83
Recurrence Equation
T (n) = O(n) + 2T (n/2)
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 28
P and NP Problems
P and NP Problems
machine).
on a nondeterministic machine).
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 28
P and NP Problems
There are direct roads between each pair of cities on the list.
Find the route the salesman should follow for the shortest possible round
trip that both starts and nishes at any one of the cities.
8
b c
7
9 5
15
a d
6 9
8
11
e f
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 26 / 28
P and NP Problems
8
b c
7
9 5
15
a d
6 9
8
11
e f
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 28
P and NP Problems
reducible to it.
NP
NP-complete
P = NP?
Lecturer: Vuong Ba Thinh Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 28