This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Analysis of Algorithm 2 60 [CS1102C AY0708S2 Lecture 5] Lecture Outline ¡ What is an algorithm? ¡ What is analysis of algorithm? ¡ How to analyze an algorithm ¡ BigO notation ¡ Examples 3 60 [CS1102C AY0708S2 Lecture 5] You are expected to know ¡ Operations on logarithm function ¡ Arithmetic and geometric progressions ¢ Their sums ¡ Linear, Quadratic, Cubic, Polynomial ¡ Ceiling, Floor, Absolute Value 4 60 [CS1102C AY0708S2 Lecture 5] Algorithm and Analysis ¡ Algorithm: ¢ A stepbystep procedure for solving a problem ¡ Analysis of Algorithm: ¢ To evaluate rigorously the resources (time and space) needed by an algorithm and represent the result of the evaluation with a formula ¢ We focus more on time requirement in our analysis ¢ The time requirement of an algorithm is also called the time complexity of the algorithm 5 60 [CS1102C AY0708S2 Lecture 5] Limitation of exact running time ¡ We can measure the exact running time of a program ¢ Use wall clock time or code inserted into program ¡ To compute the exact running time needed by an algorithm, the analysis will depend on : ¢ Language in which the algorithm is coded ¢ Data set : input to the algorithm ¢ Computer that the algorithm is executed on ¡ Such analysis will make it difficult to compare 2 algorithms ¡ It is useful to know the behavior of the algorithm before it is coded / executed ¢ Poorly designed algorithm may take very long to finish execution 6 60 [CS1102C AY0708S2 Lecture 5] How to Analyze an Algorithm? ¡ Instead of measure the exact timing ¢ Count the number of operations needed ¡ Operations: Arithmetic, Assignment, Comparison etc ¢ Usually choose the “dominant” operations ¡ Example: for (int i=1; i<=n; i++) { perform 100 operations; // A for (int j=1; j<=n; j++) perform 2 operations; // B } Total Ops = A + B = Σ {i = 1,n}100 + Σ {i = 1,n} ( Σ {j = 1,n} 2) = 100n + Σ {i = 1,n} (2n) = 100n + 2n 2 = 2n 2 + 100n 7 60 [CS1102C AY0708S2 Lecture 5] Time Requirement ¡ Knowing the number of operations required for an algorithm A, we can state that: ¢ E.g. Algorithm A takes 2n 2 + 100n operations to solve problem of size n ¡ If the time t needed for one operation is known, then we can state: ¢ E.g. Algorithm A takes (2n 2 + 100n)*t time units ¡ However, time t is a directly dependent on the factors mentioned earlier: ¢ Computer and Programming Language ¡ Instead of typing the analysis to actual time t, we can state: ¢ Algorithm A takes time that is proportional to 2n 2 + 100n for solving problem of size n 8 60 [CS1102C AY0708S2 Lecture 5] Approximation of Analysis Results ¡ Suppose the complexity of ¢ Algorithm A is found to be 3n 2 + 2n + log n + 1/4n ¢ Algorithm B is found to be 0.39n 3 + n ¡ Intuitively, we know Algorithm A will outperform B ¢ when solving larger problem i.e. larger n when solving larger problem i....
View Full
Document
 Spring '11
 dunnowho

Click to edit the document details