Algorithms And Data Structures - Complexity Of Algorithms

3y ago
54 Views
10 Downloads
355.68 KB
35 Pages
Last View : 14d ago
Last Download : 3m ago
Upload by : Esmeralda Toy
Transcription

Algorithmsand DataStructuresMarcinSydowAlgorithms and Data StructuresComplexity of AlgorithmsMarcin Sydow

Desired Properties of a Good AlgorithmAlgorithmsand DataStructuresMarcinSydowAny good algorithm should satisfy 2 obvious conditions:12correct (desired) output (for the given problem)be e ective ( fast )computead. 1) correctness of algorithmad. 2) complexity of algorithmComplexity of algorithm measures how fast is the algorithmtime complexity) and what amount of memory it usesspace complexity) - time and memory - 2 basic resources in((computations

Example - the Search ProblemAlgorithmsand DataStructuresMarcinSydowProblem ofsearching a key in an arrayWhat does the amount of work of this algorithm depend on?find(arr, len, key)Speci cation:input: arr - array of integers, len - it's length, key - integer to be foundoutput: integer 0stored i lenbeing the index in arr, under which the key is

Example - the Search ProblemAlgorithmsand DataStructuresMarcinSydowProblem ofsearching a key in an arrayWhat does the amount of work of this algorithm depend on?find(arr, len, key)Speci cation:input: arr - array of integers, len - it's length, key - integer to be foundoutput: integer 0 i lenbeing the index in arr, under which the key isstored (is it a complete/clear speci cation?)

Example - the Search ProblemAlgorithmsand DataStructuresMarcinSydowProblem ofsearching a key in an arrayWhat does the amount of work of this algorithm depend on?find(arr, len, key)Speci cation:input: arr - array of integers, len - it's length, key - integer to be foundoutput: integer 0 i lenbeing the index in arr, under which the key isstored (is it a complete/clear speci cation?) or the value of -1 when thereis no speci ed key in ( rst len positions of ) the array

Example - the Search ProblemAlgorithmsand DataStructuresMarcinSydowProblem ofsearching a key in an arrayWhat does the amount of work of this algorithm depend on?find(arr, len, key)Speci cation:input: arr - array of integers, len - it's length, key - integer to be foundoutput: integer 0 i lenbeing the index in arr, under which the key isstored (is it a complete/clear speci cation?) or the value of -1 when thereis no speci ed key in ( rst len positions of ) the arraycode:find(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}What does the amount of work of this algorithm depend on?

The speed of algorithmAlgorithmsand DataStructuresMarcinSydowHow to measure how fast (or slow) an algorithm is?There are 2 issues to be considered when designing such ameasure:1independence on any programming language (andhardware/software platform)2maximum independence on particular input dataIt should be an internal property of the algorithm itselfAny idea?

The speed of algorithmAlgorithmsand DataStructuresMarcinSydowHow to measure how fast (or slow) an algorithm is?There are 2 issues to be considered when designing such ameasure:1independence on any programming language (andhardware/software platform)2maximum independence on particular input dataIt should be an internal property of the algorithm itselfAny idea? Count basicoperations of the algorithm

Dominating OperationsAlgorithmsand DataStructuresMarcinSydowSimpli cation: it is not necessary to count all the operations - itis enough to count the representative onesBefore doing a complexity analysis2 steps must be done:1determine the dominating operation set2observe what (in input) in uences the number ofdata size)dominating operations (Dominating operations are those which cover the amount ofwork which is proportional to the whole amount of work of thealgorithm (they well represent the whole)

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}assignmenti 0?

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}assignmentcomparisoni 0 ? noi len ?

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}i 0 ? noi len ? yescomparison arr[i] keyassignmentcomparison?

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}i 0 ? noi len ? yescomparison arr[i] keyassignmentcomparisonboth the above? yes

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}i 0 ? noi len ? yescomparison arr[i] keyassignmentcomparison? yesboth the above? yesreturn statementreturn i?

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}i 0 ? noi len ? yescomparison arr[i] keyassignmentcomparison? yesboth the above? yesreturn ii ?return statementindex increment? no

