COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Similar documents
ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

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?

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CS 310: Order Notation (aka Big-O and friends)

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 261 Data Structures. Big-Oh Analysis: A Review

Introduction to Computer Science I

CISC 3130 Data Structures Fall 2018

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CS Introduction to Data Structures Week 3, 2017

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

Review! Lecture 5 C Memory Management !

Computational Expression

Tips from the experts: How to waste a lot of time on this assignment

CS61C : Machine Structures

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

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

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Announcements. 1. Forms to return today after class:

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS 221 Review. Mason Vail

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

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CSCI 261 Computer Science II

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CMSC 201 Spring 2019 Lab 06 Lists

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Lecture 3: Queues, Testing, and Working with Code

ASYMPTOTIC COMPLEXITY

Programming II (CS300)

Dynamic Memory Allocation

Linked lists. Prof. Noah Snavely CS1114

Lecture 6: Sequential Sorting

CS 2316 Individual Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 18th, before 11:55 PM Out of 100 points

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

Principles of Algorithm Analysis. Biostatistics 615/815

Last time s big ideas

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

CS 101 Spring 2007 Midterm 2 Name: ID:

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Programming II (CS300)

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

CS 150 Introduction to Computer Science 1

Tips from the experts: How to waste a lot of time on this assignment

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Another interface: Comparable

Introduction to Data Structures

CIS 110: Introduction to Computer Programming

TEMPLATES AND ITERATORS

Program 1: Generating Summation Puzzles

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

HEAPS & PRIORITY QUEUES

CS61C : Machine Structures

Announcements. Hash Functions. Hash Functions 4/17/18 HASHING

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Algorithm Analysis. Big Oh

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

CIS 121 Data Structures and Algorithms with Java Spring 2018

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

CS61C : Machine Structures

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

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Chapter 2: Complexity Analysis

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

CS 1110: Introduction to Computing Using Python Loop Invariants

CMPSCI 187 / Spring 2015 Hangman

10 Generating Javadocs; HashMap: Overriding equals

CS 302: Introduction to Programming in Java. Lecture 12

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 113: Introduction to

CS ) PROGRAMMING ASSIGNMENT 11:00 PM 11:00 PM

Lecture 5 Abstract Data Types

Lecture 3: Working with Data Structures

CSC148 Week 9. Larry Zhang

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CMPSCI 187 / Spring 2015 Sorting Kata

COS 126 Exam 2 Review Part 1

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CS2110: Software Development Methods. Maps and Sets in Java

10 Java Collections; Equality, JUnit

Algorithm Performance. (the Big-O)

CS451 - Assignment 3 Perceptron Learning Algorithm

Transcription:

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1

administrivia 2

-assignment 2 is due tomorrow at midnight (11:59pm) -next assignment goes out today -due Thursday at midnight -requires a partner! -tutoring experiment 3

4

clickers!!! again <sigh> 5

6

7

are you here? 1) yes 2) no 8

feedback on Assignment 1 9

-grade is strongly correlated with the amount, and quality, of your testing code -write many tests! keep them organized! -will get practice with JUnit testing in lab on Monday -include name (and partner s name) on every file submitted -good practice is to include them in a comment header for each class -do NOT change the signatures for the methods we give you this will break the grader -we will be requiring Javadoc comments starting with Assignment 3 -TAs will review Javadocs in lab on Monday -many people lost points on tostring method -read specs carefully! 10

How many hours did you spend on assignment 1? A) <5 B) 5-10 C) 10-15 D) 15-20 E) >20 11

last time 12

algorithm analysis 13

-correctness is only half the battle -programs are expected to terminate in a reasonable amount of time -running time of a program is strongly correlated to the choice of algorithms used in problem solving -how much time and space does an algorithm require? 14

typical run-time complexities )+% )+" )+* =755:5.28:>< )+' )+# )+( )+& )+$,-./01 0 02,-.20 03$ 03& )+! )! "!# $$ $% &' (& #) #* '( *! *" "# %$ %% 4567829:;<2/01 N 2 and N 3 are typically not acceptable for moderate input sizes!

