Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces."— Presentation transcript:

1 Week 2 - Monday

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

3

4

5

6

7  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

8  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

9 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 + " )"; }

10 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 ); }

11 Java Collections Framework

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

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

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

15 Impromptu Student Lecture

16

17  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

18  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) + 145 is O(n 2 )  2 n + 3906n 10000 + 892214 is O(2 n )  If the function of n is polynomial, we say that it is efficient or tractable

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

20  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

21  How long does it take to do multiplication by hand? 123 x 456 738 615 492__ 56088  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 )

22  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]);

23  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]; }

24  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; }

25  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(); }

26  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?

27  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)664.39 QuadraticO(n2)O(n2)10000 CubicO(n3)O(n3)1000000 ExponentialO(2 n )1.27 x 10 30 FactorialO(n!)9.33 x 10 157

28  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

29  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) =

30  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

31  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

32

33  Computing complexity  Abstract data types (ADTs)

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


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

Similar presentations


Ads by Google