COMP 103 Analysing Costs Array Set Binary Search
COMP 103 Analysing Costs: Array. Set Binary Search
RECAP-TODAY 2 RECAP Analysing Algorithm Costs – Big O notation TODAY Array. List Costs: add at end (ensure capacity)- a bit tricky: as it find the “amortised” summary cost Array. Set Costs: get, set, contains Binary search: “find. Index” method of Array. Set – logarithmic cost summary
Array. List: add at end 3 Cost of add(value): what’s the key step? worst case: average case: n public void add (E item){ ensure. Capacity(); data[count++] = item; } private void ensure. Capacity () { if (count < data. length) return; E [ ] new. Array = (E[ ]) (new Object[data. length * 2]); for (int i = 0; i < count; i++) new. Array[i] = data[i]; data = new. Array; }
Array. List: amortised cost 4 Amortised cost: total cost of adding n items : n first 10: 11 th: 12 -20: 21 st: 22 -40: 41 st: 42 -80: : - n cost = 1 each cost = 10+1 cost = 1 each cost = 20+1 cost = 1 each cost = 40+1 cost = 1 each total = 10 total = 21 total = 30 total = 51 total = 70 total = 111 total = 150 total = Amortised cost ( ) = per item
Array. List costs: Summary 5 get O(1) set O(1) remove O(n) add (at i) O(n) (worst and average) add (at end) O(1) (average) O(n) (worst) O(1) (amortised average) To think about: What would the amortised cost be if the array size is increased by a fixed amount (say 10) each time?
6 What about Array. Set ? Order is not significant ⇒ can add a new item anywhere. where? At end: O(1), but also searching for duplicates : O(n) ⇒ can reorder when removing an item. how? Replace by last element: O(1), but also searching in set: O(n) Duplicates not allowed. ⇒ must check if item already present before adding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Array. Set algorithms (pseudocode) 7 Add(value) if not contains(value), place value at end, (doubling array if necessary) increment size Remove(value) search through array if value equals item replace item by item at end. decrement size return Contains(value) search through array, if value equals item return true return false Costs?
8 Array. Set costs Costs: contains, add, remove: All the cost is in the search! How can we speed up the search? O(n)
9 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? Ant Bee Cat Doe Dog Eel Fox Fish Gnu Hen Oryx Moral: lots of operations get easier if your array is sorted.
10 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. ’
11 Making Array. Set faster. All the cost is in the searching: Searching for “Gnu” 8 Bee 0 Dog Ant Fox Hen Gnu Eel Cat 1 2 3 4 5 6 7 Dog Eel Fox Gnu Hen but if sorted… 8 Ant Bee Cat 8
12 Making Array. Set faster. Binary Search: Finding “Gnu” 8 Ant Bee Cat Dog Eel Fox Gnu Pig 0 1 2 3 4 5 6 7 mi d low 8 hi 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
13 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) nb. this is just a “helper” method within Array. Set private int find. Index(Object item) { Comparable<E> value = (Comparable<E>) item; int low = 0; // min possible index of item int high = count; // max possible index of item while (low < high) { int mid = (low + high) / 2; if (value. compare. To(data[mid]) > 0) low = mid + 1; // item should be in [mid+1. . high] else high = mid; // item should be in [low. . mid] } return low; }
14 Binary Search: Cost What is the cost of searching if there are n items in set? key step = ? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Iteration Size of range 1 n 2 k 1
15 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) 20, log 2(1, 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 Solve Solve Solution
Summary: Array. Set with Binary Search 16 Array. Set: unordered All cost in the searching: O(n) contains: add: remove: O(n ) //simple, linear search O(n ) //cost of searching to see if there’s a duplicate O(n ) //cost of searching the item to remove Sorted. Array. Set: with Binary Search is fast: O(log n ) contains: add: remove: O(log n ) //uses binary search O(n ) //cost of keeping it sorted All the cost is in keeping it sorted!!!!
17 Making Sorted. Array. Set fast If you have to call add() and/or remove() many items, then Sorted. Array. Set is no better than Array. Set If you only have to construct the set once, and then many calls to contains(), then Sorted. Array. Set is much better than Array. Set. Both O(n ) Either we. . . pay to search Or we. . . pay to keep it in order Sorted. Array. Set contains() is O(log n ) to find 1 -in-a-billion, if sorted takes ~time that 1 -in-30 would, unsorted But, how do you construct the set fast? A separate constructor.
Alternative Constuctor 18 Sort the items all at once public Sorted. Array. Set(Collection<E> 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…
- Slides: 18