basics & asymptotic notations

33
1 Basic Terminologies & Basic Terminologies & Asymptotic Notations Asymptotic Notations Data Structures Data Structures

Upload: rajendranjrf

Post on 17-Aug-2015

30 views

Category:

Education


0 download

TRANSCRIPT

1

Basic Terminologies & Basic Terminologies & Asymptotic NotationsAsymptotic Notations

Data StructuresData Structures

2

Abstract Data Type (ADT)Abstract Data Type (ADT)• Mathematical description of an object Mathematical description of an object

with set of operations on the object. with set of operations on the object. Useful building block.Useful building block.

AlgorithmAlgorithm• A high level, language independent, A high level, language independent,

description of a step-by-step processdescription of a step-by-step process Data structureData structure

• A specific family of algorithms for A specific family of algorithms for implementing an abstract data type.implementing an abstract data type.

Implementation of data structureImplementation of data structure• A specific implementation in a specific A specific implementation in a specific

languagelanguage

TerminologyTerminology

3

TerminologyTerminology

DataData

Data refers to value or set of values. Data refers to value or set of values.

e.g.Marks obtained by the students.e.g.Marks obtained by the students.

Data typeData type

data type is a classification identifying one of various data type is a classification identifying one of various typestypes

of data, such as floating-point, integer, or Boolean, of data, such as floating-point, integer, or Boolean, that that

determines the possible values for that type; the determines the possible values for that type; the operations operations

that can be done on values of that type; and the way that can be done on values of that type; and the way valuesvalues

of that type can be storedof that type can be stored

4

TerminologyTerminology

Primitive data type:Primitive data type:

These are basic data types that are provided by theThese are basic data types that are provided by the

programming language with built-in support. These programming language with built-in support. These datadata

types are native to the language. This data type istypes are native to the language. This data type is

supported by machine directlysupported by machine directly

VariableVariable

Variable is a symbolic name given to some known Variable is a symbolic name given to some known or or

unknown quantity or information, for the purpose unknown quantity or information, for the purpose of of

allowing the name to be used independently of the allowing the name to be used independently of the

information it represents.information it represents.

5

RecordRecord

Collection of related data items is known as record. Collection of related data items is known as record. The The

elements of records are usually Called fields or elements of records are usually Called fields or members .members .

Records are distinguished from arrays by the fact Records are distinguished from arrays by the fact that that

their number of fields is typically fixed, each field their number of fields is typically fixed, each field has a has a

name, and that each field may have a different type.name, and that each field may have a different type. ProgramProgram

A sequence of instructions that a computer can A sequence of instructions that a computer can

interpret and execute.interpret and execute.

TerminologyTerminology

6

AA stack is an stack is an abstract data type abstract data type supporting push, pop and isEmpty supporting push, pop and isEmpty operationsoperations

A stack A stack data structuredata structure could use an could use an array, a linked list, or anything that can array, a linked list, or anything that can hold datahold data

One stack One stack implementationimplementation is is java.util.Stack; another is java.util.Stack; another is java.util.LinkedListjava.util.LinkedList

Terminology examplesTerminology examples

7

AbstractAbstract PseudocodePseudocode AlgorithmAlgorithm

• A sequence of A sequence of high-level, high-level, language language independent independent operations, which operations, which may act upon an may act upon an abstracted view of abstracted view of data.data.

Abstract Data Type Abstract Data Type (ADT)(ADT)• A mathematical A mathematical

description of an description of an object and the set object and the set of operations on of operations on the object.the object.

AbstractAbstract PseudocodePseudocode AlgorithmAlgorithm

• A sequence of A sequence of high-level, high-level, language language independent independent operations, which operations, which may act upon an may act upon an abstracted view of abstracted view of data.data.

Abstract Data Type Abstract Data Type (ADT)(ADT)• A mathematical A mathematical

description of an description of an object and the set object and the set of operations on of operations on the object.the object.

ConceptsConcepts vs. vs. MechanismsMechanisms

Design and Analysis of Algorithms Chapter 2.28

Data StructuresData Structures

Asymptotic AnalysisAsymptotic Analysis

Design and Analysis of Algorithms Chapter 2.29

Review: algorithm efficiency Review: algorithm efficiency indicatorindicator

order of growth of

an algorithm’s basic operation count

the algorithm’s time efficiency

Design and Analysis of Algorithms Chapter 2.210

Asymptotic growth rateAsymptotic growth rate

A way of comparing functions that ignores constant factors A way of comparing functions that ignores constant factors and small input sizesand small input sizes

O(O(gg((nn)): class of functions )): class of functions t(n)t(n) that grow that grow no fasterno faster than than gg((nn))

