Presentation is loading. Please wait.

Presentation is loading. Please wait.

An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.

Similar presentations


Presentation on theme: "An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington."— Presentation transcript:

1 An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean

2 RECAP  Analysing Algorithm Costs – “Big O” notation  examples TODAY  Costs – examples from ArraySet: get(), set(), contains(), add(), remove()  Binary search: “findIndex” method of ArraySet – logarithmic cost Announcements:  First two assignments should be back with you very soon. 2 RECAP-TODAY

3  We should probably count these:  actions in the innermost loop (happen the most times)  actions that happen every time round (not inside an “if”)  actions involving “data values”(rather than indexes) public E remove (int index){ if (index = count) throw new ….Exception(); E ans = data[index]; for (int i=index+1; i< count; i++) data[i-1]=data[i]; count--; data[count] = null; return ans; } Problem: What is a “step” ? ←Key Step 3

4 ArrayList: get, set, remove  Assume some List contains n items.  Cost of get and set:  best, worst, average:  ⇒ constant number of steps, regardless of n  Cost of Remove:  worst case: what is the worst case ? how many steps ?  average case: half way what is the average case ? how many steps ? n 4

5 ArrayList: add (add at some position) public void add(int index, E item){ if (index count) throw new IndexOutOfBoundsException(); ensureCapacity(); for (int i=count; i > index; i--) data[i]=data[i-1]; data[index]=item; count++; }  Cost of add(index, value):  key step?  worst case:  average case: n 5

6 ArraySet costs Costs:  contains, add, remove:O(n)  All the cost is in the search!  How can we speed up the search? 6

7 Hand up if you find “Gnu” Dog Fish Cat Fox Eel Ant Bee Hen Gnu Doe Oryx Fox Fish Are there any duplications in that list? how many? 7 Ant Bee Cat Doe Dog Eel Fox Fox Fish Fish Gnu Hen Oryx Moral: lots of operations get easier if your array is sorted.

8 Hand up if you find “constructs” ‘ In most cases I don't believe that the disjunction between the preferred ideal way that intellectuals reflect and the modal operation of human cognition is much of an issue. Intellectuals, or those who fancy themselves as such, might struggle with issues of ontology. But I do not believe that this is particularly on the radar of the typical individual whose concerns are more prosaic, the basic material and emotional comforts and securities of life. Confusions only emerge when institutions and systems aim to span the full gamut of conventional cognition. For example, in politics or religion, where intellectuals build systems which are very relevant to the lives of most humans. Because of the general obscurity of intellectual constructs to the "average Joe" there is a large body of literature which exists to make abstruse concepts "relevant" in everyday terms to everyday folk. ’ 8

9 Making ArraySet faster. All the cost is in the searching:  Searching for “Gnu”  but if sorted… 123456780 BeeDogAntFoxHenGnuEelCat 8 AntBeeCatDogEelFoxGnuHen 8 9

10 Making ArraySet faster.  Binary Search: Finding “Gnu”  If the items are sorted (“ordered”), then we can search fast Look in the middle: if item is middle item ⇒ return if item is before middle item ⇒ look in left half if item is after middle item ⇒ look in right half 012345678 AntBeeCatDogEelFoxGnuPig 8 10 low mid hi

11 Binary Search This code returns the index of where the item ought to be, whether or not it is present (given this index, “contains” is trivial) private int findIndex(Object item) { Comparable value = (Comparable ) item; int low = 0; // min possible index of item int high = count; // max possible index of item while (low 0) low = mid + 1; // item should be in [mid+1..high] else high = mid; // item should be in [low..mid] } return low; } 11 nb. this is just a “helper” method within ArraySet

12 Binary Search: Cost  What is the cost of searching if there are n items in set ?  key step = ?  Iteration Size of range 1n 2 k1 012345678929101112133014151617181920212223242526272831 12

13 Log 2 (n ) : The number of times you can divide a set of n things in half. log 2 (1000)  10, log 2 (1,000,000)  20, log 2 (1,000,000,000)  30 Every time you double n, you add one step to the cost!  Logarithms often arise in analysing algorithms, especially “Divide and Conquer” algorithms: Problem Solution Solve 13

14 Summary: ArraySet with Binary Search ArraySet: unordered  All cost in the searching: O(n)  contains:O(n ) //simple, linear search  add: O(n ) //cost of searching to see if there’s a duplicate  remove:O(n ) //cost of searching the item to remove SortedArraySet: with Binary Search  Binary Search is fast: O(log n )  contains:O(log n ) //uses binary search  add: O(n ) //cost of keeping it sorted  remove:O(n ) //cost of keeping it sorted  All the cost is in keeping it sorted!!!! 14

15 Making SortedArraySet fast  If you have to call add() and/or remove() many items, then SortedArraySet is no better than ArraySet  Both O(n )  Either we...pay to search Or we...pay to keep it in order  If you only have to construct the set once, and then many calls to contains(), then SortedArraySet is much better than ArraySet.  SortedArraySet contains() is O(log n )  to find1-in-a-billion, if sorted takes ~time that 1-in-30 would, unsorted  But, how do you construct the set fast ?  A separate constructor. 15

16 Alternative Constuctor  Sort the items all at once public SortedArraySet(Collection col){ // Make space count=col.size(); data = (E[]) new Object[count]; // Put items from collection into the data array. : // sort the data array. Arrays.sort(data); }  So… how do you sort? Next lecture we will investigate… 16


Download ppt "An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington."

Similar presentations


Ads by Google