1 / 39

CS 3610/5610N data structures Lecture: complexity analysis

CS 3610/5610N data structures Lecture: complexity analysis. Time matters. To understand a data structure. FIGURE 1-1 Gift shop and each dot representing a house. Algorithm Analysis. Example 50 packages delivered to 50 different houses 50 houses one mile apart, in the same area.

Télécharger la présentation

CS 3610/5610N data structures Lecture: complexity analysis

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. CS 3610/5610N data structuresLecture: complexity analysis Data Structures Using C++ 2E

  2. Time matters Data Structures Using C++ 2E

  3. To understand a data structure Data Structures Using C++ 2E

  4. FIGURE 1-1 Gift shop and each dot representing a house Algorithm Analysis • Example • 50 packages delivered to 50 different houses • 50 houses one mile apart, in the same area Data Structures Using C++ 2E

  5. FIGURE 1-2 Package delivering scheme Algorithm Analysis (cont’d.) • Example (cont’d.) • Driver picks up all 50 packages • Drives one mile to first house, delivers first package • Drives another mile, delivers second package • Drives another mile, delivers third package, and so on • Distance driven to deliver packages • 1+1+1+… +1 = 50 miles • Total distance traveled: 50 + 50 = 100 miles Data Structures Using C++ 2E

  6. FIGURE 1-3 Another package delivery scheme Algorithm Analysis (cont’d.) • Example (cont’d.) • Similar route to deliver another set of 50 packages • Driver picks up first package, drives one mile to the first house, delivers package, returns to the shop • Driver picks up second package, drives two miles, delivers second package, returns to the shop • Total distance traveled • 2 * (1+2+3+…+50) = 2550 miles Data Structures Using C++ 2E

  7. Algorithm Analysis (cont’d.) • Example (cont’d.) • n packages to deliver to n houses, each one mile apart • First scheme: total distance traveled • 1+1+1+… +n = 2n miles • Function of n • Second scheme: total distance traveled • 2 * (1+2+3+…+n) = 2*(n(n+1) / 2) = n2+n • Function of n2 Data Structures Using C++ 2E

  8. TABLE 1-1 Various values of n, 2n, n2, and n2 + n Algorithm Analysis (cont’d.) • Analyzing an algorithm • Count number of operations performed • Not affected by computer speed Data Structures Using C++ 2E

  9. Algorithm Analysis (cont’d.) • Example 1-1 • Illustrates fixed number of executed operations Data Structures Using C++ 2E

  10. Analysis of Example 1-1 • Line 1 has one operation, <<; • Line 2 has two operations; • Line 3 has one operation, >=; • Line 4 has one operation, =; • Line 6 has one operation; and • Line 7 has three operations. • Either Line 4 or Line 6 executes. • Therefore, the total number of operations executed in the preceding code is 1 + 2 + 1 + 1 + 3 = 8. In this algorithm, the number of operations executed is fixed. Data Structures Using C++ 2E

  11. Algorithm Analysis (cont’d.) • Example 1-2 • Illustrates dominant operations Data Structures Using C++ 2E

  12. Analysis of Example 1-2 • Before the while loop: 5 operations (lines 1 – 4) • After the while loop: 9 or 8 operations (depends on whether line 11 or line 13 executes) • The while loop: • 1 operation (line 5) • 4 operations (lines 6-8) • Note: the loop head (line 5) will be executed one more time than the body to terminate the loop. • Therefore, if the loop runs k times, the total number of operations is 5 + (k*5 + 1) + 9 or 5 + (k*5 + 1) + 8.

  13. Algorithm Analysis (cont’d.) • Search algorithm • n: represents list size • f(n): count function • Number of comparisons in search algorithm • c: units of computer time to execute one operation • cf(n): computer time to execute f(n) operations • Constant c depends computer speed (varies) • f(n): number of basic operations (constant) • Determine algorithm efficiency • Knowing how function f(n) grows as problem size grows Data Structures Using C++ 2E

  14. Algorithm Analysis (cont’d.) TABLE 1-2 Growth rates of various functions Data Structures Using C++ 2E

  15. TABLE 1-3 Time for f(n) instructions on a computer that executes 1 billion instructions per second Figure 1-4 Growth rate of functions in Table 1-3 Algorithm Analysis (cont’d.) Data Structures Using C++ 2E

  16. The Big-O Notation • Notation useful in describing algorithm behavior • Shows how a function f(n) grows as n increases without bound • Asymptotic • Study of the function f as n becomes larger and larger without bound • Examples of functions • g(n)=n2 (no linear term) • f(n)=n2 + 4n + 20 Data Structures Using C++ 2E

  17. TABLE 1-4 Growth rate of n2 and n2 + 4n + 20n Algorithm Analysis: The Big-O Notation (cont’d.) • As n becomes larger and larger • Term 4n + 20 in f(n) becomes insignificant • Term n2 becomes dominant term Data Structures Using C++ 2E

  18. Algorithm Analysis: The Big-O Notation (cont’d.) • Algorithm analysis • If function complexity can be described by complexity of a quadratic function without the linear term • We say the function is of O(n2)orBig-O of n2 • Let f and g be real-valued functions • Assume f and g nonnegative • For all real numbers n, f(n) >= 0and g(n) >= 0 • f(n) is Big-O of g(n):written f(n) = O(g(n)) • If there exists positive constants c and n0 such that f(n) <= cg(n) for all n >= n0 Data Structures Using C++ 2E

  19. TABLE 1-5 Some Big-O functions that appear in algorithm analysis Algorithm Analysis: The Big-O Notation (cont’d.) Data Structures Using C++ 2E

  20. Three time complexities Data Structures Using C++ 2E

  21. Running time, cont’d Data Structures Using C++ 2E

  22. Running time of iterative functions • The routine “Example 1-2” is an iterative example • The number of operations can usually be calculated through the summation of the operations within individual steps/stages. Data Structures Using C++ 2E

  23. Recursive example: merge sort

  24. Merge() Data Structures Using C++ 2E

  25. Time complexity of Merge Sort Data Structures Using C++ 2E

  26. Time complexity

  27. Big-O notation: revisit Data Structures Using C++ 2E

  28. Asymptotic notation Data Structures Using C++ 2E

  29. Big-O, Big-Omega, Big-Theta Data Structures Using C++ 2E

  30. Example Data Structures Using C++ 2E

  31. Example, cont’d Data Structures Using C++ 2E

  32. Logarithms Data Structures Using C++ 2E

  33. Some rules about Big-O notations Data Structures Using C++ 2E

  34. Some rules about Big-O notations, cont’d Data Structures Using C++ 2E

  35. Some rules about Big-O notations, cont’d Data Structures Using C++ 2E

  36. Important notation Data Structures Using C++ 2E

  37. Big Omega notation Data Structures Using C++ 2E

  38. Big Theta notation

  39. Big-O, Big-Omega, Big-Theta Data Structures Using C++ 2E

More Related