Data Structure and Algorithms (CO2003) : Chapter 2 - Algorithm Complexity
Data Structure and Algorithms (CO2003) : Chapter 2 - Algorithm Complexity
1. Algorithm Efficiency
2. Big-O notation
4. P and NP Problems
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 28
Outcomes
• L.O.1.1 - Define concept “computational complexity” and its special cases, best, average,
and worst.
• L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
complexity of algorithms composed by using the following control structures: sequence,
branching, and iteration (not recursion).
• L.O.1.3 - List, give examples, and compare complexity classes, for examples, constant,
linear, etc.
• L.O.1.4 - Be aware of the trade-off between space and time in solutions.
• L.O.1.5 - Describe strategies in algorithm design and problem solving.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 28
Algorithm Efficiency
Algorithm Efficiency
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 28
Algorithm Efficiency
General format
efficiency = f (n)
n is the size of a problem (the key number that determines the size of input data)
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 28
Linear Loops
f o r ( i = 0 ; i < 1 0 0 0 ; i ++)
// a p p l i c a t i o n c o d e
f (n) = n
f o r ( i = 0 ; i < 1 0 0 0 ; i += 2 )
// a p p l i c a t i o n c o d e
f (n) = n/2
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 28
Linear Loops
time f (n) = n
f (n) = n/2
n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 28
Logarithmic Loops
Multiply loops
i = 1
w h i l e ( i <= n )
// a p p l i c a t i o n c o d e
i = i x 2
end w h i l e
Divide loops
i = n
w h i l e ( i >= 1 )
// a p p l i c a t i o n c o d e
i = i / 2
end w h i l e
time
f (n) = log2 n
n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 8 / 28
Nested Loops
Example
i = 1
w h i l e ( i <= n )
j = 1
w h i l e ( j <= n )
// a p p l i c a t i o n c o d e
j = j ∗ 2
end w h i l e
i = i + 1
end w h i l e
f (n) = n log2 n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 28
Nested Loops
n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 28
Quadratic Loops
Example
i = 1
w h i l e ( i <= n )
j = 1
w h i l e ( j <= n )
// a p p l i c a t i o n c o d e
j = j + 1
end w h i l e
i = i + 1
end w h i l e
f (n) = n2
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 28
Dependent Quadratic Loops
Example
i = 1
w h i l e ( i <= n )
j = 1
w h i l e ( j <= i )
// a p p l i c a t i o n c o d e
j = j + 1
end w h i l e
i = i + 1
end w h i l e
1 + 2 + . . . + n = n(n + 1)/2
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 28
Quadratic Loops
time f (n) = n2
n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 28
Asymptotic Complexity
Lecturer: Duc Dung Nguyen, PhD. 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: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 28
Standard Measures of Efficiency
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 28
Standard Measures of Efficiency
time n2 n log n
2
n
log2 n
n
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 28
Big-O Analysis Examples
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 28
Big-O Analysis Examples
f (size) = O(size2 )
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 28
Time Costing Operations
Lecturer: Duc Dung Nguyen, PhD. 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: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 28
Binary search
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 28
Sequential search
8 5 21 2 1 13 4 34 7 18
Lecturer: Duc Dung Nguyen, PhD. 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: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 28
P and NP Problems
P and NP Problems
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 28
P and NP Problems
8
b c
7
9 5
15
a d
6 9
8
11
e f
Lecturer: Duc Dung Nguyen, PhD. 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: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 28
P and NP Problems
NP
NP-complete
P = NP?
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 28