Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: –16:00-17:30 –17:30-19:00 The exam will be 1.5 hours in length. You can attend.

Similar presentations


Presentation on theme: "COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: –16:00-17:30 –17:30-19:00 The exam will be 1.5 hours in length. You can attend."— Presentation transcript:

1 COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: –16:00-17:30 –17:30-19:00 The exam will be 1.5 hours in length. You can attend either block. The exam will be closed book. Please remember to bring ID

2 COSC 3101NJ. Elder Announcements (Cntd…) Tutorial: Dr. Braverman (Instructor for Section M) will lead a tutorial session tomorrow: –Date: Thursday, Jan 22 –Time: 13:00-15:00 –1005 TEL Building This is an opportunity to ask questions

3 COSC 3101NJ. Elder Announcements (Cntd…) I have been making announcements by email. If you have not received an email from me, please send me your current email address. Assignment 1 is now due at 12 noon Mon, Jan 26 My office hour is now Mon 12:30-1:30 The bookstore still has copies of Jeff Edmonds’ notes (shelved with course kits) HTA 6.4 and CLRS 28.2, 30, 31.3-31.9, 33.4 are NOT required reading

4 COSC 3101NJ. Elder Recurrence Relations (Cntd…) The Master Theorem and Examples

5 COSC 3101NJ. Elder

6 COSC 3101NJ. Elder

7 COSC 3101NJ. Elder

8 COSC 3101NJ. Elder

9 COSC 3101NJ. Elder

10 COSC 3101NJ. Elder Sufficiently close to: T(n) = 4T(n/2)+ n 3 = θ(n 3 ). T 2 (n) = ? Evaluating: T 2 (n) = 4 T 2 (n/2+n 1/2 )+ n 3 θ(n 3 ).

11 COSC 3101NJ. Elder Evaluating: T(n) = aT(n-b)+f(n) h = ? n-hb n-ib n-2b n-b T(0) f(n-ib) f(n-2b) f(n-b) f(n) n Work in Level # stack frames Work in stack frame Instance size a aiai a2a2 a 1 n/bn/b a · T(0) a i · f(n-ib) a 2 · f(n-2b) a · f(n-b) 1 · f(n) n/bn/b |base case| = 0 = n-hb h = n / b i 2 1 0 Level Likely dominated by base cases Exponential

12 COSC 3101NJ. Elder Evaluating: T(n) = 1T(n-b)+f(n) h = ? Work in Level # stack frames Work in stack frame Instance size 1 1 1 1 1 f(0) f(n-ib) f(n-2b) f(n-b) f(n) n-b n n-hb n-ib n-2b T(0) f(n-ib) f(n-2b) f(n-b) f(n) h = n / b i 2 1 0 Level Total Work T(n) = ∑ i=0..h f(b·i) = θ(f(n)) or θ(n · f(n))

13 COSC 3101NJ. Elder End of Recurrences

14 COSC 3101NJ. Elder Central Algorithmic Techniques Iterative Algorithms

15 COSC 3101NJ. Elder 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?

16 COSC 3101NJ. Elder Code Representation of an Algorithm Runs on computers Precise and succinct Perception that being able to code is the only thing needed to get a job. (false) I am not a computer I need a higher level of intuition. Prone to bugs Language dependent Pros:Cons:

17 COSC 3101NJ. Elder Two Key Types of Algorithms Iterative Algorithms Recursive Algorithms

18 COSC 3101NJ. Elder Iterative Algorithms Take one step at a time towards the final destination loop (done) take step end loop

19 COSC 3101NJ. Elder 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.

20 COSC 3101NJ. Elder The Getting to School Problem

21 COSC 3101NJ. Elder Problem Specification Pre condition: location of home and school Post condition: Traveled from home to school

22 COSC 3101NJ. Elder General Principle Do not worry about the entire computation. Take one step at a time!

23 COSC 3101NJ. Elder Take a step What is required of this step?

24 COSC 3101NJ. Elder A Measure of Progress 79 km to school 75 km to school

25 COSC 3101NJ. Elder Defining Algorithm Extra requirements 78.999 km 79 km 0 km79 km75 km

26 COSC 3101NJ. Elder Location too confusing for algorithm Algorithm too lazy to define step for every location. Computation Stuck

27 COSC 3101NJ. Elder Algorithm specifies from which locations it knows how to step. Safe Locations

28 COSC 3101NJ. Elder “The computation is presently in a safe location.” Maybe true and maybe not. Loop Invariant

29 COSC 3101NJ. Elder Defining Algorithm From every safe location, define one step towards school.