Example - determining operating operationsAlgorithmsand DataStructuresMarcinSydowWhat can be thealgorithm?dominating operation set in the followingfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}i 0 ? noi len ? yescomparison arr[i] keyassignmentcomparison? yesboth the above? yesreturn ii ? yesreturn statementindex increment? no

Example, cont. - determining the data sizeAlgorithmsand DataStructuresMarcinSydowWhat is thedata size in the following algorithm?find(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}

Example, cont. - determining the data sizeAlgorithmsand DataStructuresMarcinSydowWhat is thedata size in the following algorithm?find(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}Data size:length of array arr

Example, cont. - determining the data sizeAlgorithmsand DataStructuresMarcinSydowWhat is thedata size in the following algorithm?find(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}Data size:length of array arrHaving determined thedominating operation and data sizewe can determine time complexity of the algorithm

Time Complexity of AlgorithmAlgorithmsand DataStructuresMarcinSydowDe nitionTime Complexity of Algorithm is the number of dominatingoperations executed by the algorithmas the function of datasize.Time complexity measures the amount of work done by thealgorithm during solving the problem in the way which isindependent on the implementation and particular input data.The lower time complexity the faster algorithm

Example - time complexity of algorithmAlgorithmsand DataStructuresMarcinSydowfind(arr, len, key){i 0while(i len){if(arr[i] key)return ii }return -1}Assume:dominating operation: comparison arr[i] keydata size: the length of array (denote by n)Thus, the number of dominating operations executed by thisalgorithm ranges:fromto1 (the key was found under the rst index)n (the key is absent or under the last index)There is no single function which could express the dependence of the number ofexecuted dominating operations on the data size for this algorithm.

Pessimistic Time ComplexityAlgorithmsand DataStructuresMarcinSydowlet's assume the following denotations:n - data sizeDn - the set of all possible input datasets of size nt (d ) - the number of dominating operations for dataset d (of size n)( d Dn )De nitionPessimistic Time Complexity of algorithm:W (n) sup{t (d ) : d Dn }(W(n) -Worst)Pessimistic Time Complexity expresses the number ofdominating operations in the worst case of input data of size nE.g. for our example the pessimistic time complexity is given bythe formula:

Pessimistic Time ComplexityAlgorithmsand DataStructuresMarcinSydowlet's assume the following denotations:n - data sizeDn - the set of all possible input datasets of size nt (d ) - the number of dominating operations for dataset d (of size n)( d Dn )De nitionPessimistic Time Complexity of algorithm:W (n) sup{t (d ) : d Dn }(W(n) -Worst)Pessimistic Time Complexity expresses the number ofdominating operations in the worst case of input data of size nE.g. for our example the pessimistic time complexity is given bythe formula:W (n ) n

Average Time Complexity of AlgorithmAlgorithmsand DataStructuresMarcinSydowlet's assume the following denotations:n - data sizeDn - the set of all possible input datasets of size nt (d ) - the number of dominating operations for dataset d (of size n) (d Dn )Xn - random variable, its value is t(d) for d Dnpnk - probability distribution of the random variable Xn (i.e. the probability thatfor input data of size n the algorithm will execute k dominating operationsk 0))(De nitionAverage Time Complexity of Algorithm:A(n) k 0 pnk · k PPP (Xn k ) · k(expected value of the random variable representing the numberof dominating operations)(A(n)Average)