Θ Θ ((gg((nn)): class of functions )): class of functions t(n)t(n) that grow that grow at same rateat same rate as as gg((nn))

ΩΩ((gg((nn)): class of functions )): class of functions t(n)t(n) that grow that grow at least as fastat least as fast as as gg((nn))

Design and Analysis of Algorithms Chapter 2.211

Big-ohBig-oh

c > 0, n0 0 , n n0, t(n) cg(n)

t(n) O(g(n))

Design and Analysis of Algorithms Chapter 2.212

Small-ohSmall-oh

c > 0, n0 0 , n n0, t(n) < cg(n)

t(n) o(g(n))

Design and Analysis of Algorithms Chapter 2.213

Big-omegaBig-omega

t(n) (g(n))

Design and Analysis of Algorithms Chapter 2.214

Big-omegaBig-omega

c > 0, n0 0 , n n0, t(n) cg(n)

t(n) (g(n))

Design and Analysis of Algorithms Chapter 2.215

Small-omegaSmall-omega

c > 0, n0 0 , n n0, t(n) > cg(n)

t(n) (g(n))

Design and Analysis of Algorithms Chapter 2.216

Big-thetaBig-theta

t(n) (g(n))

c1>c2>0, n00, n n0, c2g(n) t(n) c1g(n)

Design and Analysis of Algorithms Chapter 2.217

Big thetaBig theta

The reverse statement of

t(n) (g(n))

c1>c2>0, n00, n n0, c2g(n) t(n) c1g(n)

Design and Analysis of Algorithms Chapter 2.218

Big thetaBig theta

t(n) (g(n))

c1>c2>0, n00, n n0, t(n) < c2g(n) or t(n) > c1g(n)

Design and Analysis of Algorithms Chapter 2.219

Establishing rate of growth: Method 1 – using Establishing rate of growth: Method 1 – using definitiondefinition

t(n)t(n) is O( is O(gg((nn)) if order of growth of )) if order of growth of t(n)t(n) ≤ order of growth ≤ order of growth of of gg((nn) (within constant multiple)) (within constant multiple)

There exist positive constant There exist positive constant cc and non-negative integer and non-negative integer nn00

such thatsuch that

t(n)t(n) ≤ ≤ c gc g((nn) for every ) for every nn ≥ ≥ nn0 0

Examples:Examples: 1010nn O(2 O(2nn22))

55nn+20 +20 O(10 O(10nn))

Design and Analysis of Algorithms Chapter 2.220

A B O Ω Θ

1 ln2n n YesYes NoNo NoNo

2 nk cn YesYes NoNo NoNo

3 nsinn NoNo NoNo NoNo

4 2n 2n/2 NoNo YesYes NoNo

5 nlgc clgn YesYes YesYes YesYes

6 lg(n!) lg(nn) YesYes YesYes YesYes

n

2n (2n/2) 2n (2n/2)

Establishing rate of growth: Method 1 – using Establishing rate of growth: Method 1 – using definitiondefinition

Examples:Examples:

Design and Analysis of Algorithms Chapter 2.221

A B O Ω Θ

1 ln2n n YesYes NoNo NoNo

2 nk cn YesYes NoNo NoNo

3 nsinn NoNo NoNo NoNo

4 2n 2n/2 NoNo YesYes NoNo

5 nlgc clgn YesYes YesYes YesYes

6 lg(n!) lg(nn) YesYes YesYes YesYes

n

O(nsinn) (nsinn) n n

Establishing rate of growth: Method 1 – using Establishing rate of growth: Method 1 – using definitiondefinition

Examples:Examples:

Design and Analysis of Algorithms Chapter 2.222

Establishing rate of growth: Method 2 – Establishing rate of growth: Method 2 – using limitsusing limits

limlimn→∞ n→∞ tt((nn)/)/gg((nn))

0 order of growth of tt((n)n) < order of growth of gg((nn))t(n) t(n) o(g(n)), o(g(n)), t(n) t(n) O(g(n)) O(g(n))

c>0 order of growth of tt((n)n) = order of growth of gg((nn))t(n) t(n) (g(n)),(g(n)), t(n) t(n) O(g(n)), t(n) O(g(n)), t(n) (g(n))(g(n))

∞ order of growth of tt((n)n) > order of growth of gg((nn))

t(n) t(n) (g(n)),(g(n)), t(n) t(n) (g(n))(g(n))

==

Design and Analysis of Algorithms Chapter 2.223

Establishing rate of growth: Method 2 – Establishing rate of growth: Method 2 – using limitsusing limits

Examples:Examples:• loglogbb n vs. log n vs. logc c nn

loglogbbn = logn = logbbc logc logccn n

limlimn→∞n→∞( log( logbbn / logn / logccn) = limn) = limn→∞n→∞ (log(logbbc) = logc) = logbbcc

