recap running time of function running time of cubic function running time of quadratic function...

33
Lecture 18

Upload: kelly-lewis

Post on 19-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

Checking an Algorithm Analysis Once we have performed an algorithm analysis, we want to determine whether it is correct and as good we can possibly make it One way to do so is to code the program and see if the empirically observed running time matches the running time predicted by the analysis

TRANSCRIPT

Recap Running Time of Function Running Time of Cubic Function Running Time of Quadratic Function Running Time of Linear Function Running Time of Logarithmic Function Logarithm Bits in Binary Numbers Repeated Doubling Repeated Halving Static Searching Problem Sequential Search Binary Search Interpolation Search Checking an Algorithm Analysis Once we have performed an algorithm analysis, we want to determine whether it is correct and as good we can possibly make it One way to do so is to code the program and see if the empirically observed running time matches the running time predicted by the analysis Continued. When N increases by a factor of 10, the running time goes up by a factor of 10 for linear programs, 100 for quadratic programs, and 1000 for cubic programs Programs that run in O(N log N) take slightly more than 10 times as long to run under the same circumstances These increases can be hard to spot if the lower order terms have relatively large coefficients and N is not large enough An example is the jump from N = 10 to N = 100 in the running time for the various implementations of the maximum contiguous subsequence sum problem Differentiating linear programs from O(N log N) programs, based purely on empirical evidence, also can be very difficult Continued. Another commonly used trick to verify that some program is O(F(N)) is to compute the values T(N)/F(N) for a range of N, where T(N) is the empirically observed running time If F(N) is a tight answer for the running time, the computed values converge to a positive constant If F(N) is an overestimate, the values converge to zero If F(N) is an underestimate, and hence wrong, the values diverge Example Empirical running time for N binary searches in an N-item array NCPU Time T (millisecond) T/NT/(N log N) 10, , , , , , , Continued. Note in particular that we do not have definitive convergence One problem is that the clock that we used to time the program ticks only every 10 ms Note also that there is no great difference between O(N) and O(N log N) Certainly an O(N log N) algorithm is much closer to being linear than being quadratic Finally, note that the machine in this example has enough memory to store 640,000 objects If your machine does not have this much memory, you will not be able to reproduce these results Limitations of Big-Oh Analysis Big-Oh analysis is a very effective tool, but it does have limitations Its use is not appropriate for small amounts of input For small amounts of input, use the simplest algorithm Also, for a particular algorithm, the constant implied by the Big-Oh may be too large to be practical For example: if one algorithm's running time is governed by the formula 2N log N and another has a running time of 1000N, the first algorithm would most likely be better, even though its growth rate is larger Continued. Large constants can come into play when an algorithm is excessively complex They also come into play because our analysis disregards constants and thus cannot differentiate between things like memory access and disk access Our analysis assumes infinite memory, but in applications involving large data sets, lack of sufficient memory can be a severe problem Continued. Sometimes, even when constants and lower order terms are considered, the analysis is shown empirically to be an overestimate In this case, the analysis needs to be tightened. Or the average- case running time bound may be significantly less than the worst-case running time bound, and so no improvement in the bound is possible For many complicated algorithms the worst-case bound is achievable by some bad input, but in practice it is usually an overestimate Two examples: Sorting algorithms Shellsort Quicksort Continued. However, worst-case bounds are usually easier to obtain than their average-case counterparts For example: a mathematical analysis of the average-case running time of Shellsort has not been obtained Sometimes, merely defining what average means is difficult We use a worst-case analysis because it is expedient and also because, in most instances, the worst-case analysis is very meaningful In the course of performing the analysis, we frequently can tell whether it will apply to the average case Summary of Chapter In this chapter we introduced algorithm analysis and showed that algorithmic decisions generally influence the running time of a program much more than programming tricks do We also showed the huge difference between the running times for quadratic and linear programs and illustrated that cubic algorithms are, for the most part, unsatisfactory We examined an algorithm that could be viewed as the basis for our first data structure The binary search efficiently supports static operations thereby providing a logarithmic worst-case search Later in the text we examine dynamic data structures that efficiently support updates Common Errors Continued. MATLAB Environment MATLAB opening window MATLAB Windows Command window Command History Workspace Window Current Folder Window Document Window Graphics Window Edit Window Start Button Command Window The command window is located in the center pane of the default view of the MATLAB screen The command window offers an environment similar to a scratch pad Using it allows you to save the values you calculate, but not the commands used to generate those values If you want to save the command sequence, you will need to use the editing window to create an M-file. Both approaches are valuable. Command History The command history window records the commands issued in the command window. When one exit MATLAB, or when one issue the clc command, the command window is cleared However, the command history window retains a list of all your commands. One may clear the command history with the edit menu If you work on a public computer, as a security precaution, MATLABs defaults may be set to clear the history when you exit MATLAB This window is valuable for a number of reasons, among them that it allows one to review previous MATLAB sessions and that it can be used to transfer commands to the command window For example, first clear the contents of the command window by typing clc This action clears the command window but leaves the data in the command history window intact One can transfer any command from the command history window to the command window by double-clicking or by clicking and dragging the line of code into the command window. Workspace Window The workspace window keeps track of the variables one have defined as you execute commands in the command window These variables represent values stored in the computer memory, which are available for one to use If you have been doing the examples, the workspace window should show just one variable, ans, and indicate that it has a value of 25 and is a double array Continued. Set the workspace window to show more about the displayed variables by right clicking on the bar with the column labels Check size and bytes, in addition to name, value, and class Workspace window should now display the following information, although one may need to resize the window to see all the columns: Continued. The array uses 8 bytes of memory MATLAB was written in C, and the class designation tells us that in the C language, ans is a double-precision floating-point array It is enough to know that the variable ans can store a floating point number Actually, MATLAB considers every number you enter to be a floating-point number, whether one insert a decimal point or not Continued. In addition to information about the size of the arrays and type of data stored in them, one can also choose to display statistical information about the data Once again right click the bar in the workspace window that displays the column headings Notice that one can select from a number of different statistical measures, such as the max, min, and standard deviation Examples Continued. clc Command clears the command window, leaving a blank page for to work on However, it does not delete from memory the actual variables created The clear command deletes all of the saved variables The action of the clear command is reflected in the workspace window clear The workspace window is now empty Example of Blank Window Current Folder Window The current folder window lists all the files in the active directory When MATLAB either accesses files or saves information, it uses the current folder unless told differently The default for the location of the current folder varies with version of the software and the way it was installed However, the current folder is listed at the top of the main window The current folder can be changed by selecting another directory from the drop-down list located next to the directory listing or by browsing through your computer files Browsing is performed with the browse button, located next to the drop-down list The Current Folder Window Document Window Double-clicking on any variable listed in the workspace window automatically launches a document window, containing the variable editor Values stored in the variable are displayed in a spread sheet format Values can be changed in the array editor, or new values can be added For example: if the two-dimensional matrix C have not already entered, enter the following command in the command window: C = [ ; ; ]; Placing a semicolon at the end of the command suppresses the output so that it is not repeated in the command window. However, C should now be listed in the workspace window If you double-click on it, a document window will open above the command window, as shown in next Figure More values can be added to the C matrix or change existing values The Document Window displaying the Variable Editor Continued. The document window/variable editor can also be used in conjunction with the workspace window to create entirely new arrays Run the mouse slowly over the icons in the shortcut bar at the top of the workspace window The new variable icon looks like a grid with a large asterisk behind it Select the new variable icon, and a new variable called unnamed should appear on the variable list You can change its name by right-clicking and selecting rename from the pop-up menu To add values to this new variable, double-click on it and add data from the array editor window