1 / 17

Analytical Tools

Analytical Tools. Introduction to Algorithms Recipes as Algorithms Analyzing Computer Algorithms Specification of Input Describing Result Proving Termination. Algorithms. An algorithm is a set of instructions used to solve a specific problem Synonyms: Process Method Technique

pierree
Download Presentation

Analytical Tools

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. Analytical Tools • Introduction to Algorithms • Recipes as Algorithms • Analyzing Computer Algorithms • Specification of Input • Describing Result • Proving Termination

  2. Algorithms • An algorithm is a set of instructions used to solve a specific problem • Synonyms: • Process • Method • Technique • Procedure • Routine • Recipe

  3. Properties of Algorithms • In order to be useful, an algorithm must have the following properties: • Accurate specification of the input • Under what conditions is the algorithm expected to give correct answers • Definiteness of each instruction • Each instruction must be clear and unambiguous • Termination • The algorithm must eventually halt for all legal input values • Description of result or effect • It must be clear what the algorithm is intended to do

  4. Recipes as Algorithms • Probably the most common form of algorithm is the recipe: Lemon Souffle 1 envelope unflavored gelatin ¼ cup cold water 6 egg yolks 1 cup sugar 2/3 cup lemon juice 1 tablespoon grated lemon rind 4 egg whites 1.5 cups heavy cream

  5. Recipes as Algorithms (cont) • Soften the gelatin in water. Beat egg yolks and sugar until thick and light. Stir in lemon juice and cook over low heat, beating steadily with a wisk until thickened and hot but nit boiling (10-15 minutes). • Pour mixture into large bowl, mix in gelatin until dissolved then add lemon rind. Stir occasionally until cool. • Beat egg whites until stiff but not dry. Fold into lemon mixture, then whip the cream and fold in. Pour into a two-quart souffle dish and refrigerate at least 12 hours.

  6. Evaluation of Recipe as Algorithm • Input • Ingredients are listed • Result • Indicated by name • Correctness • The proof of the pudding is in the eating • Termination • What if it never boils? Or never becomes light? • Definiteness • What is “light”? How long should it be whipped?

  7. Computer Algorithms • How do computer algorithms differ from instructions for human beings with regard to: • Description of input • Specification of result • Time to execute • Correctness • Instruction precision

  8. Specification of Input • Two most widely-used techniques: • Type declarations for argument values • Comments associated with procedure heading int main (int a, int b) { // returns smaller of two integers int smallest; if (a<b) smallest = a; else smallest = b; return smallest; }

  9. Conditions that are not Types • Occasionally input conditions are not types, but can nevertheless be checked at run-time, using an assert statement: char digitChar (unsigned int val) { // return char equivalent for the integer argument value which must be between 0-9 assert(val < 10); switch (val) { case 0: return ‘0’; case 1: return ‘1’; …. case 9: return ‘9’; } }

  10. Simpler Version • By noting some characteristics of ASCII and that characters are just small integers: char digitChar (unsigned int val) { // return char equivalent for the integer argument value which must be between 0-9 assert(val < 10); Return ‘0’ + val; }

  11. Some Input Conditions cannot be Checked double power (double base, unsigned int n) { // return the value yielded by raising base to // the exponent, n. Assumes that overflow // does not occur double result = 1.0; for (unsigned int i=0;i<n; i++) result *= base; return result; }

  12. Describing the Result • The result of executing the algorithm is often provided • By the return type of a function • By comments int min (int a, int b) // return the smaller of two arguments { …. }

  13. Side Effects • Sometimes algorithms are not executed for their result, but for a side effect. • Common side effects: • Printing a result • Setting or modifying a value in a variable • Global variable, instance variable) • Copying information to a file • Etc. • These should always be documented in a comment, since they are not immediately obvious

  14. Printing Integers • Problem: print a positive integer value on the output stream • General problem solving technique: • Reduce task to a sequence of steps • e.g. a recipe • Reduce a task by considering various cases • e,g a recursive procedure

  15. The Idea Behind a Recursive Procedure • Write code to solve a trivial version of the problem (i.e. the base case) • If the number has only one digit, print that digit • Write code that solves a more complicated version of the problem by: • Making the problem a little bit simpler, and • Calling the recursive procedure to solve the simpler problem (I.e. the recursive call) • A recursive call to print all but the last digit • Print the last digit

  16. A Recursive Procedure void printUnsigned (unsigned int val) { // print the character representation of the unsigned argument if (val < 10) // base case: val is only one digit printChar(digitChar(val)); else { printUnsigned (val /10); // recursive call: // print high order part printChar(digitChar(val % 10)); // print last character } }

  17. The printUnsigned Recursive Procedure • It is relatively easy to see: • The input conditions • The definiteness of the algorithm • Can we show: • Termination? • Correctness? • Execution time?

More Related