-for sufficiently large N, a function s growth rate is determined by its dominate term 10N 2 + 40N + 760 what is the dominate term? c log N N N log N N 2 N 3 constant logarithmic linear linearithmic quadratic cubic increasing growth rate 16

how to get log growth? -how many bits are needed to represent N consecutive integers? -starting at x=1, how many iterations of x*2 before x>=n? -the repeated doubling principle -starting at x=n, how many iterations of x/2 before x<=1? -the repeated halving principle 17

-big-o notation (O) is used to capture the dominate term in an algorithm -assuming large N! -for example, the running time of a quadratic algorithm is N 2 is specified O(N 2 ) -pronounced order N squared -this notation allows us to establish a relative order among algorithms -O(N log N) is better than O(N 2 ) 18

analyze the running time for(int i=0; i<n; i+=2) sum++; for(int i=0; i<n; i++) for(int j=0; j<n*n; j++) sum++ A) c B) log N C) N D) N log N E) N 2 F) N 3 for(int i=0; i<n; i*=2) sum++;

today 20

-collections -iterators 21

Collection interface 22

-a Collection is a data structure that holds items -very unspecific as to how the items are held -ie. the data structure -supports various operations: -add, remove, contains, -examples: -ArrayList -PriorityQueue -LinkedList -TreeSet 23

-you ll be working with Collections for assignment 3 -backed by an array for storage that you will implement yourselves -items will be sorted as they are inserted -no duplicates allowed -what are some of the issues with using an array? 24

add int[] data = new int[6]; data.add(5); data.add(17); data.add(9); data.add(12); data.add(1); data.add(33); 5 17 9 12 1 33 size = 0123 6 data.add(22); now what??? don t forget size++ what is the complexity of add? A) c B) log N C) N D) N log N E) N 2 F) N 3

-we need to grow our array! -avoid allocating slightly larger arrays -you will most likely need to grow again soon -good rule of thumb is to double the size -con: wastes up to 2x space -pro: growth will be rare 26

grow data 5 17 9 12 1 33 tmp = new int[data.length*2]; tmp copy all from data to tmp tmp 5 17 9 12 1 33 D) N log N data = tmp; A) c data 5 17 9 12 1 33 B) log N C) N what is the complexity of growing? E) N 2 F) N 3

remove 5 17 9 12 1 33 size = 6 data.remove(9); 5 17 12 1 33 size = 5 is this CORRECT? A) yes B) no

remove 5 17 9 12 1 33 size = 6 data.remove(9); 5 17 12 1 33 5 17 12 1 33 size = 5 size = 5 A) c B) log N C) N D) N log N E) N 2 F) N 3 what is the complexity of remove?

wait, what were we talking about? 30

Collection -a Collection is an object that groups multiple elements into a single unit -used to store, retrieve, manipulate, and communicate stored data -is like an array except their size can change dynamically, and have more advanced behaviors -the Java Collections API provides a set of classes and interfaces for storing data in different types of data structures -you will be implementing many of these on your own in assignments! 31

iterators 32

-not all data structures are guaranteed to use an array -thus, we can t just do: for(i=0; i<size; i++) data[i] -the Iterator interface provides generic retrieval of items from a data structure -the Collection interface requires an Iterator -for example, ArrayCollection has iterator() method that returns an Iterator 33

Iterator -an Iterator is specific to a data structure, and knows how to traverse the structure -hasnext: determines if iteration is complete -next: gets the next item -remove: removes the las seen item -internally, keeps track of where the next item is (as well as other state) -actually points to between items 34

next iterator 5 17 9 12 1 33 iterator.next(); // returns 9 iterator.next(); // returns 12 iterator.next(); // returns 1

remove removes the last item seen 5 17 9 12 1 33 iterator iterator.remove(); must be preceded by a call to next() iterator 5 17 9 1 33

enhanced for-loop -allows for simplification of code by representing a visit to each element of an array or Collection without explicitly expressing how you get from element to element standard way: for(int i=0; i < things.length; i++) // do something with things[i] Enhanced loop: for(thing t : things) // do something with t -uses an Iterator behind the scenes! 37

next time 38

-reading -chapters 8.1-8.4 -homework -assignment 2 due tomorrow at 11:59pm -assignment 3 out today, due next Thursday 39