Extracting Performance FunctionsBasic OperationsBasic Operations: ExamplesCounting ProceduresCounting IF StatementsCounting LoopsInput Dependent LoopsCounting Nested LoopsAlgorithm PeculiaritiesRecursive FunctionsRecursion: An ExampleBoundary ConditionsExtracting Performance Extracting Performance FunctionsFunctionsBasic OperationsBasic OperationsThe number of Basic Operations performed The number of Basic Operations performed must be proportional to the run timemust be proportional to the run timeCounting techniques depend on control Counting techniques depend on control structuresstructuresThe Worst Case assumption is most commonThe Worst Case assumption is most commonAverage Case can be done for some Average Case can be done for some algorithmsalgorithmsBasic Operations: ExamplesBasic Operations: ExamplesSortingSortingKey-to-Key ComparisonsKey-to-Key ComparisonsSearchingSearchingKey-to-Unknown ComparesKey-to-Unknown ComparesMatrix Multiply Adds, or MultipliesMatrix Multiply Adds, or MultipliesGraph Operations Processing a VertexGraph Operations Processing a VertexPolynomial Evaluation Arithmetic Ops.Polynomial Evaluation Arithmetic Ops.Counting ProceduresCounting ProceduresStraight-Line Code:Straight-Line Code:–Simply Count the operations you seeSimply Count the operations you seeAssume basic operation is additionAssume basic operation is additiona := b + c - dc := x+5d := d * 3e := e + 1Total Operations = 3Counting IF StatementsCounting IF StatementsBasic Operation is AdditionBasic Operation is AdditionAssume Worst CaseAssume Worst CaseCount Only One SideCount Only One SideIf a = b+c Then c := d + e f := g + c + 1Else c := e + fEndifTotal Operations = 4Counting LoopsCounting LoopsAssume Worst-Case Number of IterationsAssume Worst-Case Number of IterationsCount Body, Multiply by Iteration CountCount Body, Multiply by Iteration CountAssume Basic Operation is AdditionAssume Basic Operation is AdditionFor i := 1 to 12 do a := b+c d := d + 7End ForTotal Operations = 24Input Dependent LoopsInput Dependent LoopsIf the number of iterations depends on the If the number of iterations depends on the size of the input, size of the input, nn, then the count is a , then the count is a function of function of nnFor i := 1 to n do a := b+c d := d + 7End ForTotal Operations = 2nCounting Nested LoopsCounting Nested LoopsThe following rules of thumb The following rules of thumb usuallyusually apply apply–A single loop yields a linear function of A single loop yields a linear function of nn–A doubly-nested loop yields a function of A doubly-nested loop yields a function of nn22–A triply-nested loop yields a function of A triply-nested loop yields a function of nn33Be Careful when applying these rulesBe Careful when applying these rulesFor i := 1 to n do For j := 1 to n do a := a + 1 End ForEnd ForTotal Operations = n2For i := 1 to n do For j := 1 to 3 do a := a + 1 End ForEnd ForTotal Operations = 3nAlgorithm PeculiaritiesAlgorithm PeculiaritiesIt is necessary to take the peculiarities of an It is necessary to take the peculiarities of an algorithm into account when counting operationsalgorithm into account when counting operationsi := 1; Cond := ExternalFunction( );While (i<n) And (Cond) do a := a + 1; Cond := ExternalFunction( ); i := i + 1;End While;For j := i to n do a := a + 1;End For;Total Operations = nRecursive FunctionsRecursive FunctionsDefine W(Define W(nn) as the number of operations ) as the number of operations done for input of size done for input of size nnWhen encountering a recursive call, add When encountering a recursive call, add W(x) where x is the size of the input for the W(x) where x is the size of the input for the recursive callrecursive callMore work must be done to obtain a usable More work must be done to obtain a usable solutionsolutionRecursion: An ExampleRecursion: An ExampleBasic Operation is MultiplicationBasic Operation is MultiplicationSize of input is Value of Size of input is Value of xxFunction Fact(x: integer):Integerbegin If x < 1 Then Fact = 1; Else Fact := Fact(x-1)*x; End IfendW(n) = W(n-1) + 1Boundary ConditionsBoundary ConditionsThe Equation W(n) = W(n-1) + 1 is called A The Equation W(n) = W(n-1) + 1 is called A Recurrence RelationRecurrence RelationIt Must be solved to remove the reference to It Must be solved to remove the reference to W on the right hand sideW on the right hand sideSolution requires a boundary condition of Solution requires a boundary condition of the form W(the form W(aa) = ) = kk for constants for constants aa and and kkIn the In the FactFact example: W(0) = 0 example: W(0) =
or
We will never post anything without your permission.
Don't have an account? Sign up