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

Tiêu đề Algorithm Complexity
Người hướng dẫn Duc Dung Nguyen, PhD
Trường học Hochiminh City University of Technology
Chuyên ngành Data Structure and Algorithms
Thể loại Chương
Thành phố Ho Chi Minh City
Định dạng
Số trang 33
Dung lượng 0,96 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Data 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 4

Algorithm Efficiency

Trang 5

Algorithm 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 8

Linear Loops

f (n) = n/2

Trang 10

Logarithmic Loops

time

f (n) = log2n

Trang 12

Nested Loops

time f (n) = n log

2n

Trang 14

Dependent Quadratic Loops

Trang 15

Quadratic Loops

time

n

f (n) = n2

Trang 16

Asymptotic 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 17

Big-O notation

Trang 18

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)

• Set thecoefficient of the termto one

• Keep the largest termand discard the others

Some example of Big-O:

Trang 19

Standard 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 20

Standard Measures of Efficiency

Trang 21

Big-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 22

Big-O Analysis Examples

Nested linear loop:

f (size) = O(size2)

Trang 23

Time Costing Operations

• The most time consuming: data movementto/from memory/storage

• Operations under consideration:

• Comparisons

• Arithmetic operations

• Assignments

Trang 24

Problems and common complexities

Trang 26

Binary 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 27

Sequential search

• Best case: T (n) = O(1)

• Worst case: T (n) = O(n)

Trang 28

Quick sort

Recurrence Equation

T (n) = O(n) + 2T (n/2)

• Best case: T (n) = O(n log2n)

• Worst case: T (n) = O(n2)

Trang 29

P and NP Problems

Trang 30

P 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 31

P 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 32

P and NP Problems

Travelling Salesman Problem:

Deterministic machine: f (n) = n(n − 1)(n − 2) 1 = O(n!)

6

8

9

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