1 / 21

CSCI 62 Data Structures

CSCI 62 Data Structures. Dr. Joshua Stough September 11, 2008. Today. Generics Asymptotics Big-O Time/Space Fermi Solutions Recursion/Induction. Generics. Recognize errors sooner rather later. generic – class parameterized by the type of data. Java tutorial, generics example.

Download Presentation

CSCI 62 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 62Data 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<Double>, or Vector<Boolean> • Class def.: public class Association<K,V> • Instantiation: • Association<String,Integer> personAttribute = • new Assocation<String,Integer>("Age",34); type parameters autoboxed

  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<T> { 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<Integer> buildVector1(int n) // pre: n >= 0 // post: construct a vector of size n of 1..n { Vector<Integer> v = new Vector<Integer>(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<Integer> buildVector2(int n) // pre: n >= 0 // post: construct a vector of size n of 1..n { Vector<Integer> v = new Vector<Integer>(n); // 1 for (int i = 0; i < n; i++) // 2 { v.add(0,i); // 3 } return v; // 4 }

  14. Asymptotics • Factor Table public static Vector<Vector<Integer>> factTable(int n) // pre: n > 0 // post: returns a table of factors of values 1 through n { Vector<Vector<Integer>> table = new Vector<Vector<Integer>>(); for (int i = 1; i <= n; i++) { Vector<Integer> factors = new Vector<Integer>(); for (int f = 1; f <= i; f++) { if ((i % f) == 0) { factors.add(f); } } table.add(factors); } return 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 Inserting to the front of a Vector Binary trees, recursive fibonacci

  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?

More Related