1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Inheritance and efficiency  ArrayList  SortedArrayList  tradeoffs with adding/searching.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Inheritance and efficiency  ArrayList  SortedArrayList  tradeoffs with adding/searching.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Elementary Data Structures and Algorithms
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 CSC 321: Data Structures Fall 2012 Binary Search Trees  BST property  override binary tree methods: add, contains  search efficiency  balanced trees:
1 CSC 222: Object-Oriented Programming Spring 2013 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 CSC 222: Object-Oriented Programming Spring 2012 recursion & sorting  recursive algorithms  base case, recursive case  silly examples: fibonacci,
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
ArrayList, Multidimensional Arrays
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
1 CSC 222: Object-Oriented Programming Spring 2013 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
1 CSC 221: Computer Programming I Spring 2008 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
CSC 211 Data Structures Lecture 13
1 CSC 421: Algorithm Design & Analysis Spring 2013 Divide & conquer  divide-and-conquer approach  familiar examples: merge sort, quick sort  other examples:
1 CSC 222: Object-Oriented Programming Spring 2013 Searching and sorting  sequential search vs. binary search  algorithm analysis: big-Oh, rate-of-growth.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Decrease & conquer  previous examples  search spaces examples: travel, word ladder  depth.
1 CSC 222: Computer Programming II Spring 2004 Sorting and recursion  insertion sort, O(N 2 )  selection sort  recursion  merge sort, O(N log N)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Brute force approach  KISS vs. generality  HW2 examples: League Standings, Enigma, Musical.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Divide & conquer (part 1)  divide-and-conquer approach  familiar examples: binary search,
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Java Collections & List implementations  Collection classes: ─List (ArrayList, LinkedList),
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
1 CSC 421: Algorithm Design and Analysis Spring 2014 Brute force approach & efficiency  league standings example  KISS vs. generality  exhaustive search:
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CSC 427: Data Structures and Algorithm Analysis
CSC 421: Algorithm Design and Analysis
CSC 222: Object-Oriented Programming
CSC 421: Algorithm Design and Analysis
CSC 321: Data Structures Fall 2016 Balanced and other trees
CSC 427: Data Structures and Algorithm Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 222: Object-Oriented Programming
CSC 421: Algorithm Design and Analysis
CSC 222: Object-Oriented Programming
Chapter 5 Ordered List.
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Fall 2017
CSC 421: Algorithm Design & Analysis
CSC 321: Data Structures Fall 2015 Binary Search Trees BST property
COP 3503 FALL 2012 Shayan Javed Lecture 8
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Chapter 5 Ordered List.
TCSS 143, Autumn 2004 Lecture Notes
CSC 421: Algorithm Design and Analysis
CSC 221: Computer Programming I Fall 2009
Basics of Recursion Programming with Recursion
CSC 421: Algorithm Design and Analysis
CSC 222: Object-Oriented Programming Spring 2013
CSC 421: Algorithm Design and Analysis
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Spring 2012
CSC 321: Data Structures Fall 2018 Balanced and other trees
Presentation transcript:

1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Inheritance and efficiency  ArrayList  SortedArrayList  tradeoffs with adding/searching  timing code  divide-and-conquer algorithms

