Asymptotic Analysis Algorithms and Data Structures Asymptotic Analysis

  • Slides: 40
Download presentation
Asymptotic Analysis ﺗﺤﻠیﻞ ﻣﺠﺎﻧﺒی Algorithms and Data Structures ﺳﺎﺧﺘﻤﺎﻥ ﺩﺍﺩﻩﻫﺎ ﻭ

Asymptotic Analysis ﺗﺤﻠیﻞ ﻣﺠﺎﻧﺒی Algorithms and Data Structures ﺳﺎﺧﺘﻤﺎﻥ ﺩﺍﺩﻩﻫﺎ ﻭ

Asymptotic Analysis Background 2. 3 کﺪﺍﻡ ﺑﻬﺘﺮ ﺍﺳﺖ؟. ﺩﻭ ﺗﺎ ﺍﻟگﻮﺭیﺘﻢ ﺩﺍﺭیﻢ We could

Asymptotic Analysis Background 2. 3 کﺪﺍﻡ ﺑﻬﺘﺮ ﺍﺳﺖ؟. ﺩﻭ ﺗﺎ ﺍﻟگﻮﺭیﺘﻢ ﺩﺍﺭیﻢ We could implement both algorithms, run them both – Expensive and error prone Preferably, we should analyze them mathematically – Algorithm analysis 2

Asymptotic Analysis 2. 3. 1 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢﻫﺎ ﺍﻏﻠﺐ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ ﺭﺍ ﺑﺮ ﺣﺴﺐ یک

Asymptotic Analysis 2. 3. 1 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢﻫﺎ ﺍﻏﻠﺐ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ ﺭﺍ ﺑﺮ ﺣﺴﺐ یک یﺎ چﻨﺪ ، ﺩﺭ ﺣﺎﻟﺖ کﻠی ﻣﺘﻐیﺮ ﺗﺤﻠیﻞ ﻣیکﻨیﻢ ﺍﺟﺎﺯﻩ ﺩﻫیﺪ ﺑﺎ یک ﻣﺘﻐیﺮ ﺷﺮﻭﻉ کﻨیﻢ : – The number of items n currently stored in an array or other data structure – The number of items expected to be stored in an array or other data structure – The dimensions of an n × n matrix ﻣﺜﺎﻟﻬﺎیی ﺍﺯ ﺗﺤﻠیﻞ ﺑﺮ ﺣﺴﺐ چﻨﺪ ﻣﺘﻐیﺮ : – Dealing with n objects stored in m memory locations – Multiplying a k × m and an m × n matrix – Dealing with sparse matrices of size n × n with m non-zero entries 3

Asymptotic Analysis 5 Step Counting ﺷﻤﺎﺭﺵ گﺎﻣﻬﺎ 2. 3. 1 For example, the time

Asymptotic Analysis 5 Step Counting ﺷﻤﺎﺭﺵ گﺎﻣﻬﺎ 2. 3. 1 For example, the time taken to find the largest object in an array of n random integers will take n operations int find_max( int *array, int n ) { 1 int max = array[0]; 2 for ( int i = 1; i < n; ++i ) { ﻫﺮ ﺩﺳﺘﻮﺭ ﺳﺎﺩﻩ ﺩﺭ : 1 ﺳﺎﺩﻩ ﺳﺎﺯی 3 if ( array[i] > max ) { یک ﻭﺍﺣﺪ ﺯﻣﺎﻧی ﺍﻧﺠﺎﻡ ﻣیﺸﻮﺩ 4 max = array[i]; } Lines Cost } return max; } 1 1 2 n 3 n-1 4 n-1 T(n) = 3 n + 1

Asymptotic Analysis 2. 3. 1 Linear/Binary Search ! ﺗﺤﻠیﻞ ﻋﻤﻞ ﺍﺻﻠی کﺎﻓی ﺍﺳﺖ :

