Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

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

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

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

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

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

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

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

CSE373: Data Structures and Algorithms Lecture 1: Introduc<on; ADTs; Stacks/Queues

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

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

CSC148 Week 2. Larry Zhang

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

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Postfix (and prefix) notation

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Linked Structures. See Section 3.2 of the text.

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

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

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

csci 210: Data Structures Stacks and Queues

3137 Data Structures and Algorithms in C++

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

Lecture 2: Implementing ADTs

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

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

CS W3134: Data Structures in Java

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

CS24 Week 4 Lecture 2

Adam Blank Lecture 3 Winter 2017 CSE 332. Data Structures and Parallelism

Section 05: Solutions

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Linda Shapiro Winter 2015

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

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

More Data Structures (Part 1) Stacks

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Motivation for Queues

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Stacks and Queues. David Greenstein Monta Vista

CSE wi: Practice Midterm

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

CSE 143. Computer Programming II

COM2001 Advanced Programming Techniques. Mike Stannett Department of Computer Science The University of Sheffield

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

Stacks and Queues

CSE 2123 Recursion. Jeremy Morris

Section 05: Solutions

APCS-AB: Java. Recursion in Java December 12, week14 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Summer Final Exam Review Session August 5, 2009

Queue. COMP 250 Fall queues Sept. 23, 2016

[2:3] Linked Lists, Stacks, Queues

infix expressions (review)

CSE332: Data Abstractions Lecture 7: B Trees. James Fogarty Winter 2012

Midterm Exam 2 CS 455, Spring 2011

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

CS301 - Data Structures Glossary By

INTERFACES IN JAVA. Prof. Chris Jermaine

We've tried to include the common errors and grading standard for every question.

CS 106B Lecture 5: Stacks and Queues

CS 4349 Lecture August 21st, 2017

Lecture 4 Stack and Queue

Stacks and queues (chapters 6.6, 15.1, 15.5)

Computer Science Foundation Exam

12 Abstract Data Types

Practice Midterm Exam Solutions

INTERFACES IN JAVA. Prof. Chris Jermaine

ECE250: Algorithms and Data Structures Midterm Review

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Stacks Fall 2018 Margaret Reid-Miller

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSE373: Data Structures & Algorithms Lecture 12: Amortized Analysis and Memory Locality. Lauren Milne Spring 2015

COMP 250 Winter stacks Feb. 2, 2016

Queues Fall 2018 Margaret Reid-Miller

cs Java: lecture #6

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

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

CSE 373: Data Structures and Algorithms

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

CSE 373: Data Structures and Algorithms

CS 151 Name Final Exam December 17, 2014

CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees. Aaron Bauer Winter 2014

ECE 122. Engineering Problem Solving Using Java

Csci 102: Sample Exam

Lecture 2: Stacks and Queues

Linked lists (6.5, 16)

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

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

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

About this exam review

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CMPT 125: Practice Midterm Answer Key

Lecture 11 Unbounded Arrays

Transcription:

Adam Blank Lecture 1 Winter 2017 CSE 332 Data Abstractions

CSE 332: Data Abstractions Welcome to CSE 332!

Outline 1 Administrivia 2 A Data Structures Problem 3 Review of Stacks & Queues

What Am I Getting Into? 1 Course Material Classic Data Structures/Algorithms Rigorously analyze efficiency When to use each type of data structure Sorting Dictionary ADT Parallelism and Concurrency... CSE 143 vs. CSE 332 Client of Priority Queue vs. Implementor of Priority Queue Linked Lists vs. Graphs BST vs. Balanced BST Merge Sort vs. Advanced Sorting X vs. Parallelism

Course Goals 2 During the course, we will... Implement many different data structures Discuss trade-offs between them Rigorously analyze the algorithms that use them (math!) Be able to pick the right one for the job Experience the purposes and headaches of multithreading After the course, you will be able to... make good design choices as a developer, project manager, or system customer justify and communicate your design decisions This is the course where you stop thinking like a Java Programmer and start thinking like a Computer Scientist!

Support and Asking for Help 3 Resources Section every week! Lots of office hours! Piazza! Asking for help is not a sign of weakness; it s a sign of strength.

Boring Administrivia 4 Course Website http://cs.uw.edu/332 Grading 35% programming projects, 25% exercises, 20% midterm, 20% final four tokens Partner Projects All three programming projects are partners projects If you want to work alone, you must petition to do it. Please sign up for one of the options TODAY! Textbook Data Structures and Algorithm Analysis in Java (3rd edition) by Weiss

My Philosophy 5 Do what helps you most.... but active learning has been proven to result in better performance.

