analysis of algorithms
play

Analysis of Algorithms Data Structures and Algorithms for CL III, WS - PowerPoint PPT Presentation

Department of General and Computational Linguistics Analysis of Algorithms Data Structures and Algorithms for CL III, WS 2019-2020 Corina Dima corina.dima@uni-tuebingen.de M ICHAEL G OODRICH Data Structures & Algorithms in Python R OBERTO T


  1. Department of General and Computational Linguistics Analysis of Algorithms Data Structures and Algorithms for CL III, WS 2019-2020 Corina Dima corina.dima@uni-tuebingen.de

  2. M ICHAEL G OODRICH Data Structures & Algorithms in Python R OBERTO T AMASSIA M ICHAEL G OLDWASSER 1. Python Primer 2. Object-Oriented Programming Analysis of Algorithms | 2

  3. Don’t forget to register – registration closes tonight! https://dsacl3-2019.github.io/ Analysis of Algorithms | 3

  4. M ICHAEL G OODRICH Data Structures & Algorithms in Python R OBERTO T AMASSIA M ICHAEL G OLDWASSER 3. Algorithm Analysis v experimental studies v seven functions v asymptotic analysis Analysis of Algorithms | 4

  5. Input Algorithm Output Analysis of Algorithms | 5

  6. Running Time • The running time of an best case average case algorithm typically grows with worst case the input size. 120 • But may also vary for inputs of 100 Running Time the same size 80 • Running time is influenced by 60 the hardware and software 40 environment 20 0 1000 2000 3000 4000 Input Size Analysis of Algorithms | 6

  7. Experimental Study • Write a program implementing 9000 the algorithm 8000 • Run the program with inputs of 7000 varying size and composition, 6000 Time (ms) recording the time needed 5000 4000 3000 2000 1000 0 • Analyze the results 0 50 100 Input Size or using clock() or the timeit module Analysis of Algorithms | 7

  8. Limitations of Experiments Action Challenge Analysis of Algorithms | 8

  9. Limitations of Experiments Action Challenge Write a program implementing the Algorithm must be fully algorithm implemented before performing an experimental study Analysis of Algorithms | 9

  10. Limitations of Experiments Action Challenge Write a program implementing the Algorithm must be fully algorithm implemented before performing an experimental study Run the program with inputs of Experiments can only be done on a varying size and composition, limited set of inputs recording the time needed Analysis of Algorithms | 10

  11. Limitations of Experiments Action Challenge Write a program implementing the Algorithm must be fully algorithm implemented before performing an experimental study Run the program with inputs of Experiments can only be done on a varying size and composition, limited set of inputs recording the time needed Analyze the results Experimental runs of two different algorithms are difficult to compare directly unless the experiments are performed in the same hardware and software environments Analysis of Algorithms | 11

  12. Beyond Experimental Analysis • An approach to analyzing the efficiency of algorithms that: Can be used to evaluate the relative efficiency of two 1. algorithms independently of the hardware and software environment Can be performed by studying a high-level description of the 2. algorithm (pseudocode), without actually implementing it Takes into account all possible inputs 3. Characterizes running time as a function of the input size, n 4. Analysis of Algorithms | 12

  13. Theoretical Analysis • Perform the analysis directly on a high-level description of the algorithm • Count the number of primitive operations that are executed, and use this number, t , as a measure of the running time of the algorithm Analysis of Algorithms | 13

  14. Primitive Operations • Basic computations performed by an algorithm • Identifiable in pseudocode • Largely independent from the programming language • Assumed to take a constant amount of time in the RAM model Analysis of Algorithms | 14

  15. Examples of Primitive Operations • Assigning an identifier to an object • Determining the object associated with an identifier • Performing an arithmetic operation (e.g. adding two numbers) • Comparing two numbers • Accessing a single element of a list by index • Calling a function • Returning from a function Analysis of Algorithms | 15

  16. Focusing on Worst-Case Input • An algorithm might run faster on some inputs that it does on others of the same size • Express the running time of an algorithm as a function of the input size obtained by taking the average over all possible inputs of the same size • Challenging: requires defining a probability distribution over the set of inputs • Solution: characterize running times in terms of the worst case, as a function of the input size, n, of the algorithm • Easier: only need to identify the worst-case input • Plus: performing well on the worst-case input means that the algorithm needs to do well on every input Analysis of Algorithms | 16

  17. • Associate, with each algorithm, a function f(n) that characterizes the number of primitive operations that are performed as a function of the input size n Analysis of Algorithms | 17

  18. Seven Important Functions in Algorithm Analysis 1. Constant ! " = $ ! " = %&' ( ", b > 1 2. Logarithmic ! " = " 3. Linear ! " = " log " 4. N-log-N ! " = " 0 5. Quadratic ! " = " 1 6. Cubic, other polynomials ! " = 2 3 , 2 > 0 7. Exponential Analysis of Algorithms | 18

  19. The Constant Function • " # = %, '() *(+, '-.,/ 0(1*2312 0 • No matter the n , the function assigns the value c • c is a constant, e.g. c = 5, c = 27, c = 2 56 • But will use typically 7 1 = 1 , given that any other constant function ' 1 = 0 can be written as ' 1 = 07(1) • Simple, but helps characterize the number of steps needed to do a basic operation like adding or comparing two numbers Analysis of Algorithms | 19

  20. The Logarithm Function • " # = %&' ( #, * > 1 • Defined as: - = ./0 1 2 34 526 /2.7 34 * 8 = 2 • By definition, ./0 1 1 = 0 • * is called the base of the logarithm • The most commonly used base is 2: a common operation is to repeatedly divide the input in half Analysis of Algorithms | 20

  21. The Linear Function • " # = # • Given an input value n , assigns the value itself • Arises in algorithm analysis any time we have to do a single operation for each of n elements, e.g. - Comparing a number x to each element of a sequence of size n - Counting the number of elements in a sequence Analysis of Algorithms | 21

  22. The N-log-N Function • " # = # %&' # • Base 2 logarithm • Also called the linearithmic function (Sedgewick & Wayne, 2011) • Grows a little more rapidly than the linear function, and a lot less rapidly than the quadratic function • An n-log-n algorithm is usually preferable to a quadratic algorithm Analysis of Algorithms | 22

  23. The Quadratic Function • " # = # % • Given an input the function assigns the product of n with itself • Appears in the analysis of algorithms because of nested loops, where the inner loop performs a linear number of operations, and the outer loop is performed a linear number of times • Also appears in nested loops where the first iteration uses one operation, the second two operations, the third three operations etc., where the number of operations is * & + = 1 + 2 + 3 + … + 1 − 2 + 1 − 1 + 1 = '() Analysis of Algorithms | 23

  24. The Quadratic Function • " # = # % • Given an input the function assigns the product of n with itself • Appears in the analysis of algorithms because of nested loops, where the inner loop performs a linear number of operations, and the outer loop is performed a linear number of times • Also appears in nested loops where the first iteration uses one operation, the second two operations, the third three operations etc., where the number of operations is * + = 1 + 2 + 3 + … + 1 − 2 + 1 − 1 + 1 = 1(1 + 1) & 2 '() Analysis of Algorithms | 24

  25. The Quadratic Function • " # = # % • Given an input the function assigns the product of n with itself • Appears in the analysis of algorithms because of nested loops, where the inner loop performs a linear number of operations, and the outer loop is performed a linear number of times • Also appears in nested loops where the first iteration uses one operation, the second two operations, the third three operations etc., where the number of operations is * + = 1 + 2 + 3 + … + 1 − 2 + 1 − 1 + 1 = 1(1 + 1) & 2 '() Card Friedrich Gauss, 1777 - 1855 Analysis of Algorithms | 25

  26. The Cubic Function and Other Polynomials • " # = # % • " # = & ' + & ) # + & * # * + & % # % + … + & , # , , where - . , - 0 , - 1 , - 2 , … , - 3 are constants called the coefficients of the polynomial, and - 3 ≠ 0. • 7 indicates the highest power of the polynomial and is called the degree of the polynomial • Examples - 9 : = 2 + 5: + : 1 - 9 : = 1 + : 2 Analysis of Algorithms | 26

  27. The Exponential Function • " # = % # , % > ( • ) is called the base, * is called the exponent • + * assigns to the input n the value obtained by multiplying the base b a total number of n times • Appears in the analysis of algorithms where we have a loop that starts by performing one operation and then e.g. doubles the number of operations performed with each iteration – at the n th iteration the number of operations performed is 2 - . - 2 / = 1 + 2 + 2 5 + … + 2 - . /01 Analysis of Algorithms | 27

Recommend


More recommend