Chapter 1 Basic Concepts Overview: System Life Cycle Algorithm Specification Data Abstraction...

48
Chapter 1 Basic Concepts Chapter 1 Basic Concepts Overview: System Life Cycle Overview: System Life Cycle Algorithm Specification Algorithm Specification Data Abstraction Data Abstraction Performance Analysis Performance Analysis Performance Measurement Performance Measurement
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    231
  • download

    9

Transcript of Chapter 1 Basic Concepts Overview: System Life Cycle Algorithm Specification Data Abstraction...

Chapter 1 Basic ConceptsChapter 1 Basic Concepts

Overview: System Life CycleOverview: System Life Cycle

Algorithm SpecificationAlgorithm Specification

Data AbstractionData Abstraction

Performance AnalysisPerformance Analysis

Performance MeasurementPerformance Measurement

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page2CYUT, Feb. 2002 Chapter 1 Basic Concepts Page2

Data StructuresData Structures What is the "Data Structure" ?What is the "Data Structure" ?

– Ways to represent dataWays to represent data

Why data structure ?Why data structure ?– To design and implement large-scale computer systemTo design and implement large-scale computer system

– Have proven correct algorithmsHave proven correct algorithms

– The art of programmingThe art of programming

How to master in data structure ?How to master in data structure ?– practice, discuss, and thinkpractice, discuss, and think

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page3CYUT, Feb. 2002 Chapter 1 Basic Concepts Page3

System Life CycleSystem Life Cycle SummarySummary

– R A D R C VR A D R C V

RequirementsRequirements– What inputs, functions, and outputsWhat inputs, functions, and outputs

AnalysisAnalysis– Break the problem down into manageable piecesBreak the problem down into manageable pieces

– Top-down approachTop-down approach

– Bottom-up approachBottom-up approach

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page4CYUT, Feb. 2002 Chapter 1 Basic Concepts Page4

System Life Cycle(Cont.)System Life Cycle(Cont.) DesignDesign

– Create abstract data types and the algorithm Create abstract data types and the algorithm specifications, language independentspecifications, language independent

Refinement and CodingRefinement and Coding– Determining data structures and algorithmsDetermining data structures and algorithms

VerificationVerification– Developing correctness proofs, testing the program, Developing correctness proofs, testing the program,

and removing errorsand removing errors

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page5CYUT, Feb. 2002 Chapter 1 Basic Concepts Page5

VerificationVerification

Correctness proofsCorrectness proofs– Prove program mathematicallyProve program mathematically

time-consuming and difficult to develop for large systemtime-consuming and difficult to develop for large system

TestingTesting– Verify that every piece of code runs correctlyVerify that every piece of code runs correctly

provide data including all possible scenariosprovide data including all possible scenarios

Error removalError removal– Guarantee no new errors generatedGuarantee no new errors generated

NotesNotes– Select a proven correct algorithm is importantSelect a proven correct algorithm is important

– Initial tests focus on verifying that a program runs correctly, then Initial tests focus on verifying that a program runs correctly, then reduce the running timereduce the running time

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page6CYUT, Feb. 2002 Chapter 1 Basic Concepts Page6

Chapter 1 Basic ConceptsChapter 1 Basic Concepts Overview: System Life CycleOverview: System Life Cycle Algorithm SpecificationAlgorithm Specification Data AbstractionData Abstraction Performance AnalysisPerformance Analysis Performance MeasurementPerformance Measurement

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page7CYUT, Feb. 2002 Chapter 1 Basic Concepts Page7

Algorithm SpecificationAlgorithm Specification DefinitionDefinition

– An An algorithmalgorithm is a finite set of instructions that, if followed, is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms must accomplishes a particular task. In addition, all algorithms must satisfy the following criteria:satisfy the following criteria:

(1)(1)InputInput. There are zero or more quantities that are externally . There are zero or more quantities that are externally supplied.supplied.

(2)(2)OutputOutput. At least one quantity is produced.. At least one quantity is produced.

(3)(3)DefinitenessDefiniteness. Each instruction is clear and unambiguous.. Each instruction is clear and unambiguous.

(4)(4)FinitenessFiniteness. If we trace out the instructions of an algorithm, then . If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of for all cases, the algorithm terminates after a finite number of steps.steps.

(5)(5)EffectivenessEffectiveness. Every instruction must be basic enough to be . Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper. carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in (3); it also It is not enough that each operation be definite as in (3); it also must be feasible.must be feasible.

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page8CYUT, Feb. 2002 Chapter 1 Basic Concepts Page8