loglogbbn n (log(logccn)n)

Design and Analysis of Algorithms Chapter 2.224

Exercises: establishing rate of growth – Exercises: establishing rate of growth – using limitsusing limits

lnln22nn vs. ln vs. lnnn22

22nn vs. 2 vs. 2n/2n/2

22n-1 n-1 vs. 2vs. 2nn

loglog22n n vs. vs. nn

Design and Analysis of Algorithms Chapter 2.225

L’Hôpital’s ruleL’Hôpital’s rule

IfIf limlimn→∞ n→∞ t(n)t(n) = = limlimn→∞ n→∞ gg((nn) = ∞) = ∞

The derivatives The derivatives ff´, ´, gg´ exist,´ exist,

ThenThen

t(n)t(n)gg((nn))

limlimnn→∞→∞

= t t ´(´(nn))g g ´(´(nn))

limlimnn→∞→∞

• Example: logExample: log22nn vs. vs. nn

Design and Analysis of Algorithms Chapter 2.226

A B O Ω Θ

1 ln2n n YesYes NoNo NoNo

2 nk cn YesYes NoNo NoNo

3 nsinn NoNo NoNo NoNo

4 2n 2n/2 NoNo YesYes NoNo

5 nlgc clgn YesYes YesYes YesYes

6 lg(n!) lg(nn) YesYes YesYes YesYes

n

Establishing rate of growthEstablishing rate of growth

Examples:Examples:

Design and Analysis of Algorithms Chapter 2.227

Stirling’s formulaStirling’s formula

n

en

nn )(2!

Design and Analysis of Algorithms Chapter 2.228

n! v.s. nn! v.s. nnn

lg(n!) v.s. lg(nlg(n!) v.s. lg(nnn))

Examples using stirling’s Examples using stirling’s formulaformula

OO

Design and Analysis of Algorithms Chapter 2.229

n! n! o(n o(nnn))

lg(n!) v.s. lg(nlg(n!) v.s. lg(nnn))

??? lg(n!) ??? lg(n!) o(lg(no(lg(nnn))))

Examples using stirling’s Examples using stirling’s formulaformula

OO

Design and Analysis of Algorithms Chapter 2.230

n! n! o(n o(nnn))

lg(n!) v.s. lg(nlg(n!) v.s. lg(nnn))

lg(n!) lg(n!) (lg(n(lg(nnn))))

Examples using stirling’s Examples using stirling’s formulaformula

OO

Design and Analysis of Algorithms Chapter 2.231

Special attentionSpecial attention

n! n! o(n o(nnn))

However,However,

lg(n!) lg(n!) o(lg(n o(lg(nnn)))) lg(n!) lg(n!) (lg(n(lg(nnn))))

sinn sinn (1/2) (1/2) sinn sinn O(1/2) O(1/2) sinn sinn (1/2) (1/2)

However,However,

nn1/2 1/2 (n(nsinnsinn)) nn1/2 1/2 O(n O(nsinnsinn)) nn1/2 1/2 (n(nsinnsinn))

Design and Analysis of Algorithms Chapter 2.232

Asymptotic notation propertiesAsymptotic notation properties

Transitivity:Transitivity:

f(n) = f(n) = (g(n)) && g(n) = (g(n)) && g(n) = (h(n)) (h(n)) f(n) = f(n) = (h(n)) (h(n))

f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(h(n)) f(n) = O(h(n))

f(n) = Ω(g(n)) && g(n) = Ω(h(n)) f(n) = Ω(g(n)) && g(n) = Ω(h(n)) f(n) = Ω(h(n)) f(n) = Ω(h(n))

If tIf t11(n) (n) O(g O(g11(n)) and t(n)) and t22(n) (n) O(g O(g22(n)), then (n)), then

tt11(n) + t(n) + t22(n) (n)

Design and Analysis of Algorithms Chapter 2.233

Asymptotic notation propertiesAsymptotic notation properties

Transitivity:Transitivity:

f(n) = f(n) = (g(n)) && g(n) = (g(n)) && g(n) = (h(n)) (h(n)) f(n) = f(n) = (h(n)) (h(n))

f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(h(n)) f(n) = O(h(n))

f(n) = Ω(g(n)) && g(n) = Ω(h(n)) f(n) = Ω(g(n)) && g(n) = Ω(h(n)) f(n) = Ω(h(n)) f(n) = Ω(h(n))

If tIf t11(n) (n) O(g O(g11(n)) and t(n)) and t22(n) (n) O(g O(g22(n)), then (n)), then

tt11(n) + t(n) + t22(n) (n) O(maxg O(maxg11(n), g(n), g22(n)) (n))