1. Trang chủ
  2. » Giáo án - Bài giảng

Data Structure and Algorithms CO2003 Chapter 2 Algorithm Complexity

33 363 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 214,62 KB

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.2 - Analyze algorithms and use Big-O notation to characterize the computat

Trang 1

Data Structure and Algorithms [CO2003]

Chapter 2 - Algorithm Complexity

Lecturer: Duc Dung Nguyen, PhD

Contact: nddung@hcmut.edu.vn

August 22, 2016

Faculty of Computer Science and Engineering

Hochiminh city University of Technology

Trang 3

• L.O.1.1 - Define concept “computational complexity” and its specialcases, 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, anditeration (not recursion)

• L.O.1.3 - List, give examples, and compare complexity classes, forexamples, 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 the difficulty 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 onlybig problem sizes

• We arenot concernedwith anexact measurementof an algorithm’sefficiency

• Terms that donot substantially change the function’s magnitude are

eliminated

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 thecoefficientof 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 Time

linear log O(n log2n) 140 000 2 seconds

exponential O(2n) 210000 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(valmatrix1<matrix>, valmatrix2<matrix>, val

size<integer>, refmatrix3<matrix>)

Addmatrix1tomatrix2and place results inmatrix3

Pre:matrix1andmatrix2have data

sizeis number of columns and rows in matrix

Post: matrices added - result inmatrix3

Trang 22

Big-O Analysis Examples

Nested linear loop:

f (S) = O(

DY

i=1Si)

f (size) = O(size2)

Trang 23

Time Costing Operations

• The most time consuming: data movementto/from

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)

• Average case: T (n) =Pni=1i.pi

pi : probability for the target being at a[i]

pi= 1/n ⇒ T (n) = (Pni=1i)/n = O(n(n + 1)/2n) = 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 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 roads between each pair of cities on the list

Find the route the salesman should follow for the shortest possible roundtrip that both starts and 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!)

Ngày đăng: 29/03/2017, 18:21

TỪ KHÓA LIÊN QUAN