Describing AlgorithmsDescribing Algorithms Natural languageNatural language

– English, ChineseEnglish, Chinese Instructions must be definite and effectivenessInstructions must be definite and effectiveness

Graphic representationGraphic representation– FlowchartFlowchart

work well only if the algorithm is small and simplework well only if the algorithm is small and simple

Pseudo languagePseudo language– ReadableReadable

– Instructions must be definite and effectivenessInstructions must be definite and effectiveness

Combining English and C++Combining English and C++– In this textIn this text

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page9CYUT, Feb. 2002 Chapter 1 Basic Concepts Page9

ProblemProblem– Devise a program that sorts a set of Devise a program that sorts a set of n>= 1 n>= 1 integersintegers

Step I - ConceptStep I - Concept– From those integers that are currently unsorted, find the From those integers that are currently unsorted, find the

smallest and place it next in the sorted listsmallest and place it next in the sorted list

Step II - AlgorithmStep II - Algorithm– for (i= 0; i< n; i++)for (i= 0; i< n; i++){{

Examine Examine list[i] list[i] to to list[n-1] list[n-1] and suppose that the and suppose that the smallest integer is smallest integer is list[min]list[min];;

Interchange Interchange list[i] list[i] and and list[min]list[min];;

}}

Translating a Problem into an AlgorithmTranslating a Problem into an Algorithm

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page10CYUT, Feb. 2002 Chapter 1 Basic Concepts Page10

Translating a Problem into an Algorithm(Cont.)Translating a Problem into an Algorithm(Cont.)

Step III - CodingStep III - Coding

void sort(int *a, int n){ for (i= 0; i< n; i++) { int j= i; for (int k= i+1; k< n; k++){ if (a[k ]< a[ j]) j= k; int temp=a[i]; a[i]=a[ j]; a[ j]=temp; }}

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page11CYUT, Feb. 2002 Chapter 1 Basic Concepts Page11

Correctness ProofCorrectness Proof TheoremTheorem

– Function Function sort(a, n) sort(a, n) correctly sorts a set of correctly sorts a set of n>= 1 n>= 1 integers. The result remains in integers. The result remains in a[0], ..., a[n-1] a[0], ..., a[n-1] such that such that aa[0]<= a[1]<=...<=a[n-1].[0]<= a[1]<=...<=a[n-1].

Proof:Proof:For For i= qi= q, following the execution of line 6-11, we have , following the execution of line 6-11, we have

a[q]<= a[r], q< r< =n-1a[q]<= a[r], q< r< =n-1..

For For i> qi> q, observing, , observing, a[0], ..., a[q] a[0], ..., a[q] are unchanged.are unchanged.

Hence, increasing Hence, increasing ii, for , for i= n-2i= n-2, we have , we have

a[0]<= a[1]<= ...<=a[n-1]a[0]<= a[1]<= ...<=a[n-1]

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page12CYUT, Feb. 2002 Chapter 1 Basic Concepts Page12

Recursive AlgorithmsRecursive Algorithms Direct recursionDirect recursion

– Functions call themselvesFunctions call themselves Indirect recursionIndirect recursion

– Functions call other functions that invoke the calling Functions call other functions that invoke the calling function againfunction again

When is recursion an appropriate mechanism?When is recursion an appropriate mechanism?– The problem itself is defined recursivelyThe problem itself is defined recursively– Statements: if-else and while can be written recursivelyStatements: if-else and while can be written recursively– Art of programmingArt of programming

Why recursive algorithms ?Why recursive algorithms ?– Powerful, express an complex process very clearlyPowerful, express an complex process very clearly

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page13CYUT, Feb. 2002 Chapter 1 Basic Concepts Page13

Recursive Implementation of Binary SearchRecursive Implementation of Binary Search

int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnumint middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;

break; case 0: return middle; case 1: right= middle- 1; break; } } return -1;}

int compare(int x, int y){ if (x< y) return -1; else if (x== y) return 0; else return 1;}

int compare(int x, int y){ if (x< y) return -1; else if (x== y) return 0; else return 1;}

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page14CYUT, Feb. 2002 Chapter 1 Basic Concepts Page14

Recursive Implementation of Binary SearchRecursive Implementation of Binary Search

int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnumint middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1:return binsearch(list, searchnum, middle+1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle- 1); } } return -1;}

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page15CYUT, Feb. 2002 Chapter 1 Basic Concepts Page15

