midterm review
play

Midterm Review Tyler Moore CSE 3353, SMU, Dallas, TX , 2013 - PDF document

Notes Midterm Review Tyler Moore CSE 3353, SMU, Dallas, TX , 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see


  1. Notes Midterm Review Tyler Moore CSE 3353, SMU, Dallas, TX , 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see http://www.cs.sunysb.edu/~skiena/ Administrivia Notes Extra office hours next week Monday 12:30pm-1:30pm Monday 5-5:30pm You may pick up graded HW2 and answer key then Midterm next Tuesday March 5 You may use one side of 1/2 sheet of letter paper for handwritten notes No calculators Review today 2 / 34 Defining bounding functions Notes f ( n ) = O ( g ( n )) means c · g ( n ) is an upper bound on f ( n ). Thus there exists some constant c such that f ( n ) is always ≤ c · g ( n ) for n ≥ n o for some constant n 0 . f ( n ) = Ω( g ( n )) means c · g ( n ) is a lower bound on f ( n ). Thus there exists some constant c such that f ( n ) is always ≥ c · g ( n ) for n ≥ n o . for some constant n 0 . f ( n ) = Θ( g ( n )) means c 1 · g ( n ) is an upper bound on f ( n ) and c 2 · g ( n ) is a lower bound on f ( n ). Thus there exists some constant c 1 and c 2 such that f ( n ) ≤ c 1 · g ( n ) and f ( n ) ≥ c 2 · g ( n ) for n ≥ n o for some constant n 0 . 3 / 34 Dominance and little oh Notes g ( n ) We say that f ( n ) dominates g ( n ) if lim n →∞ f ( n ) = 0. Otherwise f ( n ) does not dominate g ( n ). We say that f ( n ) = o ( g ( n )) ⇐ ⇒ g ( n ) dominates f ( n ) So n 2 = o ( n 3 ) since n 3 dominates n 2 . 4 / 34

  2. Dominance examples Notes f ( n ) = n 2 , g ( n ) = n . Does f ( n ) dominate g ( n )? Is n 2 o ( n )? Is n o ( n 2 )? Is n o ( n )? 5 / 34 Dominance relations Notes You should come to accept the dominance ranking of the basic functions: n ! ≫ 2 n ≫ n 3 ≫ n 2 ≫ n log n ≫ n ≫ √ n ≫ log n ≫ 1 6 / 34 Discussion on Analysis Notes If the question asks you to explain why f ( n ) = Ω( g ( n )), etc., then provide a value for c and n 0 where the relationship holds, perhaps doing a bit of algebra to make the point clear. Best-case/worst-case/average-case does not correspond to Oh/Omega/Theta. Why do we see each of the following cost functions? (log n , n , n 2 , 2 n , n !) 7 / 34 Useful rules on exponents and logarithms Notes log a ( xy ) = log a ( x ) + log a ( y ) log a ( b ) = log c b log c a log a ( n b ) = b · log a ( n ) x a x b = x a + b x a x b = x a − b x a · y a = ( xy ) a x a y a = ( x y ) a 8 / 34

  3. Python Notes If you have to write code on the exam, in most circumstances pseudo-code or Python is fine There are a few Python-specific points worth noting Python’s handling of mutable/immutable objects List comprehensions You do NOT need to worry about user-defined classes in Python (e.g., the code I showed on Binary Search Trees) 9 / 34 Variables in Python Notes Better thought of as names or identifiers attached to an object. A nice explanation: http://python.net/~goodger/projects/pycon/2007/ idiomatic/handout.html#other-languages-have-variables 10 / 34 Key distinction: mutable vs. immutable objects Notes Immutable: objects whose value cannot change Tuples (makes sense) 1 Booleans (surprise?) 2 Numbers (surprise?) 3 Strings (surprise?) 4 Mutable: objects whose value can change Dictionaries 1 Lists 2 User-defined objects (unless defined as immutable) 3 This distinction matters because it explains seemingly contradictory behavior 11 / 34 Variable assignment in action Notes >>> #variables are really names ... c = 4 >>> d = c >>> c+=1 >>> c 5 >>> d #d does not change because numbers are immutable 4 >>> #lists are mutable ... a = [1,4,2] >>> b = a #so this assigns the name b to the object attached to name a >>> a.append(3) >>> a [1, 4, 2, 3] >>> b #b still points to the same object, its contents have just changed. [1, 4, 2, 3] 12 / 34

  4. Im/mutablility and function calls Notes >>> #let’s try this in a function ... def increment(n): #n is a name assigned to the function argument when called ... #because numbers are immutable, the following ... #reassigns n to the number represented by n+1 ... n+=1 ... return n ... >>> a = 3 >>> increment(a) 4 >>> #a does not change ... a 3 13 / 34 Im/mutablility and function calls Notes >>> def sortfun(s): ... s.sort() ... return s ... >>> def sortfun2(s): ... l = list(s) ... l.sort() ... return l ... >>> a = [1,4,2] >>> sortfun(a) [1, 2, 4] >>> a [1, 2, 4] >>> b = [3,9,1] >>> sortfun2(b) [1, 3, 9] >>> b [3, 9, 1] 14 / 34 Im/mutablility and function calls Notes def selection_sort(s): """ Input: list s to be sorted Output: sorted list """ for i in range(len(s)): #don’t name min since reserved word minidx=i for j in range(i+1,len(s)): if s[j]<s[minidx]: minidx=j s[i],s[minidx]=s[minidx],s[i] return s >>> b [3, 9, 1] >>> selection_sort(b) [1, 3, 9] >>> b [1, 3, 9] 15 / 34 List comprehensions Notes Recall set-builder notation from Discrete Math: S = { 3 x | x ∈ N , x > 5 } We can approximate that in Python > range (1 ,11) > > [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10] > { 3 ∗ x f o r x i n range (1 ,11) i f x > 5 } > > s e t ( [ 2 4 , 18 , 27 , 21 , 3 0 ] ) > [3 ∗ x f o r x i n range (1 ,11) i f x > 5] > > [18 , 21 , 24 , 27 , 30] Comprehensions arise in very common coding scenarios 16 / 34

  5. List comprehensions Notes Here’s a common coding task: iterate over some list, perform some action on each element of that list, and store the results in a new list. Here’s an example: > c h e e s e s = [ ’ s w i s s ’ , ’ g r u y e r e ’ , ’ cheddar ’ , ’ s t i l t o n ’ , > > ’ r o q u e f o r t ’ , ’ b r i e ’ ] > c h e e s e l e n =[] > > > f o r c i n c h e e s e s : > > . . . c h e e s e l e n . append ( l e n ( c )) . . . > c h e e s e l e n > > [ 5 , 7 , 7 , 7 , 9 , 4] We can do this on a single line: c h e e s e l e n =[ l e n ( c ) c c h e e s e s ] f o r i n > c h e e s e l e n > > [ 5 , 7 , 7 , 7 , 9 , 4] 17 / 34 List comprehensions Notes But wait, there’s more! Suppose you only want to add items to the list if they meet a certain condition, say if the item begins with the letter s. Well here’s the long way: > s c h e e s e l e n =[] > > > f o r c i n c h e e s e s : > > . . . c[0]== ’ s ’ : i f . . . s c h e e s e l e n . append ( l e n ( c ) ) . . . > s c h e e s e l e n > > [ 5 , 7] You can add a condition at the end of the list comprehension: c h e e s e l e n =[ l e n ( c ) f o r c i n c h e e s e s i f c[0]==” s ” ] > s c h e e s e l e n > > [ 5 , 7] 18 / 34 More list comprehension examples Notes 1 > > s p o r t s > 2 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 3 > > [ s s s p o r t s l e n ( s ) > 8] f o r i n i f > 4 [ ’ i c e hockey ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ ] 5 > > [ s f o r s i n s p o r t s i f ’ b a l l ’ i n s ] > 6 [ ’ f o o t b a l l ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ ] 7 8 > > s p o r t s l o c > 9 [ ( ’ f o o t b a l l ’ , ’ out ’ ) , ( ’ t e n n i s ’ , ’ both ’ ) , ( ’ i c e hockey ’ , ’ i n ’ ) , ( ’ l a c r o s s e ’ , ’ out ’ ) , ( ’ f i e l d hockey ’ , ’ out ’ ) , ( ’ b a s k e t b a l l ’ , ’ i n ’ ) , ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 10 > > [ s [ 0 ] f o r s i n s p o r t s l o c ] > 11 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 12 > > o u t d o o r s p o r t s =[ s [ 0 ] f o r s i n s p o r t s l o c i f s [1]== ’ out ’ ] > 13 > > o u t d o o r s p o r t s > 14 [ ’ f o o t b a l l ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s e b a l l ’ ] 19 / 34 List comprehension exercise Notes Write a function that squares each element of a list so long as the elements are positive using a list comprehension. Complete this code: s q u a r e L i s t ( l ) : def return # f i l l i n l i s t comprehension 20 / 34

Recommend


More recommend