And We re Off! 6 Choose a data structure and algorithms to solve the following problem: Prefix Sums Input: An array arr of size n. Methods: arr.sum(i) should return i k=0 arr[k] arr.update(i, value) should update the value of the array at index i with value. Then, analyze how good your solution is. Naïve Implementation Structure: The input array. arr.sum(i): Loop from 0 to i adding up the elements. arr.update(i, value): Update index i with value. How good is it? sum is O(n); update is O(1). Suppose we know update is going to happen very rarely but sum will happen a lot. Can we do better?

A Little More Clever 7 Prefix Sums Input: An array arr of size n. Methods: arr.sum(i) should return i k=0 arr[k] arr.update(i, value) should update the value of the array at index i with value. Then, analyze how good your solution is. Another Try (?) Structure: An array, partials of partial sums (e.g. [3, 1, 9] [3, 4, 13]). arr.sum(i): Return partials[i]. arr.update(i, value): Update every index from i to the end by adding the difference between old and new. How good is it? sum is O(1); update is O(n).

Better Than Nothing... 8 Which is better? First Solution sum is O(n) update is O(1) Second Solution sum is O(1) update is O(n) This is a design trade-off! The answer is sometimes the left and sometimes the right. The left is better when... sum is rare; update is frequent We aren t allowed to change the data structure (or we re not allowed extra space). The right is better when... sum is frequent; update is rare We re more concerned with the time complexity of sum than our space efficiency

EVEN BETTER 9 Consider the following input array: arr: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] Let s get fancy now. Consider the following split of the array: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 3 6 2 arr[0] arr[1] arr[2] 7 1 9 arr[3] arr[4] arr[5] 3 6 arr[0] arr[1] 2 arr[2] 7 1 arr[3] arr[4] 9 arr[5] 3 arr[0] 6 arr[1] 7 arr[3] 1 arr[4]

EVEN BETTER 10 Consider the following input array: arr: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] Let s build up a tree that stores partial sums in each node. Start at the leaves: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 3 6 2 arr[0] arr[1] arr[2] 7 1 9 arr[3] arr[4] arr[5] 3 6 arr[0] arr[1] 2 2-2 7 1 arr[3] arr[4] 9 5-5 3 0-0 6 1-1 7 3-3 1 4-4

EVEN BETTER 11 Consider the following input array: arr: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] Let s build up a tree that stores partial sums in each node. Now go one level up: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 3 6 2 arr[0] arr[1] arr[2] 7 1 9 arr[3] arr[4] arr[5] 9 0-1 2 2-2 8 3-4 9 5-5 3 0-0 6 1-1 7 3-3 1 4-4

EVEN BETTER 12 Consider the following input array: arr: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] Let s build up a tree that stores partial sums in each node. And another... 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 11 0-2 17 3-5 9 0-1 2 2-2 8 3-4 9 5-5 3 0-0 6 1-1 7 3-3 1 4-4

EVEN BETTER 13 Consider the following input array: arr: 3 6 2 7 1 9 And finally, we get: arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 28 0-5 11 0-2 17 3-5 9 0-1 2 2-2 8 3-4 9 5-5 3 0-0 6 1-1 7 3-3 1 4-4 Let s use THIS as our data structure. (For reference, this data structure is called a Segment Tree.)

EVEN BETTER 14 Consider the following input array: arr: 3 6 2 7 1 9 arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] 28 0-5 11 0-2 17 3-5 9 0-1 2 2-2 8 3-4 9 5-5 3 0-0 6 1-1 7 3-3 1 4-4 How Do We Write update? Walk up the tree from the leaf node that represents the index we re updating. Change each node accordingly. What is the complexity of update? It s O(logn), because the tree is balanced.

