Top Banner
Complexity analysis
24

Complexity Theory Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Complexity analysis

Page 2: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Complexity Theory

Page 3: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Complexity theory is a problem can be solved? Given:◦ Limited resources: processor time and memory

space

Complexity Theory

Page 4: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Algorithm

Page 5: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Definition: An algorithm is a finite step-by-step list of well-defined instructions for solving a particular problem.

Algorithm complexity: is a key task in comparing the efficiency of algorithms.

Two factors are considered:◦ 1- the (running) time Time Complexity◦ 2- the space (usage) Space Complexity

Algorithm

Page 6: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Time Complexity: estimates the running time of the algorithm.◦ Measured by the number of key step of the

basic steps used by the algorithm. Basic Steps are:

◦ Arithmetic operations (+, -, *, /)◦ Comparison operations (>, >=, <, <=, ==)◦ Reading from a memory, file◦ Writing to memory, file, (cin, fin)

Time Complexity

Page 7: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Key step: The most costly step of the algorithm◦ Takes most of the running time◦ Other steps is much less or at most proportional

to the time of the key step.

Key step

Page 8: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

In worse-case analysis:◦We find the maximum value of the

number of the key steps.

In Average-case analysis:◦We find the average value of the number

of the key steps.

Worse-Case & Average-Case Analysis

Page 9: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Let f (n) the complexity of an algorithm, i.e.,◦ # key operations◦ For an instance of size n

A computer processor has a speed of 1 MIP◦ Million Instructions Per (MIP) second

Growth Rate

Page 10: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Growth Rate (Cont.)

Page 11: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Big Oh O() Notation is used◦ To estimate the growth rate of f : N R+

◦ f ϵ O(g)◦ There exist two positive integer k, n0 :

f(n) ≤ k*g(n), for all n ≥ n0

f grows not fast than g f belongs to the complexity class O(g)

Big Oh O( ) Notation

Page 12: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

f(n) = 3 n+2= O(n) as 3 n +2 ≤ 4 n for all n ≥ 2.

f(n) =100 n+6=O(n) as 100 n+6 ≤ 101 n for all n ≥ 6.

f(n) =1000 n2 + 100 n - 6 = Ο(n2) as 1000 n2 + 100 n - 6 ≤ 1001 n2 for all n ≥ 100.

f(n) = 6 2n+ n2 = Ο (2n) as 6 2n+ n2 ≤ 7 * 2n for all n ≥ 4.

Big Oh O ( ) Examples

Page 13: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Find the class complexity of the following functions

1. f(n) = n3 + 23n2 - 8n + 6 2. f(n) = (6n7+4) / (n2+1) 3. f(n) = 2n + 3n5 + 34n4

Let’s practice!

Page 14: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Common Complexity Classes

Page 15: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Common complexity classes

O(1)< O(log n) < O(n)< O(n log n)< O(n2)<O(n3)< O(2n)< O(10n)

O(n log n) is better than O(n2) O(n) is better than O(n log n)

Class Complexity Hierarchy

Page 16: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Dropping Coefficient◦f(n) = k * nc f ϵ O(nc)◦f(n) = 10 n4 f ϵ O(n4)

Dominant Term◦f(n) = nc+nc-1+…+ n2 +n+1 f ϵ O(nc)◦f(n) = (n8 + 1) / (n3 + 2n2 – 1) f ϵ O(n5)

Manipulating Big Oh O() Notation

Page 17: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Introduction to Algorithms

Page 18: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

What is Algorithm ??

Page 19: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

An algorithm: is a finite step-by-step list of well-defined instructions for solving a particular problem.

Algorithm complexity is a key task in comparing the efficiency of algorithms.

Two factors are considered:◦ 1- the (running) time Time Complexity◦ 2- the space (usage) Space Complexity

Algorithm

Page 20: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Real life algorithm

Page 21: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Sorting Algorithms◦ Bubble Sort◦ Insertion Sort◦ And many other algorithms (Quick Sort, Selection Sort, ….

)

Searching Algorithms◦ Linear search (sequential search)◦ Binary search◦ And many other algorithms (BFS, DFS, … )

And There are many and many algorithms used in this fields and other fields.

Examples of algorithms

Page 22: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Sorting Algorithms

Bubble sort

Page 23: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.

Insertion Sort

Page 24: Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.