2 Dictionary revisited recall the Dictionary class from last week  the ArrayList add method simply appends the item at the end  O(1)  the ArrayList contains method performs sequential search  O(N) this is OK if we are doing lots of adds and few searches import java.util.List; import java.util.ArrayList; import java.util.Scanner; import java.io.File; public class Dictionary { private List words; public Dictionary() { this.words = new ArrayList (); } public Dictionary(String filename) { this(); try { Scanner infile = new Scanner(new File(filename)); while (infile.hasNext()) { String nextWord = infile.next(); this.words.add(nextWord.toLowerCase()); } catch (java.io.FileNotFoundException e) { System.out.println("FILE NOT FOUND"); } public void add(String newWord) { this.words.add(newWord.toLowerCase()); } public void remove(String oldWord) { this.words.remove(oldWord.toLowerCase()); } public boolean contains(String testWord) { return this.words.contains(testWord.toLowerCase()); }

3 Timing dictionary searches we can use the java.util.Date class to verify the O(N) efficiency dict. sizeinsert time 38, msec 77, msec 144, msec dict. sizesearch time 38, msec 77, msec 144, msec execution time roughly doubles as dictionary size doubles import java.util.Scanner; import java.io.File; import java.util.Date; public class DictionaryTimer { public static void main(String[] args) { System.out.println("Enter name of dictionary file:"); Scanner input = new Scanner(System.in); Date start1 = new Date(); Dictionary dict = new Dictionary(input.next()); Date end1 = new Date(); System.out.println(end1.getTime()-start1.getTime()); Date start2 = new Date(); for (int i = 0; i < 100; i++) { dict.contains("zzyzfys"); } Date end2 = new Date(); System.out.println(end2.getTime()-start2.getTime()); }

4 Sorting the list if searches were common, then we might want to make use of binary search  this requires sorting the words first, however we could change the Dictionary class to do the sorting and searching  a more general solution would be to extend the ArrayList class to SortedArrayList  could then be used in any application that called for a sorted list recall: public class java.util.ArrayList implements List { public ArrayList() { … } public boolean add(E item) { … } public void add(int index, E item) { … } public E get(int index) { … } public E set(int index, E item) { … } public int indexOf(Object item) { … } public boolean contains(Object item) { … } public boolean remove(Object item) { … } public E remove(int index) { … } … }

5 SortedArrayList (v.1) using inheritance, we only need to redefine what is new  add method sorts after adding; indexOf uses binary search  no additional fields required  big-Oh for add? big-Oh for indexOf? public class SortedArrayList > extends ArrayList { public SortedArrayList() { super(); } public boolean add(E item) { super.add(item); Collections.sort(this); return true; } public int indexOf(Object item) { return Collections.binarySearch(this, (E)item); }

6 SortedArrayList (v.2) is this version any better? when?  big-Oh for add?  big-Oh for indexOf? public class SortedArrayList > extends ArrayList { public SortedArrayList() { super(); } public boolean add(E item) { super.add(item); return true; } public int indexOf(Object item) { Collections.sort(this); return Collections.binarySearch(this, (E)item); }

7 SortedArrayList (v.3) if insertions and searches are mixed, sorting for each insertion/search is extremely inefficient  instead, could take the time to insert each item into its correct position  big-Oh for add? big-Oh for indexOf? public class SortedArrayList > extends ArrayList { public SortedArrayList() { super(); } public boolean add(E item) { int i; for (i = 0; i < this.size(); i++) { if (item.compareTo(this.get(i)) < 0) { break; } super.add(i, item); return true; } public int indexOf(Object item) { return Collections.binarySearch(this, (E)item); }

8 Timing dictionary searches note that repeated calls to add serve as insertion sort dict. sizeinsert time 38, sec 77, sec 144, sec dict. sizesearch time 38,621 0 msec 77,242 0 msec 144,48410 msec insertion time roughly quadruples as dictionary size doubles; search time is trivial import java.util.Scanner; import java.io.File; import java.util.Date; public class DictionaryTimer { public static void main(String[] args) { System.out.println("Enter name of dictionary file:"); Scanner input = new Scanner(System.in); Date start1 = new Date(); Dictionary dict = new Dictionary(input.next()); Date end1 = new Date(); System.out.println(end1.getTime()-start1.getTime()); Date start2 = new Date(); for (int i = 0; i < 100; i++) { dict.contains("zzyzfys"); } Date end2 = new Date(); System.out.println(end2.getTime()-start2.getTime()); }

9 SortedArrayList (v.4) if adds tend to be done in groups (as in loading the dictionary)  it might pay to perform lazy insertions & keep track of whether sorted  big-Oh for add? big-Oh for indexOf?  if desired, could still provide addInOrder method (as before) public class SortedArrayList > extends ArrayList { private boolean isSorted; public SortedArrayList() { super(); this.isSorted = true; } public boolean add(E item) { this.isSorted = false; return super.add(item); } public int indexOf(Object item) { if (!this.isSorted) { Collections.sort(this); this.isSorted = true; } return Collections.binarySearch(this, (E)item); }

10 Timing dictionary searches if we modify the Dictionary class to use a SortedArrayList dict. sizeinsert time 38, msec 77, msec 144, msec dict. size1 st search 38,621 0 msec 77,242 0 msec 144,48410 msec dict. sizesearch time 38,621 0 msec 77,242 0 msec 144,48410 msec import java.util.Scanner; import java.io.File; import java.util.Date; public class DictionaryTimer { public static void main(String[] args) { System.out.println("Enter name of dictionary file:"); Scanner input = new Scanner(System.in); Date start1 = new Date(); Dictionary dict = new Dictionary(input.next()); Date end1 = new Date(); System.out.println(end1.getTime()-start1.getTime()); Date start2 = new Date(); dict.contains("zzyfys"); Date end2 = new Date(); System.out.println(end2.getTime()-start2.getTime()); Date start3 = new Date(); for (int i = 0; i < 100; i++) { dict.contains("zzyzfys"); } Date end3 = new Date(); System.out.println(end3.getTime()-start3.getTime()); }

11 Divide & Conquer algorithms recursive algorithms such as binary search and merge sort are known as divide & conquer algorithms the divide & conquer approach tackles a complex problem by breaking into smaller pieces, solving each piece, and combining into an overall solution  e.g., to binary search a list, check the midpoint then binary search the appropriate half of the list divide & conquer is applicable when a problem can naturally be divided into independent pieces  e.g., merge sort divided the list into halves, conquered (sorted) each half, then merged the results

12 Iterative vs. divide & conquer many iterative algorithms can naturally be characterized as divide-and- conquer  sequential search for X in list[0..N-1] = false if N == 0 true if X == list[0] sequential search for X in list[1..N-1] otherwise  sum of list[0..N-1] = 0 if N == 0 list[0] + sum of list[1..N-1] otherwise  number of occurrences of X in a list[0..N-1] = number of occurrences of X in list[1..N-1] if X != list[0] 1 + number of occurrences of X in list[1..N-1] if X == list[0] interesting, but not very useful from a practical side (iteration is faster)

13 Euclid's algorithm one of the oldest known algorithms is Euclid's algorithm for calculating the greatest common divisor (gcd) of two integers  appeared in Euclid's Elements around 300 B.C., but may be even 200 years older  defines the gcd of two numbers recursively, in terms of the gcd of smaller numbers /** Calculates greatest common divisor of a and b a a positive integer b a positive integer (a >= b) the GCD of a and b */ public int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } e.g.., gcd(32, 12)= gcd(12, 8) = gcd(8, 4) = gcd(4, 0) = 4 e.g.., gcd(1024, 96)= gcd(96, 64) = gcd(64, 32) = gcd(32, 0) = 32 e.g.., gcd(17, 5)= gcd(5, 2) = gcd(2, 1) = gcd(1, 0) = 1 if the larger number has N digits, Euclid's algorithm requires at most O(N) recursive calls however, each (a % b) requires O(N) steps  O(N 2 ) there is no known algorithm with better big-Oh (but is possible to reduce constants)

14 Multidimensional divide & conquer we will see later that divide & conquer is especially useful when manipulating multidimensional structures  e.g., print values in a binary tree public void traverse(Node root) { if (root != null) { traverse(root.getLeft()); System.out.println(root.getValue()); traverse(root.getRight()); }  e.g., find the distance of the closest pair of points in a space 1.LDist = distance of closest pair in left half 2.RDist = distance of closest pair in right half 3.LClose = set of points whose x-coord are within min(LDist,RDist) to the left of center 4.RClose = set of points whose x-coord are within min(LDist,RDist) to the right of center 5.answer = min(LDist, RDist, distance(LClose i,RClose j ))