Analysing Costs: ArraySet Binary Search COMP 103.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Abstract Data Types (ADTs) Data Structures The Java Collections API
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
COMP s1 Computing 2 Complexity
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
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.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
CSC 211 Data Structures Lecture 13
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 
Section 1.7 Comparing Algorithms: Big-O Analysis.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
Searching and Sorting Searching algorithms with simple arrays
CSC 222: Object-Oriented Programming
Introduction to Analysing Costs
COMP 53 – Week Seven Big O Sorting.
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
More complexity analysis & Binary Search
COMP 103 Binary Search Trees.
Topic 14 Searching and Simple Sorts
Building Java Programs
Algorithm design and Analysis
searching Concept: Linear search Binary search
CSE 373 Data Structures and Algorithms
Topic 14 Searching and Simple Sorts
CSC 143 Binary Search Trees.
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Presentation transcript:

Analysing Costs: ArraySet Binary Search COMP 103

RECAP  Analysing Algorithm Costs – Big O notation TODAY  ArrayList Costs:  add at end (ensure capacity) - a bit tricky: as it find the “amortised” cost  summary  ArraySet Costs:  get, set, contains  Binary search: “findIndex” method of ArraySet – logarithmic cost  summary 2 RECAP-TODAY

ArrayList: add at end  Cost of add(value):  what’s the key step?  worst case:  average case: public void add (E item){ ensureCapacity(); data[count++] = item; } private void ensureCapacity () { if (count < data.length) return; E [ ] newArray = (E[ ]) (new Object[data.length * 2]); for (int i = 0; i < count; i++) newArray[i] = data[i]; data = newArray; } n 3

ArrayList: amortised cost  Amortised cost: total cost of adding n items : first 10: cost = 1 eachtotal = 10 11th:cost = 10+1total = :cost = 1 eachtotal = 30 21st:cost = 20+1total = :cost = 1 eachtotal = 70 41st:cost = 40+1total = :cost = 1 eachtotal = 150 : - n total =  Amortised cost ( ) = 4  n per item

ArrayList costs: Summary  getO(1)  setO(1)  removeO(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 ? 5

What about ArraySet ?  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

ArraySet algorithms (pseudocode) 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? 7

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

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? 9 Ant Bee Cat Doe Dog Eel Fox Fox Fish Fish Gnu Hen Oryx Moral: lots of operations get easier if your array is sorted.

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. ’ 10

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

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 AntBeeCatDogEelFoxGnuPig 8 12 low mid hi

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; } 13 nb. this is just a “helper” method within ArraySet

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

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 15

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!!!! 16

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. 17

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… 18