Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.

Slides:



Advertisements
Similar presentations
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey  Class variables  Big Oh notation.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Week 12: Running Time and Performance 1.  Most of the problems you have written in this class run in a few seconds or less  Some kinds of programs can.
Introduction to Analysis of Algorithms
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Elementary Data Structures and Algorithms
CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006.
Chapter 19 Java Data Structures
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMPSCI 102 Introduction to Discrete Mathematics.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Week 2 CS 361: Advanced Data Structures and Algorithms
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Mathematics Review and Asymptotic Notation
Iterative Algorithm Analysis & Asymptotic Notations
CS 1704 Introduction to Data Structures and Software Engineering.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Week 1 - Friday.  What did we talk about last time?  Basic programming model  Other Java stuff  References  Static  Inner classes  Exceptions.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Chapter 18 Java Collections Framework
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
Week 2 - Wednesday.  What did we talk about last time?  Running time  Big Oh notation.
Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Week 15 – Monday.  What did we talk about last time?  Tries.
Chapter 2 Algorithm Analysis
Sixth Lecture ArrayList Abstract Class and Interface
Week 2 - Wednesday CS221.
Week 4 - Friday CS221.
Recitation 13 Searching and Sorting.
Introduction to Algorithms
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
Week 2 - Friday CS221.
Week 15 – Monday CS221.
Building Java Programs
Algorithm design and Analysis
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
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
Presentation transcript:

Week 2 - Monday

 What did we talk about last time?  Exceptions  Threads  OOP  Interfaces

 Allow classes, interfaces, and methods to be written with a generic type parameter, then bound later  Java does the type checking (e.g. making sure that you only put String objects into a List )  After typechecking, it erases the generic type parameter  This works because all classes extend Object in Java  Appears to function like templates in C++, but works very differently under the covers  Most of the time you will use generics, not create them

 You can make a class using generics  The most common use for these is for container classes  e.g. you want a List class that can be a list of anything  JCF is filled with such generics

public class Pair { private T x; private T y; public Pair(T a, T b ) { x = a; y = b; } public T getX() { return x; } public T getY() { return y; } public void swap() { T temp = x; x = y; y = temp; } public String toString() { return "( " + x + ", " + y + " )"; } public class Pair { private T x; private T y; public Pair(T a, T b ) { x = a; y = b; } public T getX() { return x; } public T getY() { return y; } public void swap() { T temp = x; x = y; y = temp; } public String toString() { return "( " + x + ", " + y + " )"; }

public class Test { public static void main(String[] args) { Pair pair1 = new Pair ("ham", "eggs"); Pair pair2 = new Pair ( 5, 7 ); pair1.swap(); System.out.println( pair1 ); System.out.println( pair2 ); } public class Test { public static void main(String[] args) { Pair pair1 = new Pair ("ham", "eggs"); Pair pair2 = new Pair ( 5, 7 ); pair1.swap(); System.out.println( pair1 ); System.out.println( pair2 ); }

Java Collections Framework

 Collection  Iterable  List  Queue  Set  SortedSet  Map  SortedMap

 LinkedList  ArrayList  Stack  Vector  HashSet  TreeSet  HashMap  TreeMap

 Collections  sort()  max()  min()  replaceAll()  reverse()  Arrays  binarySearch()  sort()

Impromptu Student Lecture

 How do we measure the amount of time an algorithm takes?  Surely sorting 10 numbers takes less time than sorting 1,000,000 numbers  Sorting 1,000,000 numbers that are already sorted seems easier than sorting 1,000,000 unsorted numbers  We use a worst-case, asymptotic function of input size called Big Oh notation

 Worst-case because we care about how bad things could be  Asymptotic because we ignore lower order terms and constants  15n 2 + 6n + 7log(n) is O(n 2 )  2 n n is O(2 n )  If the function of n is polynomial, we say that it is efficient or tractable

 For running time functions f(n) listed below, give the Big Oh  f(n) = 3n 2 + 4n  O(n 2 )  f(n) = 15n 3 + nlog n  O(n 3 )  f(n) = 1000n log n  O(n)  f(n) = 5050  O(1)

