Cs122 algorithmanalysis, Lecture notes of Data Structures and Algorithms

algorithm analysis tutorial related to computer science Data structure course

Typology: Lecture notes

2014/2015

Uploaded on 12/11/2015

eng_cina_hassan_cina
eng_cina_hassan_cina 🇬🇧

1 document

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm Analysis
CS 122: Data Structures I
Spring 2013
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Cs122 algorithmanalysis and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Algorithm Analysis^ CS 122: Data Structures I

Spring 2013

Reading

•^

Chapter 4 [Goodrich 2011].

-^

Chapter 2 [Weiss 2005]. Spring 2013

Data Structures (1)

Introduction

•^

What to analyze?^ –

Running time.

Space.

-^

For an input of size N, we define:

-^

For an input of size N, we define:^ –

T

avg

(N): the average running time.

T

worst

(N): the worst running time.

T

avg

(N) ≤ T

worst

(N).

•^

For more than one input, T

avg

(N) and T

worst

(N) will

have more than one argument. Spring 2013

Data Structures (1)

Outline

•^

Introduction

•^

Common Functions to Represent Growth Rates.

-^

Asymptotic Notation.

-^

Runtime calculation.

-^

Runtime calculation. Spring 2013

Data Structures (1)

The Constant Function

f(n) = c

•^

c is a fixed constant.^ –

Examples:

c = 5, c = 27, or c = 2

10

•^

Independent of the value of n.

-^

Independent of the value of n.

-^

Use Cases (the number of steps needed to do a basicoperation)^ –

adding two numbers.

assigning a value to some variable.

comparing two numbers.

-^

Appears as: f(n) = 1 and f(n)= c g(n). Spring 2013

Data Structures (1)

The Logarithmic Function

f (n) = log

b^

n, for some constant b > 1

x = log

b^

n if and only if b

x^

= n.

•^

Approximate calculation:

log

a^

n

equals the number of

times

n

is divided by

a

to get a number less than or

equal to

. Examples: -

log

27 = 3, 27 / 3 / 3 / 3 = 1.

log

27 = 3, 27 / 3 / 3 / 3 = 1. 3

log

64 = 3, 64 / 4 / 4 / 4 = 1. 4

log

12 = 4, 12 / 2 / 2 / 2 / 2 = 0.75 ≤ 1. 2

•^

Algorithms: base-2 logarithm appears in algorithmsthat keeps dividing input by 2 in every iteration.

-^

log

c n

is used to denote the function (log

)n c^

(Log

squared, c=2) Spring 2013

Data Structures (1)

The Linear Function

f(n) = n

•^

For an input of size n, a simple basic operation ispreformed for every input value.

-^

Example, comparing a number x to each element of

-^

Example, comparing a number x to each element of an array of size n requires n comparisons.

linear

search.

-^

It represents the best running time that can beachieve for any algorithm that processes a collectionof n objects. Spring 2013

Data Structures (1)

The N-Log-N Function

f(n) = n log n

•^

For an input of size n, the operations performed bythe algorithm are equal to the value of n times the logarithm base 2 of n.logarithm base 2 of n.

-^

A linear algorithm is better than an algorithm thatruns in n log n time.

-^

An n-log-n algorithm is better than a quadraticalgorithm.

-^

Example: merge sort. Spring 2013

Data Structures (1)

Nested Loop and the Quadratic

Function

•^

Consider the following nested loop:^ –

the first iteration of a loop uses one operation,

the second uses two operations,

the third uses three operations,

and so on.

-^

Number of operations: 1+2+3+…+(n-1)+n = n(n+1)/2.

-^

n(n+1)/2 = n

2

/2 + n/

n

2

, quadratic.

Spring 2013

Data Structures (1)

The Cubic Function

f(n) = n

3

•^

For an input value n, the function f assigns theproduct of n with itself three times (“n cubed”).

-^

Polynomials

: f (n) = a

+a 0

n+a 1

n 2

2 +a

n 3

3

ad

n

d

•^

Polynomials

: f (n) = a

+a 0

n+a 1

n 2

+a

n 3

ad

n

a

, a 0

, a 1

, a 2

, ···, a 3

are the coefficients.d

a

d^

≠ 0.

d is the highest power in the polynomial



degree of the polynomial.

Spring 2013

Data Structures (1)

Comparing Growth Rates of

Functions

Spring 2013

Data Structures (1)

The Ceiling and Floor Functions

•^

The value of a logarithm is typically not an integer.

-^

The running time of an algorithm is usually expressedby means of an integer quantity, such as the number of operations performed.of operations performed.

-^

The analysis of an algorithm involves using thefollowing functions:^ –

Floor:

the largest integer less than or equal to x (≤ x).

Ceiling:

the smallest integer greater than or equal to x

(≥ x).

Spring 2013

Data Structures (1)

^ xx^ ^ 

Asymptotic Complexity

-^

In algorithm analysis, we focus on the growth rate of therunning time as a function of the input size n.

-^

We are more concerned about large values of n.

-^

The calculated function of the complexity gives an approximate measure of efficiency. This is why we call itapproximate measure of efficiency. This is why we call it asymptotic complexity.

-^

Example: Spring 2013

Data Structures (1)

f(n) = n

2 + 100n+ log

n^

  • 1000

n<

1000: largest contribution.

n=

1000 , 100n: equal contribution.

n=

(^2) n and 100n: equal contribution..

n>10 0

(^2) n : highest contribution.

Big-Oh

•^

Given: functions f(n) and g(n), a constant c > 0, andn

0

•^

f(n) = O(g(n))

f(n) ≤ c g(n) for all n ≥

n

0

f(n) ≤ c g(n) for all n ≥

n

0

•^

Example: f(n) = 1000n, g(n) = n

1000n ≤ n

2 , n ≥ 1000.

Spring 2013

Data Structures (1)