Lecture 2: Implementing ADTs

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Advertisements

Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Abstract Data Types (ADTs) Data Structures The Java Collections API
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Binary search and complexity reading:
April 27, 2017 COSC Data Structures I Review & Final Exam
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Week 15 – Monday.  What did we talk about last time?  Tries.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
COSC160: Data Structures: Lists and Queues
QueueStack CS1020.
Stacks and Queues.
Week 4 - Friday CS221.
September 29 – Stacks and queues
Searching – Linear and Binary Searches
Introduction to complexity
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
COMP 53 – Week Seven Big O Sorting.
CSE 373: Data Structures and Algorithms
Week 15 – Monday CS221.
Cse 373 April 26th – Exam Review.
Data Structures and Algorithms
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
Building Java Programs
Lecture 1: Welcome to CSE 373
Lecture 1: Welcome to CSE 373
CMSC 341 Lecture 5 Stacks, Queues
Lecture 2: Implementing ADTs
Building Java Programs
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Data Structures and Algorithms
Stacks and Queues.
Lecture 3: Queues, Testing, and Working with Code
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
B-Tree Insertions, Intro to Heaps
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Lecture 5: Master Theorem, Maps, and Iterators
Building Java Programs
Building Java Programs
Lecture 15: binary search reading:
Lecture 5: complexity reading:
Implementing Hash and AVL
Stacks and Queues CLRS, Section 10.1.
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
slides created by Marty Stepp
Building Java Programs
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Lecture 2: Stacks and Queues
Building Java Programs
Amortized Analysis and Heaps Intro
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Lecture 4: Introduction to Code Analysis
Stacks, Queues, and Deques
Lecture 3: Maps and Iterators
Lecture 2: Stacks and Queues
Stacks and Queues.
Presentation transcript:

Lecture 2: Implementing ADTs Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Warm Up – Discuss with your neighbors! From last lecture: What is an ADT? What is a data structure? From CSE 143: What is a “linked list” and what operations is it best at? What is a “stack” and what operations is it best at? What is a “queue” and what operations is it best at? Reasons to discuss: Easier than talking to class Gives everyone a chance to participate Every time you re-apply knowledge it cements better – learn through teaching! Meet your classmates so you know who to partner with! CSE 373 SP 18 - Kasey Champion

Announcements Class Webpage is up Ben’s office hours are set: Tuesday and Thursday 1-3pm in CSE 264 Midterm Date Set: Friday July 20. Contact Ben this week if you have a conflict. Reasons to discuss: Easier than talking to class Gives everyone a chance to participate Every time you re-apply knowledge it cements better – learn through teaching! Meet your classmates so you know who to partner with! CSE 373 SP 18 - Kasey Champion

Design Decisions For every ADT there are lots of different ways to implement them Example: List can be implemented with an Array or a LinkedList Based on your situation you should consider: Memory vs Speed Generic/Reusability vs Specific/Specialized One Function vs Another Robustness vs Performance This class is all about implementing ADTs based on making the right design tradeoffs! > A common topic in interview questions CSE 373 SP 18 - Kasey Champion

Review: “Big Oh” efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc. most commonly refers to run time Assume the following: Any single Java statement takes same amount of time to run. A method call's runtime is measured by the total of the statements inside the method's body. A loop's runtime, if the loop repeats N times, is N times the runtime of the statements in its body. We measure runtime in proportion to the input data size, N. growth rate: Change in runtime as N gets bigger. How does this algorithm perform with larger and larger sets of data? Say an algorithm runs 0.4N3 + 25N2 + 8N + 17 statements. We ignore constants like 25 because they are tiny next to N. The highest-order term (N3) dominates the overall runtime. We say that this algorithm runs "on the order of" N3. or O(N3) for short ("Big-Oh of N cubed") A way of measuring performance without getting hung up on details We’ll get into the details in a later lecture – we’re just covering the basics here so we have a way to talk about tradeoffs CSE 143 SP 17 – Zora Fung

Review: Complexity Class complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N. Class Big-Oh If you double N, ... Example constant O(1) unchanged Accessing an index of an array logarithmic O(log2 N) increases slightly Binary search linear O(N) doubles Looping over an array log-linear O(N log2 N) slightly more than doubles Merge sort algorithm quadratic O(N2) quadruples Nested loops! ... exponential O(2N) multiplies drastically Fibonacci with recursion Like “order of magnitude” for numbers – instead of 10s, 100s, 1000s, we have complexity classes. CSE 373 SP 18 - Kasey Champion

