1 / 22

CS 302 Data Structures

CS 302 Data Structures. Algorithm Efficiency. This chapter will show you how to analyze the efficiency of algorithms. The basic mathematical techniques for analyzing algorithms are central to more advanced topics in Computer Science.

dyre
Télécharger la présentation

CS 302 Data Structures

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 302 Data Structures Algorithm Efficiency

  2. This chapter will show you how to analyze the efficiency of algorithms. • The basic mathematical techniques for analyzing algorithms are central to more advanced topics in Computer Science. • As examples we will see analysis of some algorithms that you have studied before • In addition, this chapter examines the important topic of sorting data.

  3. Measuring an algorithms efficiency is quite important because your choice of algorithm for a given application often has a great impact. • Suppose two algorithms perform the same task, such as searching. • What does it mean to compare the algorithms and conclude that one is better?

  4. The analysis of algorithms is the area of Computer Science that provides the tools for contrasting the efficiency of different methods of solution. • How do you compare them? • Implement them both in C++ and run them? • There are a few difficulties with this • How are the algorithms coded? • What computer should you use? • What data should the programs use? • To overcome these (and other) difficulties, we will use some mathematical techniques to the algorithms independently of coding, computers, and data.

  5. Execution Time of Algorithm • Traversal of linked nodes – example: • So the total time is A(n+1) + C(n+1) + W(n) • Which is proportional to n • Therefore a list of 100 takes longer than a list of 10

  6. The Towers of Hanoi • In the Chapter on Recursion we proved recursively that the solution to the towers of Hanoi problem with n disks requires 2n-1 moves • If each move requires the same time, m, • the solution requires (2n-1)*m time units. • As you can see, this time requirement increases rapidly as the number of disks increases

  7. Nested Loops • Consider an algorithm that contains nested loops of the following form: for (i=1 to n) for (j=1 to i) for (k=1 to 5) taks T • If task T requires t time units, • the innermost loop (on k) requires 5*t time units, • the loop on j requres5*t*itime units, and • the outermost loop on i requires

  8. Algorithm Growth Rate • The most important thing to learn is how quickly the algorithm’s time requirement grows as a function of the problem size. • Algorithm A requires time proportional to n2 • Algorithm B requires time proportional to n • This is called the growth rate

  9. Analysis and Big O Notation • Definition: • Algorithm A is order f ( n ) • Denoted O( f ( n )) • If constants k and n0 exist • Such that A requires no more than k f ( n ) time units to solve a problem of size n ≥ n0 .

  10. FIGURE 10-2 The graphs of 3  n2 and n2 - 3  n + 10 Analysis and Big O Notation

  11. FIGURE 10-3 A comparison of growth-rate functions: (a) in tabular form Analysis and Big O Notation

  12. Analysis and Big O Notation

  13. FIGURE 10-3 A comparison of growth-rate functions: (a) in graphical form Analysis and Big O Notation

  14. Analysis and Big O Notation • Order of growth of some common functions

  15. Properties of Growth-Rate Functions • You can ignore low order terms in an algorithm’s growth rate function: • O(n3+4n2+3n) is O(n3) • You can ignore a multiplicative constant in the high-order term of an algorithm’s growth rate function • O(5n3) is O(n3) • You can combine growth rate functions • O(f(n)) + O(g(n)) = O(f(n)+g(n)) • O(n2) + O(n) = O(n2+n) = O(n2)

  16. Worst-case and average-case analysis • A particular algorithm might require different times to solve different problems of the same size. • Worst-case analysis concludes that the algorithms is O(f(n)) if, in the worst case, A requires no more time than k*f(n) • Average-case analysis attempts to determine the average amount of time that an algorithm requires to solve problems of size n • This is more difficult than worst-case analysis.

  17. The efficiency of Searching Algorithms • Lets look at two algorithms sequential search and binary search • Sequential Search • Start at the beginning and look until you find it or run out of data. • Best case: • Worst case: • Average case:

  18. The efficiency of Searching Algorithms • Binary Search • Assumes a sorted array. • The algorithm determines which half to look at and ignores the other half. • If n = 2k, there are k divisions • Therefore k = log2n • Thus the algorithm is O(log2n) in the worst case • So, is a binary search better than a sequential search? • If n = 1,000,000 • log2 1,000,000 = 19 • Therefore at most 20 compares as opposed to at most 1,000,000 compares.

  19. Efficiency of Searching Algorithms • Sequential search • Worst case O(n) • Average case O(n) • Best case O(1) • Binary search of sorted array • Worst case O(log2n) • Remember required overhead for keeping array sorted

  20. Keeping Your Perspective • Array-based getEntry is O(1) • Link-based getEntry is O(n) • Consider how frequently particular ADT operations occur in given application • Some seldom-used but critical operations must be efficient

  21. Keeping Your Perspective • If problem size always small, ignore an algorithm’s efficiency • Weigh trade-offs between algorithm’s time and memory requirements • Compare algorithms for both style and efficiency

  22. End of Chapter 10

More Related