Chapter 1 Basic ConceptsChapter 1 Basic Concepts Overview: System Life CycleOverview: System Life Cycle Algorithm SpecificationAlgorithm Specification Data AbstractionData Abstraction Performance AnalysisPerformance Analysis Performance MeasurementPerformance Measurement

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page16CYUT, Feb. 2002 Chapter 1 Basic Concepts Page16

Data AbstractionData Abstraction Types of dataTypes of data

– All programming language provide at least minimal set All programming language provide at least minimal set of predefined data type, plus user defined typesof predefined data type, plus user defined types

Data types of CData types of C– Char, int, float, and doubleChar, int, float, and double

may be modified by short, long, and unsignedmay be modified by short, long, and unsigned

– Array, struct, and pointerArray, struct, and pointer

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page17CYUT, Feb. 2002 Chapter 1 Basic Concepts Page17

Data TypeData Type DefinitionDefinition

– A A data type data type is a collection of is a collection of objectsobjects and a set of and a set of operationsoperations that act on those objects that act on those objects

Example of "int"Example of "int"– Objects: 0, +1, -1, ..., Int_Max, Int_MinObjects: 0, +1, -1, ..., Int_Max, Int_Min– Operations: Operations: arithmeticarithmetic(+, -, *, /, and %), (+, -, *, /, and %),

testingtesting(equality/inequality), (equality/inequality), assignsassigns, , functionsfunctions Define operationsDefine operations

– Its Its namename, possible , possible argumentsarguments and and resultsresults must be must be specifiedspecified

The design strategy for representation of objectsThe design strategy for representation of objects– TransparentTransparent to the user to the user

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page18CYUT, Feb. 2002 Chapter 1 Basic Concepts Page18

Abstract Data TypeAbstract Data Type

DefinitionDefinition– An An abstract data typeabstract data type((ADTADT) is a ) is a data type data type that is that is

organized in such a way that the specification of the organized in such a way that the specification of the objects and the specification of the operations on the objects and the specification of the operations on the objects is separated from the representation of the objects objects is separated from the representation of the objects and the implementation of the operation.#and the implementation of the operation.#

Why abstract data type ?Why abstract data type ?– implementation-independentimplementation-independent

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page19CYUT, Feb. 2002 Chapter 1 Basic Concepts Page19

Classifying the Functions of a Data TypeClassifying the Functions of a Data Type Creator/constructor:Creator/constructor:

– Create a new instance of the designated typeCreate a new instance of the designated type

TransformersTransformers– Also create an instance of the designated type by using Also create an instance of the designated type by using

one or more other instancesone or more other instances

Observers/reportersObservers/reporters– Provide information about an instance of the type, but Provide information about an instance of the type, but

they do not change the instancethey do not change the instance

NotesNotes– An ADT definition will include at least one function An ADT definition will include at least one function

from each of these three categoriesfrom each of these three categories

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page20CYUT, Feb. 2002 Chapter 1 Basic Concepts Page20

An Example of the ADTAn Example of the ADTstructure Natural_Number is objects: an ordered subrange of the integers starting at zero and ' ending at the maximum integer (INT_MAX) on the computer functions: for all x, y is Nat_Number, TRUE, FALSE is Boolean and where . +, -, <, and == are the usual integer operations Nat_NoZero() ::= 0 Boolean Is_Zero(x) ::= if (x) return FALSE Nat_No Add(x, y) ::= if ((x+y)<= INT_MAX) return x+ y

else return INT_MAX Boolean Equal(x, y) ::= if (x== y) return TRUE

else return FALSE Nat_No Successor(x) ::= if (x== INT_MAX) return x

else return x+ 1 Nat_No Subtract(x, y) ::= if (x< y) return 0

else return x-yend Natural_Number

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page21CYUT, Feb. 2002 Chapter 1 Basic Concepts Page21

Chapter 1 Basic ConceptsChapter 1 Basic Concepts Overview: System Life CycleOverview: System Life Cycle Algorithm SpecificationAlgorithm Specification Data AbstractionData Abstraction Performance AnalysisPerformance Analysis Performance MeasurementPerformance Measurement

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page22CYUT, Feb. 2002 Chapter 1 Basic Concepts Page22

Performance AnalysisPerformance Analysis Performance evaluationPerformance evaluation

– Performance Performance analysisanalysis