Review: Case Study: The List ADT list: stores an ordered sequence of information. Each item is accessible by an index. Lists have a variable size as items can be added and removed Supported Operations: get(index): returns the item at the given index set(value, index): sets the item at the given index to the given value append(value): adds the given item to the end of the list insert(value, index): insert the given item at the given index maintaining order delete(index): removes the item at the given index maintaining order size(): returns the number of elements in the list CSE 373 SP 18 - Kasey Champion

List ADT tradeoffs Time needed to access i-th element: Array: O(1) constant time LinkedList: O(n) linear time Time needed to insert at i-th element Array: O(n) linear time Amount of space used overall Array: sometimes wasted space LinkedList: compact Amount of space used per element Array: minimal LinkedList: tiny extra char[] myArr = new char[5] 1 2 3 4 ‘h’ ‘e’ ‘l’ ‘o’ LinkedList<Character> myLl = new LinkedList<Character>(); ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ / front CSE 373 SP 18 - Kasey Champion

Thought Experiment Discuss with your neighbors: How would you implement the List ADT for each of the following situations? For each consider the most important functions to optimize. Situation #1: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist. Situation #2: Write a data structure that implements the List ADT that will be used to store the count of students who attend class each day of lecture. Sample solution is that 1: is linked list since you will probably want to insert songs in the middle 2: is array list since you know the exact number of lectures, and you won’t add days However, there is no one “right” answer – it only matters that you have a good reason _why_ you chose the answer that you did. What assumptions did you make, and what Properties did that lead to? CSE 373 SP 18 - Kasey Champion

Review: What is a Stack? stack: A collection based on the principle of adding elements and retrieving them in the opposite order. Last-In, First-Out ("LIFO") Elements are stored in order of insertion. We do not think of them as having indexes. Client can only add/remove/examine the last element added (the "top"). basic stack operations: push(item): Add an element to the top of stack pop(): Remove the top element and returns it peek(): Examine the top element without removing it size(): how many items are in the stack? isEmpty(): false if there are 1 or more items in stack, true otherwise push pop, peek top 3 2 bottom 1 stack CSE 143 SP 17 – Zora Fung

How could you implement a Stack? Discuss with your neighbors! CSE 373 SU 18 – Ben Jones

How could you implement a Stack? CSE 373 SU 18 – Ben Jones

How could you implement a Stack? CSE 373 SU 18 – Ben Jones

Implementing a Stack with an Array push(3) push(4) pop() push(5) 1 2 3 4 numberOfItems = CSE 373 SP 18 - Kasey Champion

Review: Generics // a parameterized (generic) class public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; // a parameterized (generic) class public class name<TypeParameter> { ... } Forces any client that constructs your object to supply a type. Don't write an actual type such as String; the client does that. Instead, write a type variable name such as E (for "element") or T (for "type"). You can require multiple type parameters separated by commas. The rest of your class's code can refer to that type by name. public class Box<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; More details: https://docs.oracle.com/javase/tutorial/java/generics/types.html CSE 373 SP 18 - Kasey Champion

Implementing a Generic Stack Spend 10 minutes implementing a stack CSE 373 SP 18 - Kasey Champion

Review: What is a Queue? queue: Retrieves elements in the order they were added. First-In, First-Out ("FIFO") Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only examine/remove the front of the queue. basic queue operations: add(item): aka “enqueue” add an element to the back. remove(): aka “dequeue” Remove the front element and return. peek(): Examine the front element without removing it. size(): how many items are stored in the queue? isEmpty(): if 1 or more items in the queue returns true, false otherwise front back 1 2 3 remove, peek add queue CSE 143 SP 17 – Zora Fung

How would you implement a Queue? Discuss with your neighbors! Spend rest of class implementing queue – do we need to address circular queues? CSE 373 SU 18 – Ben Jones

How would you implement a Queue? Spend rest of class implementing queue – do we need to address circular queues? CSE 373 SU 18 – Ben Jones

How would you implement a Queue? Spend rest of class implementing queue – do we need to address circular queues? CSE 373 SU 18 – Ben Jones

Circular Queues CSE 373 SU 18 – Ben Jones

Wrapping Around CSE 373 SU 18 – Ben Jones

TODO list Class webpage is live: Skim through full Syllabus on class web page Sign up for Piazza Review 142/143 materials. Materials provided on class webpage Setup Eclipse, try importing Lecture1/Lecture2 projects CSE 373 SU 18 – Ben Jones