Example - Determining the Average TimeComplexityAlgorithmsand DataStructuresMarcinSydowLet's determine the average time complexity for our exemplaryalgorithm ( nd)First, we have to assume someprobabilistic model of inputdata (i.e. the probabilistic distribution of possible inputdatasets)Let's make a simplistic assumption: the key to be found occursexactly once in array and with the same probability on eachk n P ( Xn k ) 1 / n )index (uniform distribution) ( 0 Thus:A(n) k 0 P (Xn k ) · k PPn 1k n 1/n · k 20

Space Complexity of AlgorithmAlgorithmsand DataStructuresMarcinSydowDe nitionS(n) is the number of unitsof memory used by algorithm as a function of data sizeSpace Complexity of Algorithm:This characteristic is more dependent on particular platform than time complexity.As a memory unit one can consider the machine word.Note:We will assume, that the memory used for keeping the inputdata is not considered because usually arrays (and othercompound types) are passed as arguments to functions byreference, which does not involve much memoryIn our example space complexity isconstant - because it consumes memory onlyfor a single variable (plus some xed number of additional temporal variables),independently on the input data size: S(n) const

Omitting Unimportant DetailsAlgorithmsand DataStructuresMarcinSydowimplementation of the algorithmparticular platforms by a constantmultiplicative factor. (e.g. CPU speed)The real time spent by anmay di er betweennotation allowing forexpressing the complexity functions with neglectingunimportant details (as multiplicative or additive constant, forThus, it would be very useful to have aexample)E.g. for the following function:A(n) 2.1 · n 1The most important information is that it is ait's rank of complexity is linearDoes such a notation exist?linear function -

Asymptotic Notation - Big O Algorithmsand DataStructuresMarcinSydowThe notation is called asymptotic notation .There are a couple of avours. The most common is big O :De nitionThe function g(n) isthe upper bound of rank of order of thefunction f(n):f (n) O (g (n)) c n0 n n0 f (n) c · g (n)0The O() notation intuitively corresponds to the symbol (interms of ranks of orders of functions).E.g. the fact that W(n) of our exemplary algorithm has anupper bound of the linear rank can be noted as:W (n ) n 12 O (n)The constant space complexity S(n) of that algorithm can beexpressed with the following special notation:S (n) O (1)

Asymptotic Notation - Big Theta Algorithmsand DataStructuresMarcinSydowAnother important avour of asymptotic notation is big Theta :De nitionthe same rank of order as the functionf (n) Θ(g (n)) f (n) O (g (n)) g (n) O (f (n))The function f(n) hasg(n):TheΘ()notation intuitively corresponds to the symbol (interms of ranks of orders of functions).Notice, thatΘ()is de ned with the use ofO (), similarly as symbol can bede ned with the use of symbol.E.g. the expression:reads as then2f (n ) n n 32 n 3 Θ(n2 )function is ofsquare rank of order.

Other Flavours of Asymptotic NotationAlgorithmsand DataStructuresMarcinSydowWe have 5 relation symbols for comparing numbers: In total, we also have 5 analogous symbols for comparing ranksof functions:1Θ2O - 3Ω4o - 5ω- - - (in general, a capital letter denotes non-sharp inequality and lowercase denotesa sharp one)E.g.:W(n) o(n) (lowercase o)means: the rank of function W(n) is lower than linear

Some Remarks on Using the Asymptotic NotationAlgorithmsand DataStructuresMarcinSydow O(g(n)) Notice: in expressions like f(n)the has a specialmeaning - it does not represent the normal equality. Theexpression has it's meaning only as a whole.E.g. it does not make sense to use asymptotic notation as therst expression on the left-hand side of the symbol.E.g. expressions like O(f(n)) n or O(f(n)) O(g(n)) not make any sensedoBesides the standard usage of the asymptotic notation on theright-hand side of the symbol, it can be also used in thefollowing way:f(n) g(n) O(h(n))Which means: f(n) - g(n) O(h(n))( the ranks of functions f and g di er at most by a rank offunction h )

Remarks: Comparing Ranks of FunctionsAlgorithmsand DataStructuresMarcinSydowSometimes the following technique is useful.Ranks of some 2 functions f(n) and g(n) can be compared bycomputing the following limit:limn gf ((nn))there are 3 possible cases for the limit:12g (n)) (f has higher rank)a positive constant - in that case f(n) Θ(g (n )) (the same - in that case f(n) ω(ranks)3zero - in that case f(n) o(g(n)) ( lowercase o ) (g hashigher rank)

The Most Common Ranks of FunctionsAlgorithmsand DataStructuresMarcinSydowconstant (e.g. S (n) 3 Θ(1))logarithmic (e.g. W (n) 2 lg2 n Θ(log(n)))linear (e.g. A(n) 2n 1 Θ(n))linear-logarithmic (e.g.A(n) 1.44 · n log(n) Θ(n log(n)))square (e.g. W (n) n2 4 Θ(n2 ))cubic (e.g. A(n) Θ(n3 ))log(n))sub-exponential (e.g. A(n ) Θ(n)nexponential (e.g. A(n ) Θ(2 ))factorial (e.g. W (n ) Θ(n !))In simpli cation: in practise, an over-polynomial rank of time complexity isconsidered as unacceptably high In case of space complexity, even linear rank is considered asvery high

Questions/Problems:Algorithmsand DataStructuresMarcinSydowHow to measure the speed of algorithm What 2 things should be determined before startingassessing the time complexity of an algorithmWhat is a dominating operationDe nition of Time Complexity of AlgorithmDe nition of Space Complexity of AlgorithmDe nition of Pessimistic Time ComplexityDe nition of Average Time ComplexityBe able to determine time complexity for simple algorithmsWhat is the purpose of the asymptotic notationDe nition and interpretation of the O() notationDe nitions (and interpretations) of the other types ofasymptotic notationsAbility to express rank of a given function with theasymptotic notation

Algorithmsand DataStructuresMarcinSydowThank you for your attention

Algorithms and Data Structures Marcin Sydow Desired Properties of a Good Algorithm Any good algorithm should satisfy 2 obvious conditions: 1 compute correct (desired) output (for the given problem) 2 be e ective ( fast ) ad. 1) correctness of algorithm ad. 2)complexity of algorithm Complexity of algorithm measures how fast is the algorithm