– Performance Performance measurementmeasurement

Performance Performance analysis - prioranalysis - prior– an important branch of CS, an important branch of CS, complexity theorycomplexity theory

– estimate estimate timetime and and spacespace

– machine independentmachine independent

Performance Performance measurement -posteriormeasurement -posterior– The actual The actual timetime and and space space requirementsrequirements

– machine dependentmachine dependent

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page23CYUT, Feb. 2002 Chapter 1 Basic Concepts Page23

Performance Analysis(Cont.)Performance Analysis(Cont.) Space and timeSpace and time

– Does the program efficiently use primary and Does the program efficiently use primary and secondary storage?secondary storage?

– Is the program's running time acceptable for the task?Is the program's running time acceptable for the task?

Evaluate a program generallyEvaluate a program generally– Does the program Does the program meetmeet the original the original specificationsspecifications of the task? of the task?

– Does it Does it workwork correctlycorrectly??

– Does the program contain Does the program contain documentationdocumentation that show that show how to use it how to use it and and how it workshow it works??

– Does the program Does the program effectivelyeffectively use functions use functions to create logical units?to create logical units?

– Is the program's code Is the program's code readablereadable??

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page24CYUT, Feb. 2002 Chapter 1 Basic Concepts Page24

Performance Analysis(Cont.)Performance Analysis(Cont.)

Evaluate a programEvaluate a program– MWGWREREMWGWRERE

MMeet specifications, eet specifications, WWork correctly, ork correctly,

GGood user-interface, ood user-interface, WWell-documentation,ell-documentation,

RReadable, eadable, EEffectively use functions, ffectively use functions,

RRunning time acceptable, unning time acceptable, EEfficiently use spacefficiently use space

How to achieve them?How to achieve them?– Good programming style, experience, and practiceGood programming style, experience, and practice

– Discuss and thinkDiscuss and think

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page25CYUT, Feb. 2002 Chapter 1 Basic Concepts Page25

Space ComplexitySpace Complexity

DefinitionDefinition– The The space complexity space complexity of a program is the amount of of a program is the amount of

memory that it needs to run to completionmemory that it needs to run to completion The space needed is the sum of The space needed is the sum of

– FixedFixed space and space and VariableVariable space space FixedFixed space space

– Includes the instructions, variables, and constantsIncludes the instructions, variables, and constants– Independent of the number and size of I/OIndependent of the number and size of I/O

VariableVariable space space – Includes dynamic allocation, functions' recursionIncludes dynamic allocation, functions' recursion

Total space of any program Total space of any program – S(P)= c+ S(P)= c+ SSpp(Instance)(Instance)

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page26CYUT, Feb. 2002 Chapter 1 Basic Concepts Page26

Examples of Evaluating Space ComplexityExamples of Evaluating Space Complexityfloat abc(float a, float b, float c){ return a+b+b*c+(a+b-c)/(a+b)+4.00;}

Sabc(I)= 0

float sum(float list[], int n){ float fTmpSum= 0; int i; for (i= 0; i< n; i++) fTmpSum+= list[i]; return fTmpSum;}

Ssum(I)= Ssum (n)= 0

float rsum(float list[], int n){ if (n) return rsum(list, n-1)+ list[n-1]; return 0;}

Srsum (n)= 4*n

parameter:float(list[]) 1parameter:integer(n) 1return address 1return value 1

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page27CYUT, Feb. 2002 Chapter 1 Basic Concepts Page27

Time ComplexityTime Complexity DefinitionDefinition

The The time complexity, T(p), time complexity, T(p), taken by a program P is the sum of the compitaken by a program P is the sum of the compile time and the run time le time and the run time

Total timeTotal time T(P)= compile time + T(P)= compile time + runrun (or (or executionexecution) ) timetime = c + t= c + tpp(instance characteristics)(instance characteristics) Compile time does not depend on the instance characteristicsCompile time does not depend on the instance characteristics

How to evaluate?How to evaluate? Use the system clockUse the system clock Number of Number of stepssteps performed performed

machine-independentmachine-independent

Definition of a program stepDefinition of a program step A A program step program step is a syntactically or semantically meaningful program segment is a syntactically or semantically meaningful program segment

whose execution time is independent of the whose execution time is independent of the instanceinstance characteristics characteristics(10 additions can be one step, 100 multiplications can also be one step)(10 additions can be one step, 100 multiplications can also be one step)(p33~p35 (p33~p35 有計算有計算 C++ C++ 語法之 語法之 steps steps 之概述之概述 , , 原則是一個表示式一步原則是一個表示式一步 ))

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page28CYUT, Feb. 2002 Chapter 1 Basic Concepts Page28

