Algo complexity

1,006 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,006
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
42
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Algo complexity

  1. 1. DefinitionAlgorithm: Steps of problem solving well definedAnalysis:1. Study Performance Characteristics of Algorithms: Time and Space2. Know the existing ways of problem solving3. Evaluate its suitability for a particular problem 1
  2. 2. Properties of AlgorithmAlgorithm:• Precision• Determinism• Finiteness• Correctness• Generality 2
  3. 3. Basics of Studying Algorithmso Independent of Hardwareo Op. Systems, Compilers and Languageso Independent of Data? • Badly chosen Starting Point, Initial Condition • Intelligent selection or well defined start state • Random Selection of an element • Repeat algorithm several times 3
  4. 4. AnalysisComplexity: Goal is to classify algorithms according to their performance characteristics: Time and SpaceThis can be done in two ways:Method 1: Absolute ValueSpace required: Bytes?Time required: Seconds? 4
  5. 5. Computational ComplexitySecond Method: Size of the problem represented by n Independent of machine, operating system, compiler and language used 5
  6. 6. Space ComplexityHow much Space required to execute an algorithm Unit Cost Model for memory representationBegin Initialize n variable n For i=0 to n variable i Print i NextEnd 6
  7. 7. Space ComplexitySpace no longer a problemMany algorithms compromise space requirements since memory is cheapSearch Space grows out of bound! Swapping with secondary memory 7
  8. 8. Time ComplexityTime required: Unit Cost Model for timeBegin Initialize n For i=0 to n Print i End ForEnd 8
  9. 9. Time ComplexityTime required: Unit Cost Model for timeBegin labels do not cost anything Initialize n 1 For i=0 to n n+2 Print i n+1 End For labelEnd label Total T(n) = 2n+4 9
  10. 10. ComplexityAsymptotic Complexity• Ignore machine-dependent constants --- instead of the actual running time• Look at the growth of the running time. 10
  11. 11. Asymptotic ComplexityExample:T(n) = 2n3T(n) = 1000n2Which one is better?For large values of n or small values of n 11
  12. 12. Asymptotic ComplexityWe study algorithms for large values of n but that does not mean that we should forget that high complexity algorithms can execute faster if size of problem is smallWe focus our analysis for large values of n where n → ∞ 12
  13. 13. Kinds of AnalysisWorst-case: (usually)• Max. time of algorithm on any input of size n.Average-case: (sometimes)• expected time over all inputs of size n.• Need assumption of statistical distribution of inputs.• Best-case: (bogus)• Cheat with a slow algorithm that works fast on some input. 13
  14. 14. Analysis of AlgorithmsWorst Case, Best Case, Average Case Example: SortingWhich one to use? For Real Time Applications Data already sorted! Is it really the best case? 14
  15. 15. Mathematical NotationsBig-O: O (f(N) ) is bounded from above at most!Big-Omega: Ω (f(N) ) is bounded from below at least!Little-o: o (f(N) ) is bounded from above smaller than!Little-omega: ω (f(N) ) is bounded from below greater than!Theta: Θ (f(N) ) bounded, above & below equal to! 15
  16. 16. Big-O• O-notation is an upper-bound notation.• It makes no sense to say f(n) is at least O(n2). 16
  17. 17. ComplexityExample: T(n) = 2n+4Dominant Term: As n gets large enough, the dominant term has a much larger effect on the running time of an algorithmExample 2:T(n) = 2n3 + 1000n2 + 100000 17
  18. 18. Dominant TermExample 1: T(n) = 2n+4Example 2: T(n) = 2n3 + 1000n2 + 100000Remove negligible termsExample 1: nExample 2: n3 18
  19. 19. Dominant Term & Big-OExample 1: nExample 2: n3We say that the algorithm runs Ex1: in the Order of n = O(n) Ex2: in the Order of n3 = O(n3)O(n) : The Time Complexity in the worst case, grows with the size of the problem 19
  20. 20. Rules for using Big-O• For a single statement whose execution does not depend on n: O(1) Example: i=0• For a sequence of statement, S1,S2,…,Sk T(S1)+T(S2)+…+T(Sk) Example: i=0 O(1) + print i O(1) 20
  21. 21. Rules for using Big-O• Loop: For i=0 to n S1 S2 End ForDominent Term * No. of times loop executed Example: For i=0 to n j=j*8 j=j-1 End For 21
  22. 22. Rules for using Big-O• Conditional Loop: While i<0 S1 S2 End WhileNo fixed rule, find a bound on the number of iterations 22
  23. 23. Rules for using Big-O• If condition C then S1 else S2 end ifT(C) + max (T(S1) , T(S2) ) 23
  24. 24. Rules for using Big-O• Nested Loop S1 For i=0 to n For j=0 to m S2 End For End ForAs n → ∞, m → ∞ 24
  25. 25. Time ComplexityBegin Initialize n 1 For i=0 to n n+2 For j=0 to m (m+2)(n+2) Print i (m+1)(n+1) End For End ForEnd Total T(n) = 2mn + 3m + 4n + 8 O(n)=n^2 25
  26. 26. Constants?Begin Initialize n For i=0 to n Print i End forEnd What are these?T(n)= 2 n + 4 26
  27. 27. Slope Intercept FormT(n)= 2n+4 18 n T(n) 16 14 1 6 12 2 8 10 8 3 10 6 4 12 4 5 14 2 0 6 16 1 2 3 4 5 6 27
  28. 28. Slope Intercept FormT(n)= 2n+4 C1n + C2 if n = 0 ? n T(n) if C2= 0? 1 6 2 8 18 3 10 16 14 4 12 12 10 5 14 8 6 6 16 4 2 0 1 2 3 4 5 6 28
  29. 29. Growth 29
  30. 30. Growth 30
  31. 31. Order notation – some useful comparisons• O(1) < log n < n < n log n < n2 < n3 < 2n < 3n < n! < nn 31
  32. 32. Order notation – some useful comparisons• Behavior of Growth for Log( number, base) is the same; Log2 n = Log3 n = Log10 n 32
  33. 33. Practical and Un-practical algorithms• Algorithm is practical, if it’s time complexity is T(n) = O(na) (polynomial)• Algorithm is not practical otherwise T(nn) usually is exponential) 33
  34. 34. Example• Assume that we have a record of 1000 students. If we want to search a specific record, what is the time complexity?• If ordered? Fastest Known Sorting Algorithm (n * log n) Quick Sort / Merge Sort Fastest Known Searching Algorithm (log n) Binary Search 34

×