Lecture 5: Master Theorem, Data Structures and Maps, and Iterators Algorithms CSE 373 SU 18 β BEN JONES 1
Warmup Draw a tree for this recurrence, and write equations for the recursive and non-recursive work: π π₯βππ π β€ 1 ππ π π π = α π + π π otherwise CSE 373 SP 18 β BEN JONES 2
Warmup π π₯βππ π β€ 1 ππ π π π = α π + π π otherwise CSE 373 SP 18 β BEN JONES 3
Master Theorem Given a recurrence of the following form: π π₯βππ π β€ some constant ππ π π π = α π + π π otherwise Where a, b, c, and d are all constants. The big-theta solution always follows this pattern: If then π π is Ξ π π log π π < π π π is Ξ π π log π If then log π π = π If then π π is Ξ π log π π log π π > π CSE 373 SU 18 - ROBBIE WEBER 4
Apply Master Theorem Given a recurrence of the form: π π₯βππ π β€ some constant a = 2 π π = ππ π π + π π ππ’βππ π₯ππ‘π 1 π₯βππ π β€ 1 b = 2 2π π π π = If then 2 + π ππ’βππ π₯ππ‘π c = 1 π π is Ξ π π log π π < π If π π is Ξ π π log π log π π = π then d = 1 then If log π π = π β log 2 2 = 1 π π is Ξ π log π π log π π > π π π is Ξ π π log 2 π β Ξ π 1 log 2 π CSE 373 SU 18 - ROBBIE WEBER 5
Reflecting on Master Theorem The case log π π < π Given a recurrence of the form: - Recursive case conquers work more quickly than it divides work - Most work happens near βtopβ of tree π π₯βππ π β€ some constant - Non recursive work in recursive case dominates growth, n c term π π = ππ π π + π π ππ’βππ π₯ππ‘π If then π π is Ξ π π log π π < π The case log π π = π If π π is Ξ π π log π then log π π = π - Work is equally distributed across levels of the tree - Overall work is approximately work at any level x height then If π π is Ξ π log π π log π π > π The case log π π > π βπππβπ’ β log π π - Recursive case divides work faster than it conquers work ππ πππβπππ π β π π log π π - Most work happens near βbottomβ of tree πππππππ π β π π log π π - Work at base case dominates. CSE 373 SU 18 - ROBBIE WEBER 6
Announcements Pre-Course Survey Due Tonight! HW1 Due Tonight! Use cse373-staff@cs.washington.edu if you want to e-mail the staff β faster responses than just e-mailing Ben! No class Wed. July 4. Guest lecturer Robbie Webber on Friday, July 6 (I will be out of town Wed. β Sun. with limited internet, so use the staff list for questions) CSE 373 SU 18 β BEN JONES 7
Git β How it Works Your Machine Gitlab Current Code pull commit βheadβ .git folder βheadβ push Code History Code History CSE 373 SU 18 β BEN JONES 8
Git β Playing Nicely With Other Git is designed to work on teams Workflow: You: Commit -> Push -> Partner: Pull (Swap roles and repeat) You should be pair programming, so you should not need to deal with merges If you do run into an issue with merges, talk to a TA and we will teach you more about Git! CSE 373 SU 18 β BEN JONES 9
Project Turn-In HW 1 Due Tonight! Tag with SUBMIT (in all caps) If there is no SUBMIT tag, weβll use whatever was in the master branch on Gitlab tlab as your submission How to use late days: tag it later. We will use the serverβs timestamp of the SUBMIT tag to determine late days. CSE 373 SU 18 β BEN JONES 10
Review: Maps (Dictionaries) map: Holds a set of unique keys and a collection of values , where each key is associated with one value. - a.k.a. "dictionary", "associative array", "hash" key value βat" 43 operation rations: key value βin" 37 key value - put( key , value ): Adds a βyou" 22 mapping from a key to key value a value. βthe" 56 - get get( key ): Retrieves the value mapped to the key. - remove( key ): Removes the given key and its mapped value. 56 map.get("the") CSE 143 SP 17 β ZORA FUNG 11
ArrayDictionary CSE 373 SU 18 β BEN JONES 12
Doubly-Linked List (Deque) CSE 373 SU 18 β BEN JONES 13
Doubly-Linked List (Deque) CSE 373 SU 18 β BEN JONES 14
Traversing Data Array for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } List for (int i = 0; i < myList.size(); i++) { System.out.println(myList.get(i)); } Iterat erator! r! for (T item : list) { System.out.println(item); } CSE 373 SP 18 - KASEY CHAMPION 15
Iterators iterat terator: a Java interface that dictates how a collection of data should be traversed. Behavior aviors: hasNex Next() () β returns true if the iteration has more elements next( t() β returns the next element in the iteration while (iterator.hasNext()) { T item = iterator.next(); } CSE 373 SP 18 - KASEY CHAMPION 16
Iterable Ite terabl able: a Java interface that lets a class be traversed using iterators (for each, etc). Behavior aviors: iterat terator( r() ) β returns an iterator to the class instance CSE 373 SU 18 β BEN JONES 17
Implementing Iterable Demo Implementation for CircularQueue CSE 373 SU 18 β BEN JONES 18
Bonus Slides Amortized Analysis CSE 373 SU 18 β BEN JONES 19
Amortization Whatβs the worst case for inserting into an ArrayList? -O(n). If the array is full. Is O(n) a good description of the worst case behavior? - If youβre worried about a single insertion, maybe. - If youβre worried about doing, say, π insertions in a row. NO! Amortized bounds let us study the behavior of a bunch of consecutive calls. CSE 373 SU 18 - ROBBIE WEBER 20
Amortization The most common application of amortized bounds is for insertions/deletions and data structure resizing. Letβs see why we always do that doubling strategy. How long in total does it take to do π insertions? We might need to double a bunch, but the total resizing work is at most O(n) And the regular insertions are at most π β π 1 = π(π) So π insertions take π(π) work total Or amortized π(1) time. CSE 373 SU 18 - ROBBIE WEBER 21
Amortization Why do we double? Why not increase the size by 10,000 each time we fill up? How much work is done on resizing to get the size up to π ? Will need to do work on order of current size every 10,000 inserts π π 2 10000 10000π β 10,000 β 10,000 2 = π(π 2 ) Ο π=0 The other inserts do π π work total. π 2 The amortized cost to insert is π = π(π) . π Much worse than the π(1) from doubling! CSE 373 SU 18 - ROBBIE WEBER 22
Recommend
More recommend