1.6k likes | 1.61k Views
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.
E N D
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
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
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
Recurrence Relations (Cntd…) The Master Theorem and Examples
Evaluating: T2(n) = 4 T2(n/2+n1/2)+ n3 Sufficiently close to: T(n) = 4T(n/2)+ n3= θ(n3). T2(n) =? θ(n3).
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
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)
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]; while ((j > 0) && (a[j-1] > B)) { a[j] = a[j-1]; j--;} a[j] = B; }} Pros and Cons?
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:
Two Key Types of Algorithms • Iterative Algorithms • Recursive Algorithms
Iterative Algorithms Take one step at a time towards the finaldestination loop (done) take step end loop
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.
Problem Specification • Pre condition: location of home and school • Post condition: Traveled from home to school
General Principle • Do not worry about the entire computation. • Take one step at a time!
Take a step • What is required of this step?
A Measure of Progress 75 km to school 79 km to school
79 km 0 km 79 km 75 km 78.999 km Defining Algorithm • Extra requirements
Computation Stuck • Location too confusing for algorithm • Algorithm too lazy to define step for every location.
Safe Locations • Algorithm specifies from which locations it knows how to step.
Loop Invariant • “The computation is presently in a safe location.” • Maybe true and maybe not.
Defining Algorithm • From every safe location, define one step towards school.
Take a step • What is required of this step?
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?
Establishing Loop Invariant From the Pre-Conditions on the input instance we must establish the loop invariant.
Maintain Loop Invariant • Can we be assured that the computation will always be in a safe location? • By what principle?
Maintain Loop Invariant • By Induction the computation will always be in a safe location.
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.
Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit
Simple Example Insertion Sort Algorithm
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; }}
9 km 5 km Higher Level Abstract ViewRepresentation of an Algorithm
Designing an Algorithm Exit Exit Exit 0 km Exit 79 km 75 km 79 km to school Exit
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.
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.
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.
6 elements to school 30 14 62 25 23,31,52,88 98 79 Defining Measure of Progress
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
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
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
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
OkI know you knew Insertion Sort But hopefully you are beginning to appreciate Loop Invariants for describing algorithms