Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 62 Data Structures Dr. Joshua Stough September 11, 2008.

Similar presentations


Presentation on theme: "CSCI 62 Data Structures Dr. Joshua Stough September 11, 2008."— Presentation transcript:

1 CSCI 62 Data Structures Dr. Joshua Stough September 11, 2008

2 Today Generics Asymptotics –Big-O –Time/Space –Fermi Solutions –Recursion/Induction

3 Generics Recognize errors sooner rather later. generic – class parameterized by the type of data. Java tutorial, generics example. Vector, or Vector Class def.: public class Association Instantiation: Association personAttribute = new Assocation ("Age",34); autoboxed type parameters

4 Box Example – too general public class Box { private Object object; public void add(Object a) {object = a; } public Object get() { return object; } }

5 Box Example – forces type public class Box { private T t; // T stands for "Type" public void add(T t) { this.t = t; } public T get() { return t; } }

6 Asymptotics People make different design decisions. Aspects of good design: –Already know: Encapsulation Extreme: Design Patterns, Soft. Engin. –Now: Algorithmic “niceness” Space, time efficiency Tools: –Big-O –Recursion/Induction

7 Asymptotics Determining performance: –Counting ops Assignments/swaps Multiplications Conditionals (if’s) –Bailey says comparison inappr. Btwn architectures, but: if > mult > assignment, in general. Min, Max, Both? (in-class) –Check against variable input: Problem size Crafted devilish input – best/worst/average

8 Asymptotics Best/Worst/Average –Key search on unordered list: –Best case? –Worst case? –Average case?

9 Asymptotics Define Big-O –A function f(n) is O(g(n)) (read “order g” or “big- O of g”), if and only if there exist two positive constants, c and n0, such that |f(n)| <= c · g(n) for all n n0. –O(1), O(n), O(n^2), O(n^c), O(k^n), O(n!) –Constant, linear, quadratic, polynomial, exponential, factorial. –Assign array, sum array, matrix mult, etc.

10 Asymptotics Difference table – complexity grows with area public static void diffTable(int n) // pre: n >= 0 // post: print difference table of width n { for (int row = 1; row <= n; row++) // 1 { for (int col = 1; col <= n; col++) // 2 { System.out.print(row-col+" "); // 3 } System.out.println(); // 4 }

11 Asymptotics Multiplication Table – grows with area. public static void multTable(int n) // pre: n >= 0 // post: print multiplication table { for (int row = 1; row <= n; row++) // 1 { for (int col = 1; col <= row; col++) // 2 { System.out.print(row*col+" "); // 3 } System.out.println(); // 4 }

12 Asymptotics Make a list – O(n) public static Vector buildVector1(int n) // pre: n >= 0 // post: construct a vector of size n of 1..n { Vector v = new Vector (n); // 1 for (int i = n-1; i >= 0; i--) // 2 { v.add(i); // 3 } return v; // 4 }

13 Asymptotics Make a list – O(n^2)-appears linear Remember to consider the cost of each line. public static Vector buildVector2(int n) // pre: n >= 0 // post: construct a vector of size n of 1..n { Vector v = new Vector (n); // 1 for (int i = 0; i < n; i++) // 2 { v.add(0,i); // 3 } return v; // 4 }

14 Asymptotics public static Vector > factTable(int n) // pre: n > 0 // post: returns a table of factors of values 1 through n { Vector > table = new Vector >(); for (int i = 1; i <= n; i++) { Vector factors = new Vector (); for (int f = 1; f <= i; f++) { if ((i % f) == 0) { factors.add(f); } table.add(factors); } return table; } Factor Table How to make faster? Usually just a constant.

15 Asymptotics Interesting Java tidbits –It takes between 1 and 10 nanoseconds (ns) to store a value in Java. Basic math operations take a similar length of time. –An array assignment is approximately twice as slow as a regular assignment. –A Vector assignment is approximately 50 times slower than a regular assignment.

16 Recursion 3 parts: –Base case –Self-reference –Progress toward base case Examples: –Trivial: min/max/key, sum

17 Recursion Fibonacci - recursive public static long recursiveFibNum(int n) { if (n < 3) return 1; else return (recursiveFibNum(n - 1) + recursiveFibNum(n - 2)); }

18 Recursion Fibonacci - dynamic public static long dynamicFibNum(int n) { long[] holder = new long[n + 1]; for (int i = 0; i < holder.length; i++) holder[i] = 0; return dynamicRecFibNum(n, holder);} public static long dynamicRecFibNum(int n, long answer[]) { if (n < 3) return 1; if (answer[n] != 0) return answer[n]; else { answer[n] = dynamicRecFibNum(n - 1, answer) + dynamicRecFibNum(n - 2, answer); return answer[n];}}

19 Recursion Fibonacci - iterative public static long easyIterFibNum(int n) { int i; long[] nums = new long[3]; nums[0] = nums[1] = 1; for (i = 2; n > 1; i = (i + 1) % 3, n--) { nums[i] = nums[(i + 1) % 3] + nums[(i + 2) % 3]; } return nums[(i + 1) % 3]; }

20 Induction Binary trees, recursive fibonacci Inserting to the front of a Vector

21 Symmetry and Friction Symmetry: if there’s a set method, should be a get method. (Square, Box). 1. Compare methods that extend the structure with methods that trim the structure. Do they have similar approaches? Are they similar in number? 2. Consider methods that read and write values. Can the input methods read what is written by the output methods? Can the writing methods write all values that can be read? 3. Are procedures that consume parameters matched by functions that deliver values? 4. Can points of potential garbage collection be equally balanced by new invocations? 5. In linked structures, does unlinking a value from the structure appear to be the reverse of linking a new value into the structure?


Download ppt "CSCI 62 Data Structures Dr. Joshua Stough September 11, 2008."

Similar presentations


Ads by Google