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

Đề thi cấu trúc dữ liệu và giải thuật dsa ch2 complexity algorithm

33 5 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 đề Complexity of Algorithms
Tác giả Luong The Nhan, Tran Giang Son
Trường học University of Technology, VNU-HCM
Chuyên ngành Data Structures and Algorithms
Thể loại Lecture Notes
Thành phố Ho Chi Minh City
Định dạng
Số trang 33
Dung lượng 294,63 KB

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

Nội dung

Complexity of Algorithms Complexity of Algorithms Luong The Nhan, Tran Giang Son Algorithm Efficiency Big O notation Problems and common complexities P and NP Problems 2 1 Chapter 2 Complexity of Algo[.]

Trang 1

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Chapter 2

Complexity of Algorithms

Data Structures and Algorithms

Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering

University of Technology, VNU-HCM

Trang 2

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Outcomes

• L.O.1.1 - Define concept “computational complexity”

and its sepcial 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

Trang 3

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 4

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Algorithm Efficiency

Trang 5

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Algorithm Efficiency

⇒ Computational complexity :

Trang 6

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Algorithm Efficiency

General format

efficiency = f(n)

n is the size of a problem (the key

number that determines the size of input

data)

Trang 7

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 8

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Linear Loops

f (n) = n/2

Trang 9

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 10

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Logarithmic Loops

time

f (n) = log 2 n

Trang 11

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 12

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Nested Loops

time f (n) = n log

2 n

Trang 13

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 14

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Dependent Quadratic Loops

Trang 15

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Quadratic Loops

time f (n) = n 2

Trang 16

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Asymptotic Complexity

measurement of an algorithm’s

efficiency.

change the function’s magnitude are

Trang 17

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Big-O notation

Trang 18

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Big-O notation

Example

f (n) = c.n ⇒ f (n) = O(n)

f (n) = n(n + 1)/2 = n 2 /2 + n/2 ⇒ f (n) = O(n 2 )

others.

Some example of Big-O:

Trang 19

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Standard Measures of Efficiency

Efficiency Big-O Iterations Est Time

logarithmic O(log 2 n) 14 microseconds

linear O(n) 10 000 0.1 seconds

linear log O(n log 2 n) 140 000 2 seconds

quadratic O(n 2 ) 10000 2 15-20 min.

polynomial O(n k ) 10000 k hours

exponential O(2 n ) 2 ( 10000) intractable

factorial O(n!) 10000! intractable

Assume instruction speed of 1 microsecond and 10

instructions in loop.

n = 10000

Trang 20

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Standard Measures of Efficiency

Trang 21

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

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

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Big-O Analysis Examples

Nested linear loop:

f (size) = O(size 2 )

Trang 23

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Time Costing Operations

movement to/from memory/storage.

• Comparisons

• Arithmetic operations

• Assignments

Trang 24

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Problems and common

complexities

Trang 25

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Binary search

Recurrence Equation (Phương trình hồi quy)

An equation or inequality that describes a

function in terms of its value on smaller input

Trang 26

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Trang 27

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Sequential search

Trang 28

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

Quick sort

Recurrence Equation

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

Trang 29

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

P and NP Problems

Trang 30

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

P and NP Problems

machine).

(can be solved in polynomial time on

a nondeterministic machine).

Trang 31

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

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 roads between each pair of

cities on the list.

Find the route the salesman should follow for the shortest

possible round trip that both starts and finishes at any one

6

8

9 11

Trang 32

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

P and NP Problems

Travelling Salesman Problem:

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

6

8

9 11

Trang 33

Luong The Nhan,Tran Giang Son

AlgorithmEfficiencyBig-O notationProblems andcommoncomplexities

P and NPProblems

P and NP Problems

NP-complete : NP and every other problem in NP is

polynomially reducible to it.

NP

P

NP-complete

P = NP?

Ngày đăng: 25/03/2023, 08:37

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w