30 COSC 3101NJ. Elder Take a step What is required of this step?

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

32 COSC 3101NJ. Elder From the Pre-Conditions on the input instance we must establish the loop invariant. Establishing Loop Invariant

33 COSC 3101NJ. Elder Maintain Loop Invariant Can we be assured that the computation will always be in a safe location? By what principle?

34 COSC 3101NJ. Elder Maintain Loop Invariant By Induction the computation will always be in a safe location.

35 COSC 3101NJ. Elder 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. Exit 0 kmExit

36 COSC 3101NJ. Elder Let’s Recap

37 COSC 3101NJ. Elder Designing an Algorithm Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

38 COSC 3101NJ. Elder Simple Example Insertion Sort Algorithm

39 COSC 3101NJ. Elder 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; }}

40 COSC 3101NJ. Elder Higher Level Abstract View Representation of an Algorithm 9 km 5 km

41 COSC 3101NJ. Elder Designing an Algorithm Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

42 COSC 3101NJ. Elder 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. 88 14 98 25 62 52 79 30 23 31 14,23,25,30,31,52,62,79,88,98

43 COSC 3101NJ. Elder 88 14 98 25 62 52 79 30 23 31 14,23,25,30,31,52,62,79,88,98 14 98 25 62 79 30 23,31,52,88 Some subset of the elements are sorted The remaining elements are off to the side. Define Loop Invariant

44 COSC 3101NJ. Elder 14 98 25 62 79 30 23,31,52,88 23 98 25 88 31 30 14,52,62,79 Location Not Determined Which subset of the elements are sorted, is not specified.

45 COSC 3101NJ. Elder Defining Measure of Progress 14 98 25 62 79 30 23,31,52,88 6 elements to school

46 COSC 3101NJ. Elder Define Step Select arbitrary element from side. Insert it where it belongs. 98 25 62 79 23,31,52,88 14 98 25 79 30 23,31,52,62,88 14 30

47 COSC 3101NJ. Elder Making progress while Maintaining the loop invariant 98 25 62 79 23,31,52,88 14 98 25 79 30 23,31,52,62,88 14 30 79 km75 km 5 elements to school 6 elements to school Exit Sorted sublist

48 COSC 3101NJ. Elder 88 14 98 25 62 52 79 30 23 31 88 14 98 25 62 52 79 30 23 31 n elements to school 14,23,25,30,31,52,62,79,88,98 0 elements to school Beginning & Ending Exit 0 kmExit

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

50 COSC 3101NJ. Elder Ok I know you knew Insertion Sort But hopefully you are beginning to appreciate Loop Invariants for describing algorithms

51 COSC 3101NJ. Elder Assertions in Algorithms

52 COSC 3101NJ. Elder Purpose of Assertions Useful for – thinking about algorithms – developing – describing – proving correctness

53 COSC 3101NJ. Elder 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 not negative.

54 COSC 3101NJ. Elder 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.

55 COSC 3101NJ. Elder 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.

56 COSC 3101NJ. Elder 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..

57 COSC 3101NJ. Elder Definition of Correctness &  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.

58 COSC 3101NJ. Elder An Example: A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if …

59 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if Definition of Correctness? …

60 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if any code How is this proved? Definition of Correctness …

61 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if any code Must check 2 r different settings of paths through the code. Is there a faster way? Definition of Correctness …

62 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if Step 1 code ¬ code …

63 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if … Step 2 code ¬ code

64 COSC 3101NJ. Elder A Sequence of Assertions if( ) then code else code end if if( ) then code else code end if Step r code ¬ code …

65 COSC 3101NJ. Elder Another Example: A Loop codeA loop exit when codeB endloop codeC Type of Algorithm: Iterative Type of Assertion: Loop Invariants

66 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Definition of Correctness? Iterative Algorithms Loop Invariants

67 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Definition of Correctness any code How is this proved? Iterative Algorithms Loop Invariants

68 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC The computation may go around the loop an arbitrary number of times. Definition of Correctness any code Is there a faster way? Iterative Algorithms Loop Invariants

69 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Step 0 codeA Iterative Algorithms Loop Invariants

70 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Step 1 ¬ codeB Iterative Algorithms Loop Invariants

71 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Step 2 ¬ codeB Iterative Algorithms Loop Invariants

72 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Step 3 ¬ codeB Iterative Algorithms Loop Invariants

73 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Step i ¬ codeB Iterative Algorithms Loop Invariants All these steps are the same and therefore only need be done once!

74 COSC 3101NJ. Elder codeA loop exit when codeB endloop codeC Last Step codeC Iterative Algorithms Loop Invariants

