1 / 160

Announcements

The midterm exam for CSE C will be held on Friday, February 27th. It will be a 1.5 hour closed book exam, and students can choose to attend either of the two blocks: 16:00-17:30 or 17:30-19:00.

cleach
Download Presentation

Announcements

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. 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. 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. 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. Recurrence Relations (Cntd…) The Master Theorem and Examples

  5. Evaluating: T2(n) = 4 T2(n/2+n1/2)+ n3 Sufficiently close to: T(n) = 4T(n/2)+ n3= θ(n3). T2(n) =? θ(n3).

  6. Level 0 f(n) 1 1 ·f(n) 1 f(n-b) a a ·f(n-b) 2 n-2b f(n-2b) a2 a2 ·f(n-2b) i n-ib f(n-ib) ai ai ·f(n-ib) n/b n/b n-hb T(0) a a · T(0) Exponential Likely dominated by base cases Evaluating: T(n) =aT(n-b)+f(n) Instance size Work in stack frame # stack frames Work in Level n n-b h = n/b h = ? |base case| = 0 = n-hb h = n/b

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

  8. End of Recurrences

  9. Central Algorithmic Techniques Iterative Algorithms

  10. 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?

  11. 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 Code Representation of an Algorithm Pros: Cons:

  12. Two Key Types of Algorithms • Iterative Algorithms • Recursive Algorithms

  13. Iterative Algorithms Take one step at a time towards the finaldestination loop (done) take step end loop

  14. 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.

  15. The Getting to School Problem

  16. Problem Specification • Pre condition: location of home and school • Post condition: Traveled from home to school

  17. General Principle • Do not worry about the entire computation. • Take one step at a time!

  18. Take a step • What is required of this step?

  19. A Measure of Progress 75 km to school 79 km to school

  20. 79 km 0 km 79 km 75 km 78.999 km Defining Algorithm • Extra requirements

  21. Computation Stuck • Location too confusing for algorithm • Algorithm too lazy to define step for every location.

  22. Safe Locations • Algorithm specifies from which locations it knows how to step.

  23. Loop Invariant • “The computation is presently in a safe location.” • Maybe true and maybe not.

  24. Defining Algorithm • From every safe location, define one step towards school.

  25. Take a step • What is required of this step?

  26. 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?

  27. Establishing Loop Invariant From the Pre-Conditions on the input instance we must establish the loop invariant.

  28. Maintain Loop Invariant • Can we be assured that the computation will always be in a safe location? • By what principle?

  29. Maintain Loop Invariant • By Induction the computation will always be in a safe location.

  30. Exit 0 km Exit 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.

  31. Let’s Recap

  32. Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit

  33. Simple Example Insertion Sort Algorithm

  34. 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; }}

  35. 9 km 5 km Higher Level Abstract ViewRepresentation of an Algorithm

  36. Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit

  37. 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.

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

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

  40. 6 elements to school 30 14 62 25 23,31,52,88 98 79 Defining Measure of Progress

  41. 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

  42. 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

  43. 52 52 88 88 14 14 31 31 62 62 25 25 30 30 Exit Exit 0 km 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

  44. 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

  45. OkI know you knew Insertion Sort But hopefully you are beginning to appreciate Loop Invariants for describing algorithms

More Related