Examples of Determining StepsExamples of Determining Steps the first method: count by a programfloat sum(float list[], int n){ float tempsum= 0; count++; /* for assignment */ int i; for(i= 0; i< n; i++) { count++; /* for the for loop */ tempsum+= list[i]; count++; /* for assignment */ } count++; /* last execution of for */ count++; /* for return */ return tempsum;}

float sum(float list[], int n){ float tempsum= 0 int i; for (i=0; i< n; i++) count+= 2; count+= 3; return 0;}

2n+ 3

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page29CYUT, Feb. 2002 Chapter 1 Basic Concepts Page29

Examples of Determining Steps(Cont.)Examples of Determining Steps(Cont.)

float rsum(float list[], int n){ count ++; /* for if condition */ if (n) { count++; /* for return and rsum invocation */ return rsum(list, n-1)+ list[n-1]; } count++; //return return list[0];}

2n+ 2

void add(int a[][MaxSize], int b[][MaxSize],int c[][MaxSize], int rows, int cols)

{ int i, j; for (i=0; i< rows; i++) for (j=0; j< cols; j++) c[i][j]= a[i][j] + b[i][j]; }

2rows*cols+ 2rows+ 1

trsum(0) = 2trsum(n) = 2 + trsum(n-1) = 2 + 2 + trsum(n-2) = 2*2 + trsum(n-2) = … = 2n + trsum(0)= 2n+2

p.39, program 1.19自行計算

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page30CYUT, Feb. 2002 Chapter 1 Basic Concepts Page30

Examples of Determining Steps(Cont.)Examples of Determining Steps(Cont.)

Statement s/e Frequency Total Steps

void add(int a[][MaxSize], . . . 0 0 0{ 0 0 0 int i, j; 0 0 0 for (i=0; i< rows; i++) 1 rows+ 1 rows+ 1 for (j=0; j< cols; j++) 1 rows*(cols+1) rows*cols+ rows c[i][j]= a[i][j] + b[i][j]; 1 rows*cols rows*cols } 0 0 0

Total 2rows*cols+2rows+1

The second method: build a table to count s/e: steps per execution frequency: total numbers of times each statements is executed

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page31CYUT, Feb. 2002 Chapter 1 Basic Concepts Page31

Remarks of Time ComplexityRemarks of Time Complexity Difficulty: the time complexity is not dependent solely on Difficulty: the time complexity is not dependent solely on

the number of inputs or outputsthe number of inputs or outputs To determine the step countTo determine the step count

BestBest case, case, WorstWorst case, case, andand AverageAverage ExampleExample

int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnumint middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;

break; case 0: return middle; case 1: right= middle- 1; } } return -1;}

int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnumint middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;

break; case 0: return middle; case 1: right= middle- 1; } } return -1;}

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page32CYUT, Feb. 2002 Chapter 1 Basic Concepts Page32

Asymptotic Notation(O, Asymptotic Notation(O, , , )) motivationmotivation

– Target: Compare the time complexity of two programs that compuTarget: Compare the time complexity of two programs that computing the same function and predict the growth in run time as instanting the same function and predict the growth in run time as instance characteristics changece characteristics change

– Determining the exact step count is difficult task Determining the exact step count is difficult task – Not very useful for comparative purposeNot very useful for comparative purpose

ex: Cex: C11nn22+C+C22n <= Cn <= C33n for n <= 98, (Cn for n <= 98, (C11=1, C=1, C22=2, C=2, C33=100)=100) CC11nn22+C+C22n > Cn > C33n for n > 98,n for n > 98,

– Determining the exact step count usually not worth(can not get exaDetermining the exact step count usually not worth(can not get exact run time)ct run time)

Asymptotic notationAsymptotic notation– Big "oh“ OBig "oh“ O

upper bound(current trend)upper bound(current trend)

– Omega Omega lower boundlower bound

– Theta Theta upper and lower boundupper and lower bound

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page33CYUT, Feb. 2002 Chapter 1 Basic Concepts Page33

Asymptotic Notation Asymptotic Notation OO

