1 / 33

ANALYSIS OF ALGORITHMS

ANALYSIS OF ALGORITHMS. Data Structures. Algorithms. Definition A step by step procedure for performing some task in a finite amount of time Analysis Methodology for analyzing a “good” algorithm (i.e. Archimedes) Look at Running Time. How are algorithms described ? PSEUDO-CODE.

najwa
Download Presentation

ANALYSIS OF ALGORITHMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ANALYSIS OF ALGORITHMS Data Structures

  2. Algorithms • DefinitionA step by step procedure for performing some task in a finite amount of time • Analysis Methodology for analyzing a “good” algorithm (i.e. Archimedes) • Look at Running Time

  3. How are algorithms described ? PSEUDO-CODE • representation of an algorithm • combines • natural language • familiar programming language structures • facilitates the high-level analysis of an algorithm

  4. Pseudo-Code Conventions • Expressions • Algorithm Structures • Control Structures

  5. Expressions • Standard math symbols + - * / ( ) • Relational operators • Boolean operators and or not • Assignment operator , = • Array indexing A[i]

  6. Algorithm Structure • Algorithm heading Algorithm name(param1, param2,...): Input : input elements Output : output elements • Statements call object.method(arguments) return statement return value control structures

  7. Control Structures • if ... then ... [else ...] • while ... do • repeat ... until ... • for ... do • decision structures • while loops • repeat loops • for loop

  8. General Rules • communicate high level ideas and not implementation details (programming language specifics) • clear and informative

  9. Example - Problem • Problem : Write pseudo-code for an algorithm that will determine the maximum element from a list (array)

  10. Example - Algorithm Algorithm arrayMax(A,n): Input: An array A storing n integers. Output: The maximum element in A. currentMax A[0] for i 1 to n - 1 do if currentMax < A[i] then currentMax A[i] return currentMax

  11. Analysis of Algorithms • Running time • depends on input size n • number of primitive operations performed • Primitive operation • unit of operation that can be identified in the pseudo-code

  12. Primitive Operations-Types • Assigning a value to a variable • Calling a method/procedure • Performing an arithmetic operation • Comparing two numbers • Returning from a method/procedure

  13. Primitive Operations - Some General Rules • for Loops • Nested for Loops • Conditional Statements

  14. for Loops The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations. Example for i 0 to n - 1 do A[i] 0

  15. Nested for Loops Analyze these inside out. The total running time of a statement inside a group of nested for loops is the running time of the statement multiplied by the product of the sizes of all the for loops. Example for i 0 to n - 1 do for j 0 to n - 1 do k++

  16. Conditional Statements if (cond) then S1 else S2 The running time of an if-else statement is never more than the running time of the test plus the larger of the running times of S1 and S2.

  17. Primitive Operations - Example • Count the number of primitive operations in this program : i = 0 a = 0 for i = 1 to n do print i a=a+i return i

  18. Primitive Operations - Example • Estimate : 3n + 3 • Linear Time- (i.e. 6n + 2, 3n+5, n+1,etc.)

  19. Primitive Operations - Example 2 • Count the number of primitive operations in this program : i = 0 a = 0 for i = 1 to n do print i for a = 1 to n do print a return i

  20. Primitive Operations - Example2 • Estimate : n2 + n + 3 • Polynomial (Quadratic) Time

  21. Example 3 - Class Algorithm sum(int n): Input: Integer n Output: Sum of the cubes of 1 to n partial_sum 0 for i 1 to n do partial_sum partial_sum + i*i*i return (partial_sum)

  22. Primitive Operations -Considerations • Arbitrariness of measurement • Worst-case versus average-case (difficult to be exact : range) • Implicit operations • loop control actions • array access

  23. Analysis of Algorithms • take-out the effect of constant factors (i.e. 2n vs 3n : same level) • this can be attributed to differences in hardware and software environments • compare algorithms across types (linear vs polynomial vs exponential) • look at growth rates

  24. Asymptotic Notation • Goal: • To simplify analysis by getting rid of irrelevant information • The “Big-Oh” Notation • Given functions f(n) and g(n), f(n) is O (g(n)) if f(n) <= c g(n) for n>= n0 where c and n0 are constants

  25. Big-Oh Notation • Rule : Drop lower order terms and constant factors 5n + 2 is O (n) 4n3log n + 6n3 + 1 is O (n3logn)

  26. Big-Oh Notation - Relatives • Big Omega (reverse) • Big Theta (both ways, same level) • General Rule : Use the most descriptive notation to describe the algorithm

  27. Practical Significance of Big-Oh Notation • Example for i 0 to n-1 do A[i] 0 • Running time is 2n, if we count assignments & array accesses 3n, if we include implicit loop assignments 4n, if we include implicit loop comparisons • Regardless, running time is O (n)

  28. Algorithms - Common Classifications • Typical functions that classify algorithms constant O (1) logarithmic O (log n) linear O (n) quadratic O (n2) exponential O (an), n > 1

  29. Linear Search vs Binary Search • Linear Search O (n) • Binary Search O (log n) • Example : to search for a number from 1024 entries using the binary search algorithm will only take 10 steps

  30. O(log n) Algorithm

  31. O(log n) Algorithm Binary Search Algorithm bin_search(int a[], int x, int n) Input: Array a, search target x, array size n Output: Position of x in the array (if found)

  32. ` { low 0; high n - 1 while (low <= high) do mid (low + high)/2 if(a[mid] < x) then low mid + 1 else if (a[mid] > x) then high mid - 1 else return (mid) return (NOT_FOUND) }

  33. Prefix Average Problem • Given an array X storing n numbers, we want to compute an array A such that A[1] is the average of elements X[0] … X[I] for I = 0 … n-1. • algorithm A = O (n2) • algorithm B = O (n)

More Related