Related Documents:

CS@VT Data Structures & Algorithms 2000-2021 WD McQuain Course Information 3 CS 3114 Data Structures and Algorithms Advanced data structures and analysis of data structure and algorithm performance. Sorting, searching, hashing, and advanced tree structures and algorithms. File system organization and access methods.

Design and analysis of algorithms with an emphasis on data structures. Approaches to analyzing lower bounds on problems and upper bounds on algorithms. Classical algorithm design techniques including algorithms for sorting, searching, and other operations on data structures such as hash tables, trees, graphs, strings, and advanced data

algorithms are required to effectively use flash memories. These algorithms and data structures support efficient not-in-place updates of data, reduce the number of erasures, and level the wear of the blocks in the device. This survey presents these algorithms and data structures, many of which have only been described in patents until now.

Apr 16, 2009 · 1 Data Structures and Algorithms 3 1.1 A Philosophy of Data Structures 4 1.1.1 The Need for Data Structures 4 1.1.2 Costs and Benefits 6 1.2 Abstract Data Types and Data Structures 8 1.3 Design Patterns 12 1.3.1 Flyweight 13 1.3.2 Visitor 14 1.3.3 Composite 15 1.3.4 Strategy 16 1.4 Problems, Algorith

many algorithms and data structures, especially divide-and-conquer algorithms. Master Theorem The Master Theorem is a shortcut for determining the order of complexity of a recursive algorithm The theorem states that for an algorithm given by : o o o

These lecture notes cover the key ideas involved in designing algorithms. We shall see how they depend on the design of suitable data structures, and how some structures and algorithms are more e cient than others for the same task. We will concentrate on a few basic tasks, such as storing, sorting and searching data, that underlie much of computer science, but the techniques discussed will be .

5. Implementation: Data Structures and Algorithms Each of the four phases of the algorithm relies on the clever application of traditional data structures and algorithms. Considering the above algorithm as the logical “interface” to the problem, the algorithm’s phases are again described below in terms of the solution’s .

FoNS guidelines for writing a final project report July 2012 1 Guidelines for writing a final project report July 2012 FoNS has a strong commitment to disseminating the work of the project teams that we support. Developing and changing practice to improve patient care is complex and we therefore believe it is essential to share the outcomes, learning and experiences of those involved in such .