Definition of Big "oh"Definition of Big "oh"– f(n)= O(g((n)) f(n)= O(g((n)) iff there exist iff there exist positivepositive constants constants cc and and nn00

such that such that f(n)<= cg(n)f(n)<= cg(n) for all for all nn, , n>= nn>= n00

ExamplesExamples– 3n+ 2= O(n) as 3n+ 2<= 4n for all n>= 23n+ 2= O(n) as 3n+ 2<= 4n for all n>= 2– 10n10n22+ 4n+ 2= O(n+ 4n+ 2= O(n22) as 10n) as 10n22+ 4n+ 2<= 11n+ 4n+ 2<= 11n22 for n>= 5 for n>= 5– 3n+2<> O(1), 10n3n+2<> O(1), 10n22+ 4n+ 2<> O(n)+ 4n+ 2<> O(n)

RemarksRemarks– g(n) is the least upper boundg(n) is the least upper bound

n=O(nn=O(n22)=O(n)=O(n2.52.5)= O(n)= O(n33)= O(2)= O(2nn))

– O(1): constant, O(n): linear, O(nO(1): constant, O(n): linear, O(n22): quadratic, O(n): quadratic, O(n33): cu): cubic, and O(2bic, and O(2nn): exponential): exponential

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page34CYUT, Feb. 2002 Chapter 1 Basic Concepts Page34

Asymptotic Notation Asymptotic Notation O (Cont.)O (Cont.) Remarks on "="Remarks on "="

– O(g(n))= f(n) O(g(n))= f(n) is meaninglessis meaningless– ""==" as "" as "isis" and not as "equals"" and not as "equals"

TheoremTheorem– If If f(n)= af(n)= ammnnmm+...+ a+...+ a11n+ an+ a00, then , then f(n)= O(nf(n)= O(nmm))– Proof:Proof:

1 ,

)(

0

0

0

nforan

nan

nanf

m

ii

m

mim

ii

m

im

ii

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page35CYUT, Feb. 2002 Chapter 1 Basic Concepts Page35

Asymptotic Notation Asymptotic Notation DefinitionDefinition

– f(n)= f(n)= (g(n)) (g(n)) iff there exist positive constants iff there exist positive constants cc and and nn00 such that such that f(n)>= cg(n) f(n)>= cg(n) for all for all nn, , n>= nn>= n00

ExamplesExamples– 3n+ 2= 3n+ 2= (n) as 3n+ 2>= 3n for n>= 1(n) as 3n+ 2>= 3n for n>= 1– 10n10n22+ 4n+ 2= + 4n+ 2= (n(n22) as 10n) as 10n22+4n+ 2>= n+4n+ 2>= n22 for n>= 1 for n>= 1– 6*26*2nn+ n+ n22= = (2(2nn) as 6*2) as 6*2nn+ n+ n22 >= 2 >= 2nn for n>= 1 for n>= 1

RemarksRemarks– The largest lower boundThe largest lower bound

3n+3= 3n+3= (1), 10n(1), 10n22+4n+2= +4n+2= (n); 6*2(n); 6*2nn+ n+ n22= = (n(n100100))

TheoremTheorem– If f(n)= aIf f(n)= ammnnmm+ ...+ a+ ...+ a11

nn+ a+ a00 and a and amm> 0, then f(n)= > 0, then f(n)= (n(nmm))

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page36CYUT, Feb. 2002 Chapter 1 Basic Concepts Page36

Asymptotic Notation Asymptotic Notation

DefinitionDefinition– f(n)= f(n)= (g(n)) (g(n)) iff there exist positive constants iff there exist positive constants cc11, , cc22, and n, and n00

such that such that cc11g(n)<= f(n) <= cg(n)<= f(n) <= c22g(n) g(n) for all for all n, n>= nn, n>= n00

ExamplesExamples– 3n+2=3n+2=(n) as 3n+2>=3n for n>1 and 3n+2<=4n for all n>= 2(n) as 3n+2>=3n for n>1 and 3n+2<=4n for all n>= 2– 10n10n22+ 4n+ 2= + 4n+ 2= (n (n22); 6*2); 6*2nn+n+n22= = (2(2nn))

RemarksRemarks– Both an upper and lower boundBoth an upper and lower bound– 3n+2<>3n+2<>(1); 10n(1); 10n22+4n+ 2<> +4n+ 2<> (n)(n)

TheoremTheorem– If f(n)= aIf f(n)= ammnnmm+ ... +a+ ... +a11n+ an+ a00 and a and amm> 0, then f(n)= > 0, then f(n)= (n(nmm))

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page37CYUT, Feb. 2002 Chapter 1 Basic Concepts Page37

Example of Time Complexity AnalysisExample of Time Complexity Analysis

Statement Asymptotic complexity

void add(int a[][Max.......) 0{ 0 int i, j; 0 for(i= 0; i< rows; i++) (rows) for(j=0; j< cols; j++) (rows*cols) c[i][j]= a[i][j]+ b[i][j]; (rows*cols)} 0

Total (rows*cols)

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page38CYUT, Feb. 2002 Chapter 1 Basic Concepts Page38

Example of Time Complexity Analysis(Cont.)Example of Time Complexity Analysis(Cont.)

int binsearch(int list[], int .....){ int middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;

break; case 0: return middle; case 1: right= middle- 1; } } return -1;}

worst case (log n)

The more global approach to count steps: focus the variation of instance characterics.

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page39CYUT, Feb. 2002 Chapter 1 Basic Concepts Page39

Example of Time Complexity Analysis(Cont.)Example of Time Complexity Analysis(Cont.)void perm(char *a, int k, int n){//generate all the 排列 of// a[k],…a[n-1]char temp; if (k == n-1){ for(int i= 0; i<=n; i++)

cout << a[i]<<“ ”; cout << endl; }else { for(i= k; i< n; i++){ temp=a[k];a[k]=a[i];a[i]=temp; perm(a, k+1, n); temp=a[k];a[k]=a[i];a[i]=temp; } }}