75 COSC 3101NJ. Elder Partial Correctness Proves that IF the program terminates then it works &  codeC Clean up loose ends ¬ codeB Maintaining Loop Invariant codeA Establishing Loop Invariant Exit

76 COSC 3101NJ. Elder Algorithm Termination 0 km79 km75 km Measure of progress Exit

77 COSC 3101NJ. Elder Algorithm Correctness Partial Correctness Termination Correctness

78 COSC 3101NJ. Elder Designing Loop Invariants Coming up with the loop invariant is the hardest part of designing an algorithm. It requires practice, perseverance, and insight. Yet from it the rest of the algorithm follows easily

79 COSC 3101NJ. Elder Use This Process Don't come up with the loop invariant after the fact to make me happy. Use it to design your algorithm.

80 COSC 3101NJ. Elder Don’t start coding You must design a working algorithm first.

81 COSC 3101NJ. Elder Exemplification: Try solving the problem on small input examples. Rudich www.discretemath.com

82 COSC 3101NJ. Elder Start with Small Steps What basic steps might you follow to make some kind of progress towards the answer? Describe or draw a picture of what the data structure might look like after a number of these steps.

83 COSC 3101NJ. Elder Picture from the Middle Leap into the middle of the algorithm. What would you like your data structure to look like when you are half done?

84 COSC 3101NJ. Elder The Work Completed The loop invariant should state what work has been completed towards solving the problem and what works still needs to be done. 79 km to school 75 km to school

85 COSC 3101NJ. Elder Flow Smoothly The loop invariant should flow smoothly from the beginning to the end of the algorithm. –At the beginning, it should follow easily from the preconditions. –It should progress in small natural steps. –Once the exit condition has been met, the postconditions should easily follow.

86 COSC 3101NJ. Elder Ask for 100% A good philosophy in life is to ask for 100% of what you want, but not to assume that you will get it. –What would you like to be true in the middle of your computation?

87 COSC 3101NJ. Elder Ask for 100% Pretend that a genie has granted your wish. –You are now in the middle of your computation and your dream loop invariant is true.

88 COSC 3101NJ. Elder Ask for 100% Maintain the Loop Invariant: – From here, are you able to take some computational steps that will make progress while maintaining the loop invariant?

89 COSC 3101NJ. Elder Ask for 100% If you can maintain the loop invariant, great. If not, –Too Weak: If your loop invariant is too weak, then the genie has not provided you with everything you need to move on. –Too Strong: If your loop invariant is too strong, then you will not be able to establish it initially or maintain it.

90 COSC 3101NJ. Elder No Assumptions: Suppose a Martian jumped into the top of the loop. All he would know is that was true. It alone must provide enough information so that, after going around the loop, he can establish that the loop invariant is again true.

91 COSC 3101NJ. Elder Know What a LI Is Not ``the LI is not...'' – code –A precondition –A postcondition –A statement that is always true Eg “1+1=2” –The steps taken by the algorithm

92 COSC 3101NJ. Elder Differentiating between Iterations x=x+2 –Meaningful as code –False as a mathematical statement x’ = x i = value at the beginning of the iteration x’’ = x i+1 = new value after going around the loop one more time. x'' = x'+2 –Meaningful as a mathematical statement

93 COSC 3101NJ. Elder Loop Invariants for Iterative Algorithms Three Search Examples

94 COSC 3101NJ. Elder Define Problem: Binary Search PreConditions –Key 25 –Sorted List PostConditions –Find key in list (if there). 356131821 25364349515360727483889195 356131821 25364349515360727483889195

95 COSC 3101NJ. Elder Define Loop Invariant Maintain a sublist Such that key 25 356131821 25364349515360727483889195

96 COSC 3101NJ. Elder Define Loop Invariant Maintain a sublist. If the key is contained in the original list, then the key is contained in the sublist. key 25 356131821 25364349515360727483889195

97 COSC 3101NJ. Elder Define Step Make Progress Maintain Loop Invariant key 25 356131821 25364349515360727483889195

98 COSC 3101NJ. Elder Define Step Cut sublist in half. Determine which half key would be in. Keep that half. key 25 356131821 25364349515360727483889195

99 COSC 3101NJ. Elder Define Step Cut sublist in half. Determine which half the key would be in. Keep that half. key 25 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. mid

100 COSC 3101NJ. Elder Define Step It is faster not to check if the middle element is the key. Simply continue. key 43 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half.

