Data Structure and Algorithms [CO2003]Chapter 2 - Algorithm Complexity Lecturer: Duc Dung Nguyen, PhD.. • L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computat
Trang 1Data Structure and Algorithms [CO2003]
Chapter 2 - Algorithm Complexity
Lecturer: Duc Dung Nguyen, PhD
Contact: nddung@hcmut.edu.vn
August 22, 2016
Faculty of Computer Science and Engineering
Hochiminh city University of Technology
Trang 3• L.O.1.1 - Define concept “computational complexity” and its specialcases, 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, anditeration (not recursion)
• L.O.1.3 - List, give examples, and compare complexity classes, forexamples, 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
Trang 4Algorithm Efficiency
Trang 5Algorithm Efficiency
• A problem often has many algorithms
• Comparing two different algorithms ⇒Computational complexity:measure of the difficulty degree (timeand/orspace) of an algorithm
• Howfastan algorithm is?
• How muchmemorydoes it cost?
Trang 8Linear Loops
f (n) = n/2
Trang 10Logarithmic Loops
time
f (n) = log2n
Trang 12Nested Loops
time f (n) = n log
2n
Trang 14Dependent Quadratic Loops
Trang 15Quadratic Loops
time
n
f (n) = n2
Trang 16Asymptotic Complexity
• Algorithm efficiency is considered with onlybig problem sizes
• We arenot concernedwith anexact measurementof an algorithm’sefficiency
• Terms that donot substantially change the function’s magnitude are
eliminated
Trang 17Big-O notation
Trang 18Big-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)
• Set thecoefficientof the termto one
• Keep the largest termand discard the others
Some example of Big-O:
Trang 19Standard Measures of Efficiency
Efficiency Big-O Iterations Est Time
linear log O(n log2n) 140 000 2 seconds
exponential O(2n) 210000 intractable
Assume instruction speed of 1 microsecond and 10 instructions in loop
n = 10000
Trang 20Standard Measures of Efficiency
Trang 21Big-O Analysis Examples
Algorithm addMatrix(valmatrix1<matrix>, valmatrix2<matrix>, val
size<integer>, refmatrix3<matrix>)
Addmatrix1tomatrix2and place results inmatrix3
Pre:matrix1andmatrix2have data
sizeis number of columns and rows in matrix
Post: matrices added - result inmatrix3
Trang 22Big-O Analysis Examples
Nested linear loop:
f (S) = O(
DY
i=1Si)
f (size) = O(size2)
Trang 23Time Costing Operations
• The most time consuming: data movementto/from
Trang 24Problems and common complexities
Trang 26Binary search
• Best case: when the number of steps is smallest T (n) = O(1)
• Worst case: when the number of steps is largest T (n) = O(log2n)
• Average case: in between T (n) = O(log2n)
Trang 27Sequential search
• Best case: T (n) = O(1)
• Worst case: T (n) = O(n)
• Average case: T (n) =Pni=1i.pi
pi : probability for the target being at a[i]
pi= 1/n ⇒ T (n) = (Pni=1i)/n = O(n(n + 1)/2n) = O(n)
Trang 28Quick sort
Recurrence Equation
T (n) = O(n) + 2T (n/2)
• Best case: T (n) = O(n log2n)
• Worst case: T (n) = O(n2)
Trang 29P and NP Problems
Trang 31P and NP Problems
Travelling Salesman Problem:
A salesman has a list of cities, each of which he must visit exactly once.There are direct roads between each pair of cities on the list
Find the route the salesman should follow for the shortest possible roundtrip that both starts and finishes at any one of the cities
6
8
911
Trang 32P and NP Problems
Travelling Salesman Problem:
Deterministic machine: f (n) = n(n − 1)(n − 2) 1 = O(n!)