k= n-1, (n)k< n-1, else for loop, n-k times each call Tperm(k+1, n-1) hence, (Tperm (k+1, n-1))so, Tperm (k, n-1)= ((n-k)(Tperm (k+1, n-1))) Using the substitution, we have Tperm (0, n-1)= (n(n!)), n>= 1

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page40CYUT, Feb. 2002 Chapter 1 Basic Concepts Page40

Example of Time Complexity Example of Time Complexity Analysis(Cont.)Analysis(Cont.)

Magic squareMagic square– An n-by-n matrix of the integers from 1 to nAn n-by-n matrix of the integers from 1 to n22 such that such that

the sum of each row and column and the two major the sum of each row and column and the two major diagonals is the samediagonals is the same

– Example, n= 5(n must be odd)Example, n= 5(n must be odd)

15 8 1 24 1716 14 7 5 2322 20 13 6 43 21 19 12 109 2 25 18 11

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page41CYUT, Feb. 2002 Chapter 1 Basic Concepts Page41

Magic Square (Cont.)Magic Square (Cont.) Coxeter has given the simple ruleCoxeter has given the simple rule

– Put a one in the middle box of the top row. Put a one in the middle box of the top row.

Go up and left assigning numbers in increasing order to Go up and left assigning numbers in increasing order to empty boxes. empty boxes.

If your move causes you to jump off the square, figure If your move causes you to jump off the square, figure out where you would be if you landed on a box on the oout where you would be if you landed on a box on the opposite side of the square.pposite side of the square.

Continue with this box.Continue with this box.

If a box is occupied, go down instead of up and continuIf a box is occupied, go down instead of up and continue.e.

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page42CYUT, Feb. 2002 Chapter 1 Basic Concepts Page42

Magic Square (Cont.)Magic Square (Cont.)procedure MAGIC(square, n)// for n odd create a magic square which is declared as an array// square(0: n-1, 0: n-1)// (i, j) is a square position. 2<= key <= n2 is integer valuedif n is even the [print("input error"); stop]SQUARE<- 0square(0, (n-1)/2)<- 1; // store 1 in middle of first rowkey<- 2; i<- 0; j<- (n-1)/2 // i, j are current positionwhile key <= n2 do (k, l)<- ((i-1) mod n, (j-1)mod n) // look up and left if square(k, l) <> 0 then i<- (i+1) mod n // square occupied, move down else (i, j)<- (k, l) // square (k, l) needs to be assigned square(i, j)<- key // assign it a value key<- key + 1endprint(n, square) // out resultend MAGIC

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page43CYUT, Feb. 2002 Chapter 1 Basic Concepts Page43

Practical ComplexitiesPractical Complexities Time complexityTime complexity

– Generally some function of the instance characteristicsGenerally some function of the instance characteristics Remarks on "n"Remarks on "n"

– If Tp=If Tp=(n), Tq= (n), Tq= (n(n22), then we say P is faster than Q f), then we say P is faster than Q for "sufficiently large" n.or "sufficiently large" n.