Asymptotic Analysis 2. 3. 1 Linear/Binary Search ! ﺗﺤﻠیﻞ ﻋﻤﻞ ﺍﺻﻠی کﺎﻓی ﺍﺳﺖ : 2 ﺳﺎﺩﻩ ﺳﺎﺯی public static int search(int arr[], int x) { int n = arr. length; for(int i = 0; i < n; i++) { if(arr[i] == x) return i; } return -1; } public int binary. Search(int[] a, int x) { int low = 0; int high = a. length - 1; while (low <= high) { int mid = (low + high)/2; if (a[mid] == x) return mid; else if (a[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } 6

Asymptotic Analysis 2. 3. 2 7 Linear and binary search There are other algorithms

Asymptotic Analysis 2. 3. 2 7 Linear and binary search There are other algorithms which are significantly faster as the problem size increases This plot shows maximum and average number of comparisons to find an entry in a sorted array of size n Linear - maximum – Linear search – Binary search Linear - avg n

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ Consider the two functions

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ Consider the two functions f(n) = n 2 and g(n) = n 2 – 3 n + 2 Around n = 0, they look very different 8

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ Yet on the range

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ Yet on the range n = [0, 1000], they are (relatively) indistinguishable: 9

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ The absolute difference is

Asymptotic Analysis 2. 3. 3 Quadratic Growth 2 ﺭﺷﺪ ﺩﺭﺟﻪ The absolute difference is large, for example, f(1000) = 1 000 g(1000) = 997 002 but the relative difference is very small and this difference goes to zero as n → ∞ To find the relative difference between two values, divide the difference by the original value 10

Asymptotic Analysis 2. 3. 3 Polynomial Growth ﺭﺷﺪ چﻨﺪﺟﻤﻠﻪ ﺍی To demonstrate with another

Asymptotic Analysis 2. 3. 3 Polynomial Growth ﺭﺷﺪ چﻨﺪﺟﻤﻠﻪ ﺍی To demonstrate with another example, f(n) = n 6 and g(n) = n 6 – 23 n 5+193 n 4 – 729 n 3+1206 n 2 – 648 n Around n = 0, they are very different 11

Asymptotic Analysis 2. 3. 3 Polynomial Growth ﺭﺷﺪ چﻨﺪﺟﻤﻠﻪ ﺍی Still, around n =

Asymptotic Analysis 2. 3. 3 Polynomial Growth ﺭﺷﺪ چﻨﺪﺟﻤﻠﻪ ﺍی Still, around n = 1000, the relative difference is less than 3% 12

Asymptotic Analysis 2. 3. 3 • Function Growth ﺭﺷﺪ ﺗﺎﺑﻊ 13

Asymptotic Analysis 2. 3. 3 • Function Growth ﺭﺷﺪ ﺗﺎﺑﻊ 13

Asymptotic Analysis 2. 3. 5 Common Function Growths 14

Asymptotic Analysis 2. 3. 5 Common Function Growths 14

Asymptotic Analysis 2. 3. 5 Common Function Growths 15

Asymptotic Analysis 2. 3. 5 Common Function Growths 15

Asymptotic Analysis ﺗﺤﻠیﻞ ﻣﺠﺎﻧﺒی 2. 3. 3 16 Given an algorithm: – We need

Asymptotic Analysis ﺗﺤﻠیﻞ ﻣﺠﺎﻧﺒی 2. 3. 3 16 Given an algorithm: – We need to be able to describe these values mathematically – We need a systematic means of using the description of the algorithm together with the properties of an associated data structure – We need to do this in a machine-independent way For this, we need asymptotic symbols and the associated asymptotic analysis

Asymptotic Analysis 2. 3. 5 18 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی : ﻓﺮﺿیﺎﺕ – Our functions will

Asymptotic Analysis 2. 3. 5 18 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی : ﻓﺮﺿیﺎﺕ – Our functions will describe the time or memory required to solve a problem of size n – We conclude we are restricting ourselves to certain functions: • They are defined for n ≥ 0 • They are strictly positive for all n – In fact, f(n) > c for some value c > 0 – That is, any problem requires at least one instruction and byte • They are increasing (monotonic increasing)

Asymptotic Analysis 2. 3. 5 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی These definitions are often unnecessarily tedious Note,

Asymptotic Analysis 2. 3. 5 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی These definitions are often unnecessarily tedious Note, however, that if f(n) and g(n) are polynomials of the same degree with positive leading coefficients: where 20

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 5 Suppose that f(n) and g(n) satisfy From

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 5 Suppose that f(n) and g(n) satisfy From the definition, this means given c > e > 0 there exists an N > 0 such that That is, whenever n > N 21

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 5 However, the statement says that f(n) =

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 5 However, the statement says that f(n) = Q(g(n)) If where , it follows that f(n) = Q(g(n)) 22

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 6 23 We have a similar definition for

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 6 23 We have a similar definition for O: If where , it follows that f(n) = O(g(n)) There are other possibilities we would like to describe: If , we will say f(n) = o(g(n)) – The function f(n) has a rate of growth less than that of g(n) We would also like to describe the opposite cases: – The function f(n) has a rate of growth greater than that of g(n) – The function f(n) has a rate of growth greater than or equal to that of g(n)

Asymptotic Analysis 2. 3. 7 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی We will at times use five possible

Asymptotic Analysis 2. 3. 7 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی We will at times use five possible descriptions 24

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 7 Graphically, we can summarize these as follows:

Asymptotic Analysis ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی 2. 3. 7 Graphically, we can summarize these as follows: We say if 25

Asymptotic Analysis 2. 3. 8 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی Some other observations we can make are:

Asymptotic Analysis 2. 3. 8 ﻧﻤﺎﺩﻫﺎی ﻣﺠﺎﻧﺒی Some other observations we can make are: f(n) = Q(g(n)) ⇔ g(n) = Q(f(n)) f(n) = O(g(n)) ⇔ g(n) = W(f(n)) f(n) = o(g(n)) ⇔ g(n) = w(f(n)) 26

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation If we look at the

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation If we look at the first relationship, we notice that f(n) = Q(g(n)) seems to describe an equivalence relation: 1. f(n) = Q(g(n)) if and only if g(n) = Q(f(n)) 2. f(n) = Q(f(n)) 3. If f(n) = Q(g(n)) and g(n) = Q(h(n)), it follows that f(n) = Q(h(n)) Consequently, we can group all functions into equivalence classes, where all functions within one class are big-theta Q of each other 27

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation For example, all of n

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation For example, all of n 2 100000 n 2 – 4 n + 19 n 2 + 1000000 323 n 2 – 4 n ln(n) + 43 n + 10 42 n 2 + 32 n 2 + 61 n ln 2(n) + 7 n + 14 ln 3(n) + ln(n) are big-Q of each other E. g. , 42 n 2 + 32 = Q( 323 n 2 – 4 n ln(n) + 43 n + 10 ) 28

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation 29 Recall that with the

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation 29 Recall that with the equivalence class of all 19 -year olds, we only had to pick one such student? Similarly, we will select just one element to represent the entire class of these functions: n 2 – We could chose any function, but this is the simplest

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation The most common classes are

Asymptotic Analysis 2. 3. 8 Notes on Big-Q Relation The most common classes are given names: Q(1) constant Q(ln(n)) logarithmic Q(n) linear Q(n ln(n)) “n log n” Q(n 2) quadratic Q(n 3) cubic 2 n, en, 4 n, . . . exponential 30

Asymptotic Analysis 2. 3. 8 Logarithms and Exponentials 31 Recall that all logarithms are

Asymptotic Analysis 2. 3. 8 Logarithms and Exponentials 31 Recall that all logarithms are scalar multiples of each other – Therefore logb(n)= Q(ln(n)) for any base b Alternatively, there is no single equivalence class for exponential functions: – If 1 < a < b, – Therefore an = o(bn) However, we will see that it is almost universally undesirable to have an exponentially growing function!

Asymptotic Analysis Logarithms and Exponentials 2. 3. 8 Plotting 2 n, en, and 4

Asymptotic Analysis Logarithms and Exponentials 2. 3. 8 Plotting 2 n, en, and 4 n on the range [1, 10] already shows how significantly different the functions grow Note: 210 = 1024 e 10 ≈ 22 026 410 = 1 048 576 32

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o We can show that, for

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o We can show that, for example ln( n ) = o( np ) for any p > 0 Proof: Using Hopital’s rule, we have Conversely, 1 = o(ln( n )) 33

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o Other observations: – If p

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o Other observations: – If p and q are real positive numbers where p < q, it follows that np = o(nq) – For example, matrix-matrix multiplication is Q(n 3) but a refined algorithm is Q(nlg(7)) where lg(7) ≈ 2. 81 – Also, np = o(ln(n)np), but ln(n)np = o(nq) • np has a slower rate of growth than ln(n)np, but • ln(n)np has a slower rate of growth than nq for p < q 34

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o If we restrict ourselves to

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o If we restrict ourselves to functions f(n) which are Q(np) and Q(ln(n)np), we note: – It is never true that f(n) = o(f(n)) – If f(n) ≠ Q(g(n)), it follows that either f(n) = o(g(n)) or g(n) = o(f(n)) – If f(n) = o(g(n)) and g(n) = o(h(n)), it follows that f(n) = o(h(n)) 35

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o Graphically, we can shown this

Asymptotic Analysis 2. 3. 9 Some Notes on Little-o Graphically, we can shown this relationship by marking these against the real line 36

Asymptotic Analysis 2. 3. 10 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ 37 We will use asymptotic symbols

Asymptotic Analysis 2. 3. 10 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ 37 We will use asymptotic symbols to describe the complexity of algorithms – E. g. , adding a list of n doubles will be said to be a Q(n) algorithm An algorithm is said to have polynomial time complexity if its runtime may be described by O(nd) for some fixed d ≥ 0 – We will consider such algorithms to be efficient Problems that have no known polynomial-time algorithms are said to be intractable – Traveling salesman problem: find the shortest path that visits n cities – Best run time: Q(n 2 2 n)

Asymptotic Analysis 2. 3. 10 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ In general, you don’t want to

Asymptotic Analysis 2. 3. 10 ﺗﺤﻠیﻞ ﺍﻟگﻮﺭیﺘﻢ ﻫﺎ In general, you don’t want to implement exponential-time or exponential-memory algorithms – Warning: don’t call a quadratic curve “exponential”, either. . . please 38

Asymptotic Analysis Summary In this class, we have: – Introducing asymptotic symbols: o O

Asymptotic Analysis Summary In this class, we have: – Introducing asymptotic symbols: o O Q W w – Discussed how to use these – Looked at the equivalence relations 39

Asymptotic Analysis References Wikipedia, https: //en. wikipedia. org/wiki/Mathematical_induction These slides are provided for the

Asymptotic Analysis References Wikipedia, https: //en. wikipedia. org/wiki/Mathematical_induction These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended. 40