Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 33 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
33
Dung lượng
214,62 KB
Nội dung
DataStructureandAlgorithms [CO2003] Chapter - AlgorithmComplexity Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn August 22, 2016 Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents Algorithm Efficiency Big-O notation Problems and common complexities P and NP Problems Outcomes • L.O.1.1 - Define concept “computational complexity” and its special cases, best, average, and worst • L.O.1.2 - Analyze algorithmsand 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 Algorithm Efficiency Algorithm Efficiency • A problem often has many algorithms • Comparing two different algorithms ⇒ Computational complexity: measure of the difficulty degree (time and/or space) of an algorithm • How fast an algorithm is? • How much memory does it cost? Algorithm Efficiency General format efficiency = f(n) n is the size of a problem (the key number that determines the size of input data) Linear Loops f o r ( i = ; i < 0 ; i ++) // a p p l i c a t i o n c o d e The number of times the body of the loop is replicated is 1000 f(n) = n f o r ( i = ; i < 0 ; i += ) // a p p l i c a t i o n c o d e The number of times the body of the loop is replicated is 500 f(n) = n/2 Linear Loops time f (n) = n f (n) = n/2 n Logarithmic Loops Multiply loops i = w h i l e ( i = ) // a p p l i c a t i o n c o d e i = i / end w h i l e The number of times the body of the loop is replicated is f(n) = log2 n Logarithmic Loops time f (n) = log2 n n Standard Measures of Efficiency Efficiency logarithmic linear linear log quadratic polynomial exponential factorial Big-O Iterations Est Time O(log2 n) O(n) O(n log2 n) O(n2 ) O(nk ) O(2n ) O(n!) 14 10 000 140 000 100002 10000k 210000 10000! microseconds 0.1 seconds seconds 15-20 hours intractable intractable Assume instruction speed of microsecond and 10 instructions in loop n = 10000 16 Standard Measures of Efficiency time n2 n log n n log2 n n 17 Big-O Analysis Examples Algorithm addMatrix(val matrix1, val matrix2, val size, ref matrix3) 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 r=1 while r