since Tp<= cn, n>= nsince Tp<= cn, n>= n11, and Tq<= dn, and Tq<= dn22, n>= n, n>= n22,,

but cn<= dnbut cn<= dn2 2 for n>= c/dfor n>= c/d

so P is faster than Q whenever n>= max{nso P is faster than Q whenever n>= max{n11, n, n22, d/c}, d/c}

– See Table 1.7 and Figure 1.3See Table 1.7 and Figure 1.3 For reasonable large n, n> 100, only program of sFor reasonable large n, n> 100, only program of s

mall complexity, n, nlog n, nmall complexity, n, nlog n, n22, n, n33 are feasible are feasible– See Table 1.8See Table 1.8

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page44CYUT, Feb. 2002 Chapter 1 Basic Concepts Page44

Table 1.8 Times on a 1 bsps computerTable 1.8 Times on a 1 bsps computer

Time for f(n) instructions on 109 instr/sec computer

n f(n)= n f(n)=log2n f(n)=n2 f(n)=n3 f(n)=n4 f(n)=n10 f(n)=2n

1020304050

1001,000

10,000100,000

1,000,000

.01us

.02us

.03us

.04us

.05us

.10us1.00us

10.00us100.00us

1.00ms

.03us

.09us

.15us

.21us

.28us

.66us0.96us

130.03us1.66ms

19.92ms

.1us

.4us

.9us1.6us2.5us10us1ms

100ms10s

16.67m

1us8us

27us64us

125us1ms

1s16.67m11.57d31.71y

10us160us810us

2.56ms6.25us100ms

16.67m115.7d3171y3*107y

10s2.84hr6.83d

12136d3.1y

3171y3*1013y3*1023y3*1033y3*1043y

1us1ms

1s18.3m

13d4*1013y

32*10283y

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page45CYUT, Feb. 2002 Chapter 1 Basic Concepts Page45

Table 1.7 Function valuesTable 1.7 Function values

Instance characteristic n

Time Name 1 2 4 8 16 32

1 Constant 1 1 1 1 1 1 log n Logarithmic 0 1 2 3 4 5 n Linear 1 2 4 8 16 32nlog n Log Linear 0 2 8 24 64 160 n2 Quadratic 1 4 16 64 256 1024 n3 Cubic 1 8 61 512 4096 32768 2n Exponential 2 4 16 256 65536 4294967296 n! Factorial 1 2 54 40326 20922789888000 26313*1033

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page46CYUT, Feb. 2002 Chapter 1 Basic Concepts Page46

Chapter 1 Basic ConceptsChapter 1 Basic Concepts Overview: System Life CycleOverview: System Life Cycle Algorithm SpecificationAlgorithm Specification Data AbstractionData Abstraction Performance AnalysisPerformance Analysis Performance MeasurementPerformance Measurement

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page47CYUT, Feb. 2002 Chapter 1 Basic Concepts Page47

Performance MeasurementPerformance MeasurementObtaining the actual space and time of a programUsing Borland C++, ‘386 at 25 MHzTime(hsec): returns the current time in hundredths of a sec.Goal: 得到測量結果的曲線圖 , 並進而求得執行時間方程式 Step 1, 分析 (g(n)), (g(n)), 做為起始預測做為起始預測 Step 2, write a program to testStep 2, write a program to test -- 技巧技巧 1 : to time a short event, to repeat it several times 1 : to time a short event, to repeat it several times -- 技巧技巧 2 : suitable test data need to be generated2 : suitable test data need to be generatedExample: time(start); for(b=1; b<=r[j];b++) k=seqsearch(a,n[j],0);// 被測對象 time(stop); totaltime = stop –start; runtime = totaltime/r[j]; // 結果參考 fig 1.5, fig1.6

CYUT, Feb. 2002 Chapter 1 Basic Concepts Page48CYUT, Feb. 2002 Chapter 1 Basic Concepts Page48

SummarySummary Overview: System Life CycleOverview: System Life Cycle Algorithm SpecificationAlgorithm Specification

– Definition, DescriptionDefinition, Description Data Abstraction- ADTData Abstraction- ADT Performance AnalysisPerformance Analysis

– Time and SpaceTime and Space O(g(n))O(g(n))

Performance MeasurementPerformance Measurement Generating Test DataGenerating Test Data - - analyze the algorithm being tested to determine classes analyze the algorithm being tested to determine classes

of dataof data