1 / 177

Central Algorithmic Techniques

Central Algorithmic Techniques. Iterative Algorithms. Code Representation of an Algorithm. class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = 1; i < a.length; i++) { int j = i; int B = a[i];

angelej
Download Presentation

Central Algorithmic Techniques

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. Central Algorithmic Techniques Iterative Algorithms

  2. Code Representation of an Algorithm class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = 1; i < a.length; i++) { int j = i; int B = a[i]; while ((j > 0) && (a[j-1] > B)) { a[j] = a[j-1]; j--;} a[j] = B; }} Pros and Cons? COSC 3101, PROF. J. ELDER

  3. Runs on computers Precise and succinct I am not a computer I need a higher level of intuition. Prone to bugs Language dependent Code Representation of an Algorithm Pros: Cons: COSC 3101, PROF. J. ELDER

  4. Two Key Types of Algorithms • Iterative Algorithms • Recursive Algorithms COSC 3101, PROF. J. ELDER

  5. Iterative Algorithms Take one step at a time towards the finaldestination loop (done) take step end loop COSC 3101, PROF. J. ELDER

  6. Loop Invariants A good way to structure many programs: • Store the key information you currently know in some data representation. • In the main loop, • take a step forward towards destination • by making a simple change to this data. COSC 3101, PROF. J. ELDER

  7. The Getting to School Problem COSC 3101, PROF. J. ELDER

  8. Problem Specification • Pre condition: location of home and school • Post condition: Traveled from home to school COSC 3101, PROF. J. ELDER

  9. General Principle • Do not worry about the entire computation. • Take one step at a time! COSC 3101, PROF. J. ELDER

  10. A Measure of Progress 75 km to school 79 km to school COSC 3101, PROF. J. ELDER

  11. Safe Locations • Algorithm specifies from which locations it knows how to step. COSC 3101, PROF. J. ELDER

  12. Loop Invariant • “The computation is presently in a safe location.” • May or may not be true. COSC 3101, PROF. J. ELDER

  13. Defining Algorithm • From every safe location, define one step towards school. COSC 3101, PROF. J. ELDER

  14. Take a step • What is required of this step? COSC 3101, PROF. J. ELDER

  15. Maintain Loop Invariant • If the computation is in a safe location, it does not step into an unsafe one. • Can we be assured that the computation will always be in a safe location? No. What if it is not initially true? COSC 3101, PROF. J. ELDER

  16. Establishing Loop Invariant From the Pre-Conditions on the input instance we must establish the loop invariant. COSC 3101, PROF. J. ELDER

  17. Maintain Loop Invariant • Can we be assured that the computation will always be in a safe location? • By what principle? COSC 3101, PROF. J. ELDER

  18. Maintain Loop Invariant • By Induction the computation will always be in a safe location. COSC 3101, PROF. J. ELDER

  19. Exit 0 km Exit Exit Ending The Algorithm • Define Exit Condition • Termination: With sufficient progress, the exit condition will be met. • When we exit, we know • exit condition is true • loop invariant is true from these we must establish the post conditions. COSC 3101, PROF. J. ELDER

  20. Let’s Recap COSC 3101, PROF. J. ELDER

  21. Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit COSC 3101, PROF. J. ELDER

  22. Simple Example Insertion Sort Algorithm

  23. Code Representation of an Algorithm class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = 1; i < a.length; i++) { int j = i; int B = a[i]; while ((j > 0) && (a[j-1] > B)) { a[j] = a[j-1]; j--;} a[j] = B; }} COSC 3101, PROF. J. ELDER

  24. 9 km 5 km Higher Level Abstract ViewRepresentation of an Algorithm COSC 3101, PROF. J. ELDER

  25. Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit COSC 3101, PROF. J. ELDER

  26. 52 88 14 14,23,25,30,31,52,62,79,88,98 31 98 25 30 23 62 79 Problem Specification • Precondition: The input is a list of n values with the same value possibly repeated. • Post condition: The output is a list consisting of the same n values in non-decreasing order. COSC 3101, PROF. J. ELDER

  27. 52 88 14 31 62 25 30 23 98 79 30 14 62 25 23,31,52,88 14,23,25,30,31,52,62,79,88,98 98 79 Define Loop Invariant • Some subset of the elements are sorted • The remaining elements are off to the side. COSC 3101, PROF. J. ELDER

  28. 6 elements to school 30 14 62 25 23,31,52,88 98 79 Defining Measure of Progress COSC 3101, PROF. J. ELDER

  29. 30 14 25 23,31,52,88 98 79 Define Step • Select arbitrary element from side. • Insert it where it belongs. 30 14 62 25 98 79 23,31,52,62,88 COSC 3101, PROF. J. ELDER

  30. Exit 79 km 75 km 6 elements to school 5 elements to school 30 14 25 23,31,52,88 98 79 Making progress while Maintaining the loop invariant 30 14 62 25 98 79 23,31,52,62,88 Sorted sublist COSC 3101, PROF. J. ELDER

  31. 52 52 88 88 14 14 31 31 62 62 25 25 30 30 0 km Exit Exit 23 23 98 98 79 79 n elements to school 0 elements to school 14,23,25,30,31,52,62,79,88,98 14,23,25,30,31,52,62,79,88,98 Beginning & Ending COSC 3101, PROF. J. ELDER

  32. 30 14 25 23,31,52,88 98 79 Running Time Inserting an element into a list of size i takes (i) time. Total = 1+2+3+…+n = (n2) 30 14 62 25 98 79 23,31,52,62,88 COSC 3101, PROF. J. ELDER

  33. OkI know you knew Insertion Sort But hopefully you are beginning to appreciate Loop Invariants for describing algorithms COSC 3101, PROF. J. ELDER

  34. Assertions in Algorithms

  35. Purpose of Assertions Useful for • thinking about algorithms • developing • describing • proving correctness COSC 3101, PROF. J. ELDER

  36. Definition of Assertions An assertion is a statement about the current state of the data structure that is either true or false. eg. the amount in your bank account is notnegative. COSC 3101, PROF. J. ELDER

  37. Definition of Assertions It is made at some particular point during the execution of an algorithm. If it is false, then something has gone wrong in the logic of the algorithm. COSC 3101, PROF. J. ELDER

  38. Definition of Assertions An assertion is not a task for the algorithm to perform. It is only a comment that is added for the benefit of the reader. COSC 3101, PROF. J. ELDER

  39. Specifying a Computational Problem Example of Assertions • Preconditions: Any assumptions that must be true about the input instance. • Postconditions: The statement of what must be true when the algorithm/program returns.. COSC 3101, PROF. J. ELDER

  40. Definition of Correctness <PreCond>&<code>Þ <PostCond> If the input meets the preconditions, then the output must meet the postconditions. If the input does not meet the preconditions, then nothing is required. COSC 3101, PROF. J. ELDER

  41. <assertion0> if( <condition1> ) then code<1,true> else code<1,false> end if <assertion1> <assertionr-1> if( <conditionr> ) then code<r,true> else code<r,false> end if <assertionr> An Example:A Sequence of Assertions Definition of Correctness <assertion0> any <conditions> code <assertionr> … How is this proved? • Must check 2r different • settings of <conditions> • paths through the code. Is there a faster way? COSC 3101, PROF. J. ELDER

  42. An Example:A Sequence of Assertions <assertion0> if( <condition1> ) then code<1,true> else code<1,false> end if <assertion1> <assertionr-1> if( <conditionr> ) then code<r,true> else code<r,false> end if <assertionr> Step 1 <assertion0> <condition1> code<1,true> <assertion1> … <assertion0> ¬<condition1> code<1,false> <assertion1> COSC 3101, PROF. J. ELDER

  43. <assertion1> <condition2> code<2,true> <assertion2> An Example:A Sequence of Assertions <assertion0> if( <condition1> ) then code<1,true> else code<1,false> end if <assertion1> <assertionr-1> if( <conditionr> ) then code<r,true> else code<r,false> end if <assertionr> Step 2 … <assertion1> ¬<condition2> code<2,false> <assertion2> COSC 3101, PROF. J. ELDER

  44. An Example:A Sequence of Assertions <assertion0> if( <condition1> ) then code<1,true> else code<1,false> end if <assertion1> <assertionr-1> if( <conditionr> ) then code<r,true> else code<r,false> end if <assertionr> Step r <assertionr-1> <conditionr> code<r,true> <assertionr> … <assertionr-1> ¬<conditionr> code<r,false> <assertionr> COSC 3101, PROF. J. ELDER

  45. A Sequence of Assertions <assertion0> if( <condition1> ) then code<1,true> else code<1,false> end if <assertion1> <assertionr-1> if( <conditionr> ) then code<r,true> else code<r,false> end if <assertionr> Step r <assertionr-1> <conditionr> code<r,true> <assertionr> … <assertionr-1> ¬<conditionr> code<r,false> <assertionr> COSC 3101, PROF. J. ELDER

  46. Another Example:A Loop <preCond> codeA loop <loop-invariant> exit when <exit Cond> codeB endloop codeC <postCond> • Type of Algorithm: • Iterative • Type of Assertion: • Loop Invariants COSC 3101, PROF. J. ELDER

  47. Iterative Algorithms Loop Invariants <preCond> codeA loop <loop-invariant> exit when <exit Cond> codeB endloop codeC <postCond> Definition of Correctness? COSC 3101, PROF. J. ELDER

  48. <preCond> any <conditions> code <postCond> Iterative Algorithms Loop Invariants <preCond> codeA loop <loop-invariant> exit when <exit Cond> codeB endloop codeC <postCond> Definition of Correctness How is this proved? COSC 3101, PROF. J. ELDER

  49. <preCond> any <conditions> code <postCond> Iterative Algorithms Loop Invariants <preCond> codeA loop <loop-invariant> exit when <exit Cond> codeB endloop codeC <postCond> Definition of Correctness The computation may go around the loop an arbitrary number of times. Is there a faster way? COSC 3101, PROF. J. ELDER

  50. Iterative Algorithms Loop Invariants <preCond> codeA loop <loop-invariant> exit when <exit Cond> codeB endloop codeC <postCond> Step 0 <preCond> codeA <loop-invariant> COSC 3101, PROF. J. ELDER

More Related