 We assume that all individual operations take the same amount of time  So, we compute how many total operations we'll have for an input size of n (because we want to see how running time grows based on input size)  Then, we find a function that describes that growth

 How long does it take to do multiplication by hand? 123 x __  Let’s assume that the length of the numbers is n digits  (n multiplications + n carries) x n digits + (n + 1 digits) x n additions  Running time: O(n 2 )

 How do we find the largest element in an array?  Running time: O(n) if n is the length of the array  What if the array is sorted in ascending order?  Running time: O(1) int largest = array[0]; for( int i = 1; i < length; i++ ) if( array[i] > largest ) largest = array[i]; System.out.println("Largest: " + largest); int largest = array[0]; for( int i = 1; i < length; i++ ) if( array[i] > largest ) largest = array[i]; System.out.println("Largest: " + largest); System.out.println("Largest: " + array[length-1]);

 Given two n x n matrices A and B, the code to multiply them is:  Running time: O(n 3 )  Is there a faster way to multiply matrices?  Yes, but it’s complicated and has other problems double[][] c = new double[N][N]; for( int i = 0; i < N; i++ ) for( int j = 0; j < N; j++ ) { c[i][j] = 0; for( int k = 0; k < N; k++ ) c[i][j] += a[i][k]*b[k][j]; } double[][] c = new double[N][N]; for( int i = 0; i < N; i++ ) for( int j = 0; j < N; j++ ) { c[i][j] = 0; for( int k = 0; k < N; k++ ) c[i][j] += a[i][k]*b[k][j]; }

 Here is some code that sorts an array in ascending order  What is its running time?  Running time: O(n 2 ) for( int i = 0; i < length - 1; i++ ) for( int j = 0; j < length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } for( int i = 0; i < length - 1; i++ ) for( int j = 0; j < length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 Here is some code that prints out a triangular shaped set of stars  What is its running time?  Running time: O(n 2 ) for( int i = 0; i < n; i++ ) { for( int j = 0; j <= i; j++ ) System.out.print("*"); System.out.println(); } for( int i = 0; i < n; i++ ) { for( int j = 0; j <= i; j++ ) System.out.print("*"); System.out.println(); }

 What's the running time to factor a large number N?  How many edges are in a completely connected graph?  If you have a completely connected graph, how many possible tours are there (paths that start at a given node, visit all other nodes, and return to the beginning)?  How many different n-bit binary numbers are there?

 Here is a table of several different complexity measures, in ascending order, with their functions evaluated at n = 100 DescriptionBig Ohf(100) ConstantO(1)1 LogarithmicO(log n)6.64 LinearO(n)O(n)100 LinearithmicO(n log n) QuadraticO(n2)O(n2)10000 CubicO(n3)O(n3) ExponentialO(2 n )1.27 x FactorialO(n!)9.33 x

 Let f(n) and g(n) be two functions over integers  f(n) is O(g(n)) if and only if  f(n) ≤ c∙g(n) for all n > N  for some positive real numbers c and N  In other words, past some arbitrary point, with some arbitrary scaling factor, g(n) is always bigger

 What is a logarithm?  Definition:  If b x = y  Then log b y = x (for positive b values)  Think of it as a de-exponentiator  Examples:  log 10 (1,000,000) =  log 3 (81) =  log 2 (512) =

 Add one to the logarithm in a base and you'll get the number of digits you need to represent that number in that base  In other words, the log of a number is related to its length  Even big numbers have small logs  If there's no subscript, log 10 is assumed in math world, but log 2 is assumed for CS  Also common is ln, the natural log, which is log e

 log b (xy) = log b (x) + log b (y)  log b (x/y) = log b (x) - log b (y)  log b (x y ) = y log b (x)  Base conversion:  log b (x) = log a (x)/log a (b)  As a consequence:  log 2 (n) = log 10 (n)/c 1 = log 100 (n)/c 2 = log b (n)/c 3 for b > 1  log 2 n is O(log 10 n) and O(log 100 n) and O(log b n) for b > 1

 Computing complexity  Abstract data types (ADTs)

 Read section 1.2  Finish Assignment 1  Due Friday by 11:59pm  Start Project 1  Due Friday, September 18 by 11:59pm