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

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

46 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ả Dr. Nguyen Ho Man Rang
Người hướng dẫn Professor Olga Veksler
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 46
Dung lượng 399,56 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 Dr Nguyen Ho Man Rang Algorithm Efficiency Asymptotic Analysis Problems and common complexities P and NP Problems 2 1 Chapter 2 Complexity of Algorithms Data Structures and Al[.]

Trang 1

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Chapter 2

Complexity of Algorithms

Data Structures and Algorithms

Dr Nguyen Ho Man Rang Faculty of Computer Science and Engineering

University of Technology, VNU-HCM

Trang 2

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems 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

problem solving.

Trang 3

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 4

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Sources of Materials

This materials reused some slides from the following source:

• course CS2210 Data Structures and Algorithms

instructed by Professor Olga Veksler Department of

Computer Science, University of Western Ontario.

Trang 5

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Algorithm Efficiency

Trang 6

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Compare Algorithms

• Given 2 or more algorithms to solve the

same problem, how do we select the best

one?

• Some criteria for selecting an algorithm

• Is it easy to implement, understand, modify?

• How long does it take to run it to completion?

• How much of computer memory does it use?

• Software engineering is primarily concerned

with the first criteria

• In this course we are interested in the

second and third criteria

Trang 7

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Algorithm Efficiency

that an algorithm needs to run to

completion.

an algorithm needs to run.

• We will occasionally look at space

complexity, but we are mostly interested in

time complexity in this course.

• Thus in this course the better algorithm is

the one which runs faster (has smaller time

Trang 8

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

How to Calculate Running time

• Most algorithms transform input objects

into output objects.

sorting algorithm

input object output object

• The running time of an algorithm typically

grows with the input size f (n).

0 1000 2000 3000 4000 5000 6000 7000 8000 9000

Trang 9

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

How to Calculate Running Time

running time can be very different.

• Example: algorithm that searches an array

containing n integers to find the one with a

particular value K (assume that K appears

exactly once in the array)

• best case

• worst case

• average case

Trang 10

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

How to Calculate Running Time

useless

often difficult to determine

time

• Easier to analyze

• Crucial to applications such as games,

finance and robotics

Trang 11

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Evaluations of Running Time

running time between agorithms:

• Experimental evaluation

• Theoretical evaluation

Trang 12

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Experimental Evaluation of Running Time

implementing the

algorithm

inputs of varying size

and composition

actual running time

0 1000 2000 3000 4000 5000 6000 7000 8000 9000

Trang 13

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Limitations of Experiments

• It is necessary to implement the algorithm,

which may be difficult

• Results may not be indicative of the

running time on other inputs not included

in the experiment

• In order to compare two algorithms, the

same hardware and software environments

must be used

Trang 14

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Theoretical Analysis of Running Time

algorithm instead of an

implementation

function of the input size, n

algorithm independent of the

hardware/software environment

Trang 15

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Primitive Operations

primitive or basic operations, which

are simple computations performed by

an algorithm

• Evaluating an expression: x 2 + 3x

• Assigning a value to a variable: x = y

• Indexing into an array: a[10]

Trang 16

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Counting Primitive Operations

• By inspecting the pseudocode, we can determine the

maximum number of primitive operations executed by

an algorithm, as a function of the input size

Trang 17

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Counting Primitive Operations

• By inspecting the pseudocode, we can determine the

maximum number of primitive operations executed by

an algorithm, as a function of the input size

Trang 18

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Estimating Running Time

primitive operations in the worst case.

two linear functions

Trang 19

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Growth Rate of Running Time

environment

• Thus we focus on the big-picture which is

the growth rate of an algorithm

• The linear growth rate of the running time

f (n) is an intrinsic property of algorithm

findMax()

Trang 20

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 21

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 22

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 23

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 24

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Dependent Quadratic Loops

Trang 25

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

factors to focus on the essential part

of the running time?

Trang 26

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Asymptotic Analysis

measurement of an algorithm’s

efficiency.

change the function’s magnitude are

eliminated

Trang 27

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Asymptotic Analysis

Trang 28

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 29

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Big-Oh notation

characterize running times and space

bounds

ignore constant factors and lower

order terms and focus on the main

components of a function which

affect its growth

Trang 30

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Big-Oh notation

say that f (n) is O(g(n)) if there are

• Why?

Trang 31

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Big-Oh Rules

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:

1 log 2 n n n log 2 n n 2 n k 2 n

Trang 32

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Big-Oh and Growth Rate

bound on the growth rate of a

function

means that the growth rate of f (n) is

no more than the growth rate of g(n)

rank functions according to their

growth rate

Trang 33

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Standard Measures of Efficiency

Assume instruction speed of 1 microsecond and 10

instructions in loop.

n = 10000

Trang 34

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Standard Measures of Efficiency

Trang 35

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Relatives of Big-Oh

• f (n) is Ω(g(n)) if there is a constant c > 0 and

• f (n) is Θ(g(n)) if there are constants c 0 > 0

and c 00 > 0 and an integer constant n 0 ≥ 1

such that c 0 g(n) ≥ f (n) ≥ c 00 g(n) for n ≥ n 0

Trang 36

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Intuition for Asymptotic Notation

Trang 37

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Problems and common

complexities

Trang 38

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems 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 39

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Trang 40

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Sequential search

Trang 41

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

Quick sort

Recurrence Equation

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

• Best case: T (n) = O(n log 2 n)

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

Trang 42

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

P and NP Problems

Trang 43

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems andcommoncomplexities

P and NPProblems

P and NP Problems

machine).

(can be solved in polynomial time on

a nondeterministic machine).

Trang 44

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems 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 45

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems 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 46

Dr Nguyen HoMan Rang

AlgorithmEfficiencyAsymptoticAnalysisProblems 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:38

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w