101 COSC 3101NJ. Elder Make Progress The size of the list becomes smaller. 356131821 25364349515360727483889195 356131821 25364349515360727483889195 79 km75 km 79 km75 km Exit

102 COSC 3101NJ. Elder Initial Conditions key 25 356131821 25364349515360727483889195 If the key is contained in the original list, then the key is contained in the sublist. The sublist is the entire original list. n km

103 COSC 3101NJ. Elder Ending Algorithm If the key is contained in the original list, then the key is contained in the sublist. Sublist contains one element. Exit 356131821 25364349515360727483889195 0 km If the key is contained in the original list, then the key is at this location. key 25

104 COSC 3101NJ. Elder If key not in original list If the key is contained in the original list, then the key is contained in the sublist. Loop invariant true, even if the key is not in the list. If the key is contained in the original list, then the key is at this location. 356131821 25364349515360727483889195 key 24 Conclusion still solves the problem. Simply check this one location for the key.

105 COSC 3101NJ. Elder Running Time The sublist is of size n, n / 2, n / 4, n / 8,…,1 Each step  (1) time. Total =  (log n) key 25 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half.

106 COSC 3101NJ. Elder Code

107 COSC 3101NJ. Elder Algorithm Definition Completed Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

108 COSC 3101NJ. Elder Study: Many experienced programmers were asked to code up binary search. 80% got it wrong Good thing is was not for a nuclear power plant.

109 COSC 3101NJ. Elder Make Precise Definitions Maintain a sublist with end points i & j. key 25 ij 356131821 25364349515360727483889195

110 COSC 3101NJ. Elder Make Precise Definitions Maintain a sublist with end points i & j. key 25 ij 356131821 25364349515360727483889195 Does not matter which, but you need to be consistent.

111 COSC 3101NJ. Elder If the sublist has even length, which element is taken to be mid? key 25 356131821 25364349515360727483889195 Make Precise Definitions

112 COSC 3101NJ. Elder If the sublist has even length, which element is taken to be mid? key 25 356131821 25364349515360727483889195 Should not matter Choose right. Make Precise Definitions mid

113 COSC 3101NJ. Elder Common Bugs Cut sublist in half. Determine which half the key would be in. Keep that half. key 25 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. mid

114 COSC 3101NJ. Elder Common Bugs If the middle element is the key, it can be skipped over. key 43 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. Exit mid

115 COSC 3101NJ. Elder Common Bugs Fix the bug. key 43 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. mid

116 COSC 3101NJ. Elder Common Bugs Second fix, by making the left half slightly bigger. key 43 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. New bug? mid

117 COSC 3101NJ. Elder Common Bugs Second fix, by making the left half slightly bigger. key 25 356131821 25364349515360727483889195 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. New bug? Suppose key = 25 mid

118 COSC 3101NJ. Elder 74 Common Bugs Second fix, by making the left half slightly bigger. key 25 95918883726053514943362521 1813653 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. New bug? Suppose key = 25 mid

119 COSC 3101NJ. Elder 1874 Common Bugs Second fix, by making the left half slightly bigger. key 25 95918883726053514943362521 13653 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. New bug? Suppose key = 25 mid

120 COSC 3101NJ. Elder 1374 Common Bugs Second fix, by making the left half slightly bigger. key 25 95918883726053514943362521 18653 If key ≤ mid, then key is in left half. If key > mid, then key is in right half. New bug? Suppose key = 25 No progress toward goal: Loops Forever! mid 79 km75 km Exit

121 COSC 3101NJ. Elder Why is Binary Search so Easy to Get Wrong? 1 2 3 How many possible algorithms? How many correct algorithms? Probability of success by guessing?

122 COSC 3101NJ. Elder Moral Use the loop invariant method to think about algorithms. Be careful with your definitions. Be sure that the loop invariant is always maintained. Be sure progress is always made.

123 COSC 3101NJ. Elder Loop Invariants for Iterative Algorithms A Second Search Example: The Binary Search Tree

124 COSC 3101NJ. Elder 38 25 17 421 31 2835 51 42 4049 63 5571 Define Problem: Binary Search Tree PreConditions –Key 25 –A binary search tree. –PostConditions –Find key in BST (if there).

125 COSC 3101NJ. Elder 38 25 17 421 31 2835 51 42 4049 63 5571 Binary Search Tree Its left children ≤ Any node ≤ Its right children ≤ ≤

126 COSC 3101NJ. Elder Define Loop Invariant Maintain a sub-tree. If the key is contained in the original tree, then the key is contained in the sub-tree. key 17 38 25 17 421 31 2835 51 42 4049 63 5571

