Data Structure and Algorithms [CO2003]Chapter 2 - Algorithm Complexity Lecturer: Duc Dung Nguyen, PhD.. • L.O.1.3 - List, give examples, and compare complexity classes, for examples, con
Trang 1Data Structure and Algorithms [CO2003]
Chapter 2 - Algorithm Complexity
Lecturer: Duc Dung Nguyen, PhD
Contact: nddung@hcmut.edu.vn
Faculty of Computer Science and Engineering
Hochiminh city University of Technology
Trang 3• 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
Trang 4Algorithm Efficiency
Trang 5Algorithm Efficiency
• A problem often has many algorithms
• Comparing two different algorithms ⇒ Computational complexity: measure of thedifficulty 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 only big problem sizes
• We arenot concernedwith anexact measurementof an algorithm’s efficiency
• Terms that donot substantially change the function’s magnitude areeliminated
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 thecoefficient of 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 Timelogarithmic O(log2n) 14 microseconds
linear O(n) 10 000 0.1 seconds
linear log O(n log2n) 140 000 2 seconds
quadratic O(n2) 100002 15-20 min
polynomial O(nk) 10000k hours
exponential O(2n) 210000 intractable
factorial O(n!) 10000! 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(val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>) Add matrix1 to matrix2 and place results in matrix3
Pre: matrix1 and matrix2 have data
size is number of columns and rows in matrix
Post: matrices added - result in matrix3
Trang 22Big-O Analysis Examples
Nested linear loop:
f (size) = O(size2)
Trang 23Time Costing Operations
• The most time consuming: data movementto/from memory/storage
• Operations under consideration:
• Comparisons
• Arithmetic operations
• Assignments
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)
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 30P and NP Problems
• P: Polynomial (can be solved in polynomial time on adeterministicmachine)
• NP: Nondeterministic Polynomial (can be solved in polynomial time on anondeterministic
machine)
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 roadsbetween each pair of cities on the list
Find the route the salesman should follow for the shortest possible round trip that both startsand 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!)
6
8
9