Lecture 2: Implementing ADTs

Similar documents
Lecture 2: Stacks and Queues

Lecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Lecture 3: Queues, Testing, and Working with Code

CSE 373: Data Structures and Algorithms. Final Review. Autumn Shrirang (Shri) Mare

Testing and Debugging

Lecture 3: Working with Data Structures

Priority Queues and Heaps

Building Java Programs

Lecture 3: Maps and Iterators

Lecture 1: CSE 373. Data Structures and Algorithms

CSE 143. Lecture 4: Stacks and Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Building Java Programs

Testing and Debugging

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Implementing Hash and AVL

CSC148H Week 3. Sadia Sharmin. May 24, /20

CSE 373: Introduction, ADTs, Design Decisions, Generics. Michael Lee Wednesday Jan 3, 2017

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CS24 Week 4 Lecture 2

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Searching and Sorting

csci 210: Data Structures Stacks and Queues

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Building Java Programs Chapter 13

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CS 221 Review. Mason Vail

CSC148 Week 2. Larry Zhang

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Queue. COMP 250 Fall queues Sept. 23, 2016

182 review 1. Course Goals

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CSC 1052 Algorithms & Data Structures II: Queues

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Name CPTR246 Spring '17 (100 total points) Exam 3

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Stacks and Queues. David Greenstein Monta Vista

CS 206 Introduction to Computer Science II

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Basic Data Structures

Queues. Stacks and Queues

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Summer Final Exam Review Session August 5, 2009

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Basic Data Structures 1 / 24

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

CS : Data Structures

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

What is an algorithm?

CPSC 221: Algorithms and Data Structures Lecture #0: Introduction. Come up and say hello! Fibonacci. Fibonacci. Fibonacci. Fibonacci. (Welcome!

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

COURSE MECHANICS. Welcome! CSE 332 Data Abstractions: Introduction and ADTs. Concise to-do list. Today in Class. Instructor: Kate Deibel

Stacks and Queues

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

What is an algorithm?

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Algorithmic Analysis. Go go Big O(h)!

Data Structures and Algorithms

Algorithmic Complexity

Stacks and queues (chapters 6.6, 15.1, 15.5)

Linked Structures. See Section 3.2 of the text.

Computer Memory. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

CS 206 Introduction to Computer Science II

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

CSE 332: Data Abstractions. Ruth Anderson Spring 2014 Lecture 1

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

DNHI Homework 3 Solutions List, Stacs and Queues

CS171 Midterm Exam. October 29, Name:

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Data Abstraction and Specification of ADTs

CMSC 132: Object-Oriented Programming II. Stack and Queue

Programming II (CS300)

Algorithms: Design & Practice

PA3 Design Specification

IT 4043 Data Structures and Algorithms

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

data structures and algorithms lecture 6

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

LIFO : Last In First Out

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS 106B Lecture 5: Stacks and Queues

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Transcription:

Lecture 2: Implementing ADTs Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

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? Socrative: www.socrative.com Room Name: CSE373 Please enter your name as: Last, First CSE 373 SP 18 - KASEY CHAMPION 2

Announcements/ Questions No overloading, wait for drops Class page to be live tonight Sections start tomorrow CSE 373 SP 18 - KASEY CHAMPION 3

TA Introductions! Ryan Pham Office Hours: Monday 9:30-11:30 Section: Thursday 1:30 Meredith Wu Office Hours: Friday 1:00 3:00pm Section: Thursday 10:30 CSE 373 SP 18 - KASEY CHAMPION 4

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 5

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.4N 3 + 25N 2 + 8N + 17 statements. - We ignore constants like 25 because they are tiny next to N. - The highest-order term (N 3 ) dominates the overall runtime. - We say that this algorithm runs "on the order of" N 3. - or O(N 3 ) for short ("Big-Oh of N cubed") CSE 143 SP 17 ZORA FUNG 6

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(log 2 N) increases slightly Binary search linear O(N) doubles Looping over an array log-linear O(N log 2 N) slightly more than doubles Merge sort algorithm quadratic O(N 2 ) quadruples Nested loops!............ exponential O(2 N ) multiplies drastically Fibonacci with recursion CSE 373 SP 18 - KASEY CHAMPION 7

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 8

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 - LinkedList: 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] 0 1 2 3 4 h e l l o LinkedList<Character> myll = new LinkedList<Character>(); front h o / e l l CSE 373 SP 18 - KASEY CHAMPION 9

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. LinkedList 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. ArrayList Situation #3: Write a data structure that implements the List ADT that will be used to store the set of operations a user does on a document so another developer can implement the undo function. Stack CSE 373 SP 18 - KASEY CHAMPION 10

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"). push pop, peek 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(): true if there are 1 or more items in stack, false otherwise top 3 2 bottom 1 stack CSE 143 SP 17 ZORA FUNG 11

Implementing a Stack with an Array push(3) push(4) pop() push(5) 0 1 2 3 4 3 45 numberofitems = 012 CSE 373 SP 18 - KASEY CHAMPION 12

Review: Generics // 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 { private Object object; public void set(object object) { this.object = object; } public Object get() { return object; } } 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 13

Implementing a Generic Stack CSE 373 SP 18 - KASEY CHAMPION 14

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 remove, peek front back 1 2 3 queue add CSE 143 SP 17 ZORA FUNG 15

Implementing a Queue enqueue(5) enqueue(8) enqueue(9) 0 1 2 3 4 5 8 9 dequeue() numberofitems = 0123 CSE 373 SP 18 - KASEY CHAMPION 16

Circular Queues front back enqueue(5) enqueue(8) enqueue(9) dequeue() 0 1 2 3 4 5 8 9 numberofitems = 0123 CSE 373 SP 18 - KASEY CHAMPION 17

Wrapping Around front back enqueue(7) enqueue(4) enqueue(1) front back 0 1 2 3 4 4 5 9 2 7 numberofitems = 345 0 1 2 3 4 5 6 7 8 9 5 9 2 7 4 1 CSE 373 SP 18 - KASEY CHAMPION 18

TODO list Fill out survey! -Link on class page Class webpage to be live tonight: -Skim through full Syllabus on class web page -Sign up for Piazza -Review 142/143 materials. Materials provided on class webpage CSE 373 SP 18 - KASEY CHAMPION 19