127 COSC 3101NJ. Elder Define Step Cut sub-tree in half. Determine which half the key would be in. Keep that half. key 17 38 25 17 421 31 2835 51 42 4049 63 5571 If key < root, then key is in left half. If key > root, then key is in right half. If key = root, then key is found

128 COSC 3101NJ. Elder Algorithm Definition Completed Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

129 COSC 3101NJ. Elder A volunteer, please. Card Trick

130 COSC 3101NJ. Elder Loop Invariants for Iterative Algorithms A Third Search Example: A Card Trick

131 COSC 3101NJ. Elder Pick a Card Done

132 COSC 3101NJ. Elder Loop Invariant: The selected card is one of these.

133 COSC 3101NJ. Elder Which column? left

134 COSC 3101NJ. Elder Loop Invariant: The selected card is one of these.

135 COSC 3101NJ. Elder Selected column is placed in the middle

136 COSC 3101NJ. Elder I will rearrange the cards

137 COSC 3101NJ. Elder Relax Loop Invariant: I will remember the same about each column.

138 COSC 3101NJ. Elder Which column? right

139 COSC 3101NJ. Elder Loop Invariant: The selected card is one of these.

140 COSC 3101NJ. Elder Selected column is placed in the middle

141 COSC 3101NJ. Elder I will rearrange the cards

142 COSC 3101NJ. Elder Which column? left

143 COSC 3101NJ. Elder Loop Invariant: The selected card is one of these.

144 COSC 3101NJ. Elder Selected column is placed in the middle

145 COSC 3101NJ. Elder Here is your card. Wow!

146 COSC 3101NJ. Elder Algorithm Definition Completed Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

147 COSC 3101NJ. Elder Ternary Search How many iterations are required to guarantee success? What is the probability of success with just 2 iterations? Loop Invariant: selected card in central subset of cards

148 COSC 3101NJ. Elder Loop Invariants A good way to structure any program: –Store everything you know in a data base. –In the main loop, update this data base while making progress.

149 COSC 3101NJ. Elder The “Partitioning” Problem (used in quicksort) 88 14 98 25 62 52 79 30 23 31 p=52 14 25 30 23 31 88 98 62 79 ≤ 52 ≤ Input: Output:

150 COSC 3101NJ. Elder Define Loop Invariant or 312325983014627988 p=52 ≤ p≤ pp ≤ 312371259830627988 p=52 ≤ p≤ pp ≤

151 COSC 3101NJ. Elder Defining Measure of Progress 4 elements not looked at 312325983014627988 p=52

152 COSC 3101NJ. Elder Define Step Make Progress Maintain Loop Invariant 312325983014627988 p=52 ≤ p≤ pp ≤

153 COSC 3101NJ. Elder Define Step Make Progress Maintain Loop Invariant 312325983014627988 p=52 ≤ p≤ pp ≤ 312314259830627988 p=52 ≤ p≤ pp ≤

154 COSC 3101NJ. Elder Define Step 312325983014627988 p=52 312314259830627988 Four cases 312314259830627988 p=52 312314259830627988 312325983071627988 p=52 312325983071627988 312371259830627988 p=52 312325983071627988

155 COSC 3101NJ. Elder 88 14 98 25 62 52 79 30 23 31 Beginning & Ending Exit 0 kmExit p=52 882531986214307923 3025311462987988 p=52 25 30 31 14 23 62 79 98 88 ≤ 52 ≤ n-1 elements not looked at 0 elements not looked at

156 COSC 3101NJ. Elder Running Time Each iteration takes  (1) time. Total =  (n) 312325983014627988 p=52 ≤ p≤ pp ≤ 312314259830627988 p=52 ≤ p≤ pp ≤

157 COSC 3101NJ. Elder Algorithm Definition Completed Define ProblemDefine Loop Invariants Define Measure of Progress Define StepDefine Exit ConditionMaintain Loop Inv Make ProgressInitial ConditionsEnding 79 km to school Exit 79 km75 km Exit 0 kmExit

158 COSC 3101NJ. Elder 23793014629852258831 p=52 237930146298312588 793014629831258823 887930146298312523 887930146298312523 887914629831253023887914629831253023887914629831253023887998146231253023887998621431253023887998621431253023 Tracing an Example

159 COSC 3101NJ. Elder GCD(a,b) Satisified on entry? Maintained? non-negative Success?

160 COSC 3101NJ. Elder End of Iterative Algorithms


Download ppt "COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: –16:00-17:30 –17:30-19:00 The exam will be 1.5 hours in length. You can attend."

Similar presentations


Ads by Google