EVEN BETTER 15 28 0-5 11 0-2 17 3-5 9 0-1 2 2-2 8 3-4 9 5-5 3 0-0 How Do We Write sum(i)? 1 sum(i) = sum(i, root); 2 sum(i, node) { 3 if (node.range is completely outside (0, i)) { 4 return 0; 5 6 else if ((0, i) contains something not in node.range) { 7 return sum(i, node.left) + sum(i, node.right); 8 9 else { 10 return node.value; 11 12 See above for sum(4). 6 1-1 7 3-3 1 4-4 This is O(logn), btw.

Putting It All Together 16 While trying to solve this problem, we did the following things: Considered an algorithmic problem and attempted to solve it Chose data structures and algorithms to solve the problem (duh... ) Analyzed code for runtime Considered trade-offs between different implementations Learned a new data structure which helped us solve the problem much better than before Ran into analyzing a recursive runtime One thing we didn t consider (but that we will later!) was how to solve the problem if we had multiple processors. This course is about learning fundamental data structures and algorithms to help you solve Computer Science problems. Excited yet? Okay... what if I told you this is an interview question?

Data Structures & Abstract Data Types 17 Definition (Abstract Data Type [ADT]) An Abstract Data Type is a mathematical model of the properties necessary for a data structure to be a particular data type. To put it another way, an ADT specifies what a data type is and the valid operations on it. Definition (Data Structure) A Data Structure is a particular implementation of an ADT. ADT Data Structure Implementation Stack ArrayList java.util.stack Stack LinkedList Queue LinkedList java.util.linkedlist

Stacks & Queues 18 Queue ADT enqueue(val) dequeue() peek() isempty() Adds val to the queue. Returns the least-recent item not already returned by a dequeue. (Errors if empty.) Returns the least-recent item not already returned by a dequeue. (Errors if empty.) Returns true if all inserted elements have been returned by a dequeue. Stack ADT push(val) pop() peek() isempty() Adds val to the stack. Returns the most-recent item not already returned by a pop. (Errors if empty.) Returns the most-recent item not already returned by a pop. (Errors if empty.) Returns true if all inserted elements have been returned by a pop.

Stack & Queue Examples 19 Queue Examples 7-2 4 2 3 dequeue() 7-2 4 2 3 enqueue(9) -2 4 2 3 9 Stack Examples 7-2 4 2 3 pop() 7-2 4 2 3 push(9) 9-2 4 2 3

Why Do We Care About ADTs? 20 ADTs are used to COMMUNICATE ideas more easily! Parentheses Matching Given a string of parentheses (i.e. (, ), [, ]), figure out if the parentheses are matched. WORST: A particular implementation in a particular language using the wrong ADT for (int i = 0; i < str.length(); i++) { if (str.charat(i) == ( str.charat(i) == [ ) { list.add(str.charat(i)); else if ((str.charat(i) == ) && list.get(list.length() 1) == ( ) (str.charat(i) == ] && list.get(list.length() 1) == [ )) { list.remove(list.length() 1); else { throw new Exception(); if (list.size() > 0) { throw new Exception();

Why Do We Care About ADTs? 21 ADTs are used to COMMUNICATE ideas more easily! Parentheses Matching Given a string of parentheses (i.e. (, ), [, ]), figure out if the parentheses are matched. REALLY BAD: A particular implementation, in a particular language for (int i = 0; i < str.length(); i++) { if (str.charat(i) == ( str.charat(i) == [ ) { stack.push(str.charat(i)); else if ((str.charat(i) == ) && stack.peek() == ( ) (str.charat(i) == ] && stack.peek() == [ )) { stack.pop(); else { throw new Exception(); if (!stack.isempty()) { throw new Exception();

Why Do We Care About ADTs? 22 ADTs are used to COMMUNICATE ideas more easily! BETTER: Pseudo-code using the right ADT for (index in str) { if (str[index] is open) { put it on the stack else if (str[index] is top of stack and it matches the top element) { pop the top element off the stack else { throw error if (stack isn t empty) { throw error; BEST: High-level description using the right ADT To match parentheses, loop through the string pushing open parens onto the stack. When we see a close paren, make sure it matches and pop it off. If the stack isn t empty at the end, they don t match.

Queue Implementations 23 We can implement the Queue ADT using multiple ideas: Linked List Queue Data Structure front back 1 2 3 4 Empty queue? Time complexities? Data Structure enqueue(x) { back.next = new Node(x); back = back.next; dequeue() { x = front.item; front = front.next; return x; Circular Array Queue Data Structure Q: 0 0 0 1 2 3 4 0 0 0 0 (0) (len - 1) front back Empty queue? Time complexities? Data Structure enqueue(x) { Q[back] = x; back = (back + 1) % size; dequeue() { x = Q[front]; front = (front + 1) % size; return x;

Trade-Offs? 24 LinkedList Queue CircularArray Queue Space (in queue)? No wasted space Extra (or too little?) Space (per element)? Larger Smaller Operation Times? Fast Fast Other Concerns? Never runs out of space Can run out of space Why would we ever use a circular array queue? In practice, creating new Nodes can fail Memory allocation can be expensive Sometimes, we know in advance what the maximum size of the queue will be (see P1!)

Today s Takeaways! 25 Hopefully you re excited! What is an ADT? What is a Data Structure? Understand Stack and Queue ADTs Understand Queue implementations

Homework! 26 Go to the course website and: Read the partners handout and fill out the partners form. Finish Pokemon Purple & Gold (???) Also, the website for petitioning into the course is here: http://tinyurl.com/hjl3tpj