












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Algorithm Analysis
} Time complexity is the execution time it takes for your algorithm to solve a problem. } On other words, time complexity is the number of steps taken by the algorithm, measured with respect to n (input data to be processed) } The content of the input affects the running time } The input size (number of items in the input) is the main consideration } E.g. sorting problem: the number of items to be sorted } E.g. multiply two matrices together: the total number of elements in the two matrices } Machine model assumed Instructions are executed one after another, with no concurrent operations } Not parallel computers
} A function of Fundamental Operations to be processed } f(n)=efficiency } Basic computations performed by an algorithm } Evaluating an expression } Assigning a value to a variable } Indexing into an array } Calling a method } Returning from a method
} Worst-case running time of an algorithm } The longest running time for any input of size n } An upper bound on the running time for any input guarantee that the algorithm will never take longer } Example: Sort a set of numbers in increasing order; and the data is in decreasing order } Best-case running time } sort a set of numbers in increasing order; and the data is already in increasing order } Average-case running time } May be difficult to define what “average” means
} We are not interested in knowing the exact number of operations the algorithm performs. mainly interested in knowing how the number of operations grows with increased input size. } Why? } Given large enough input, the algorithm with faster growth will execute more operations and takes long time } The simplification of efficiency is known as big-O analysis. } We don’t need to determine the complete measure of efficiency, only the factor that determines the magnitude. This factor is the big-O , and expressed as O(n) – that is, on the order of n.
Efficiency Big-O Iterations Estimated Time Logarithmic O(log 2 n) 14 Microseconds Linear O(n) 10,000 0.1 seconds Linear logarithmic O(n(log 2 n)) 140,000 2 seconds Quadratic O(n^2 ) 10,000 15 - 20 minutes Polynomial O(nk) 10,000 Hours Exponential O(cn) 2 10,000^ Intractable Factorial O(n!) 10,000! Intractable *assume instruction speed of 1 microsecond and 10 instructions in loop
} What if we change the incremental condition? Ex: i=i+ iteration Value of i 1 1 2 2 3 3 … … n n i= loop (i<=n) application code i=i+ end loop f(n)=n O(n) n
} Multiply iteration Value of i 1 1 2 2 3 4 4 8 5 16 6 32 7 64 8 128 9 256 10 512 Exit 1024 i= loop (i<n) application code i=i x 2 end loop 2 iteration^ < 1000 f(log 2 n) O(log 2 n) i n
} Iterations = inner loop iterations x outer loop iterations
i= loop (i<=n) j= loop (j<=n) application code j= j x 2 end loop i=i + 1 end loop f(n [log 2 n]) O(n [log 2 n]) log 2 n n
} Quadratic i= loop (i<=10) j= loop (j<=10) application code j= j + end loop i=i + 1 end loop n n O(n^2 ) f(n^2 )
2
2
sum = 0; for( i = 0; i < n; i++) sum = sum + i; sum = 0; for( i = 0; i < n; i++) for( j = 0; j < 2n; j++) sum++; n 2n^2 à O(n) à O(n^2 ) à O(n^2 )
if cond { S1; } else { S2; }