1. Trang chủ
  2. » Tất cả

Cấu trúc dữ liệu và thuật toán ch02 algorithmcomplexity

33 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 33
Dung lượng 0,96 MB

Nội dung

Data Structure and Algorithms [CO2003] Chapter - Algorithm Complexity Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn 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 Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 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: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 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? Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 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: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 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 Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 Linear Loops time f (n) = n f (n) = n/2 n Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 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 Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 Logarithmic Loops time f (n) = log2 n n Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 28 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 Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 28 Standard Measures of Efficiency time n2 n log n n log2 n n Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 28 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

Ngày đăng: 25/03/2023, 07:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w