1 / 30

Lecture 2

Data Structures and Algorithms. Lecture 2. Last time. Machine model assumed Instructions are executed one after another, with no concurrent operations  Not parallel computers Factors affecting the running time computer compiler algorithm used input to the algorithm. 1. Last time.

bleak
Download Presentation

Lecture 2

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. Data Structures and Algorithms Lecture 2

  2. Last time • Machine model assumed • Instructions are executed one after another, with no concurrent operations  Not parallel computers • Factors affecting the running time • computer • compiler • algorithm used • input to the algorithm 1

  3. Last time • Machine model assumed • Instructions are executed one after another, with no concurrent operations  Not parallel computers • Factors affecting the running time • computer • compiler • algorithm used • input to the algorithm 2

  4. Last time ( count.) input size running time Number of times basic operation is executed execution time for basic operation 3 Program = algorithms + data structures T(n) ≈copC(n)

  5. example void bubbleSort(int arr[], int n) {       bool swapped = true;       int j = 0;       int tmp;       while (swapped) {             swapped = false;             j++;             for (int i = 0; i < n - j; i++) {                   if (arr[i] > arr[i + 1]) {                         tmp = arr[i];                         arr[i] = arr[i + 1];                         arr[i + 1] = tmp;                         swapped = true;                   }             }       } }

  6. Best-case,average-case,worst-case 5 For some algorithms efficiency depends on form of input: • Worst case: Cworst(n) – maximum over inputs of size n • Best case: Cbest(n) – minimum over inputs of size n • Average case: Cavg(n) – “average” over inputs of size n • Number of times the basic operation will be executed on typical input • NOT the average of worst and best case • Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs

  7. Input size and basic operation examples 6

  8. Example:Sequentialsearch • Worst case • Best case • Average case 7

  9. Example:Sequentialsearch (Count.) • Worst case The last number  n 8

  10. Example:Sequentialsearch (Count.) • Worst case The last number  n 9

  11. Binary Search Used with a sorted list First check the middle list element If the target matches the middle element, we are done If the target is less than the middle element, the key must be in the first half If the target is larger than the middle element, the key must be in the second half

  12. Binary Search Example

  13. Binary Search Algorithm start = 1 end = N while start ≤ end do middle = (start + end) / 2 switch (Compare(list[middle], target)) case -1: start = middle + 1 break case 0: return middle break case 1: end = middle – 1 break end select end while return 0

  14. Algorithm Review Each comparison eliminates about half of the elements of the list from consideration If we begin with N = 2k – 1 elements in the list, there will be 2k–1 – 1 elements on the second pass, and 2k–2 – 1 elements on the third pass

  15. Worst-Case Analysis In the worst case, we will either find the target on the last pass, or not find the target at all The last pass will have only one element left to compare, which happens when21-1 = 1 If N = 2k– 1, then there must be k = log(N+1) passes

  16. Questions?

  17. Types of formulas for basic operation’s count • Exact formula e.g., C(n) = n(n-1)/2 • Formula indicating order of growth with specific multiplicative constant e.g., C(n) ≈ 0.5 n2 • Formula indicating order of growth with unknown multiplicative constant e.g., C(n) ≈cn2

  18. Order of growth • Most important: Order of growth within a constant multiple as n→∞ • Example: • How much faster will algorithm run on computer that is twice as fast? • How much longer does it take to solve problem of double input size?

  19. Values of some important functions as n  

  20. Asymptotic order of growth A way of comparing functions that ignores constant factors and small input sizes • O(g(n)): class of functions f(n) that grow no faster than g(n) • Θ(g(n)): class of functions f(n) that grow at same rate as g(n) • Ω(g(n)): class of functions f(n) that grow at least as fast as g(n

  21. Big-oh

  22. Big-omega

  23. Big-theta

  24. Establishing order of growth using the definition Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple),i.e., there exist positive constant c and non-negative integer n0 such that f(n) ≤ c g(n) for every n ≥ n0 Examples: • 10n is O(n2) • 5n+20 is O(n)

  25. Some properties of asymptotic order of growth • f(n)  O(f(n)) • f(n)  O(g(n)) iff g(n) (f(n)) • If f(n)  O(g(n)) and g(n)  O(h(n)) , then f(n)  O(h(n)) Note similarity with a ≤ b • If f1(n)  O(g1(n)) and f2(n)  O(g2(n)) , then f1(n) +f2(n)  O(max{g1(n), g2(n)})

  26. Orders of growth of some important functions • All logarithmic functions loga nbelong to the same class(log n) no matter what the logarithm’s base a > 1 is • All polynomials of the same degree k belong to the same class: aknk + ak-1nk-1 + … + a0  (nk) • Exponential functions an have different orders of growth for different a’s • order log n < order n (>0) < order an < order n! < order nn

  27. Basic asymptotic efficiency classes

  28. Time efficiency of nonrecursive algorithms General Plan for Analysis • Decide on parameter n indicating input size • Identify algorithm’s basic operation • Determine worst, average, and best cases for input of size n • Set up a sum for the number of times the basic operation is executed • Simplify the sum using standard formulas and rules

  29. Useful summation formulas and rules liu1 = 1+1+ ⋯ +1 = u -l + 1 In particular, liu1 = n - 1 + 1 = n  (n) 1ini = 1+2+ ⋯ +n = n(n+1)/2  n2/2  (n2) 1ini2 = 12+22+ ⋯ +n2 = n(n+1)(2n+1)/6  n3/3  (n3) 0inai = 1+ a + ⋯ + an = (an+1 - 1)/(a - 1) for any a  1 In particular, 0in2i = 20 + 21 + ⋯ + 2n = 2n+1- 1  (2n) (ai±bi ) = ai± bi cai = cailiuai = limai+ m+1iuai

  30. Example 1: Maximum element

More Related