Questions Bolts Nults Clocks Pills Complexity Questions Bolts

Questions

Bolts & Nults Clocks & Pills Complexity Questions

Bolts & Nuts In a toolbox, we have n nuts with different diameters and the n matching bolts. The toolbox is messy, and we want to find the smallest nut and its matching bolt. Complete the following function that runs in at most 2 n – 2 steps. public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){ How would you do it? Classic answer: « one pass to find the smallest bolt and one pass to find the smallest nuts » . Well, you only have one comparator though… You can’t compare nuts AND compare bolts. Questions
![Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){ Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){](http://slidetodoc.com/presentation_image_h/2e6b92e4d0c06dcc23985d2564e681b5/image-4.jpg)
Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){ When do we discard a nut or a bolt? • If we have a nut i that’s larger than a bolt j, then it’s not the smallest one • If we have a bolt j that’s larger than a nut i, then it’s not the smallest one How will we return the matching bolt for the (smallest) nut? • Everytime a nut i matches with a bolt j, we remember it. • To remember it, we create a hash table of size n, so add/lookup is O(1). Questions
![Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){ Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){](http://slidetodoc.com/presentation_image_h/2e6b92e4d0c06dcc23985d2564e681b5/image-5.jpg)
Bolts & Nuts public void show. Smallest. Nut. Bolt(Nut[] nuts, Bolts[] bolts, Comparator c){ // we number the bolts and the nuts from 1 à n in any manner int i = 0, j = 0, n = nuts. length; boolean min = true; // if true then we found the smallest nuts, otherwise bolts while(i < n && j < n){ if(i==j==n) break; if(nuts[i]. compare. To(bolts[j])==0){ // we MEMORIZE that this bolt fits with this nuts // one way to memorize may be to use a data structure such as a hash table i++; }else if(nuts[i]. compare. To(bolts[j])<0){ j++; min = true; }else{ At the end if "min=false" then bolt j is the smallest i++; one and if "min=true" then nuts i the smallest one. min = false; } } Questions

Exercise on sorting ss Bapi loves clocks and every Friday he gets a new one. He doesn’t want to get the same clock twice so he always keep them sorted. Which sorting algorithm would Bapi use to keep his collection of clocks sorted when he buys a new one? Questions

Which sorting algorithm would Bapi use? Exercise on sorting ss Insertion sort. When a new clock comes, put it directly at the right place in O(n). Quicksort or mergesort would be sorting the entire collection without knowing it is already sorted but for the last clock that has been bought. Questions

Exercise on sorting Philippe has to take two kind of pills but he just spilled all of them on the table and now he has to sort them. A pill is either an instance of Advil or of Tylenol. Complete the algorithm to sort Philippe’s pills. public void doctor. Sort(Pill[] pills){ Who wants to come and type a solution? I won’t Questions

Complexity Q has invented a new algorithm for searching in a graph with n nodes. Due to Q’s secret techniques, he claims that the worst time case complexity is now O(log n). What can you say? Even if we ignore the cost of going through the edges, in the worst case we have to visit all nodes. If your algorithm has a complexity lower than O(n) then you are avoiding some nodes, and you cannot do that, so it just doesn’t work. Thus Q is a Qrook. Questions

Complexity For all data structures that we have seen, if there are n elements to store then the space complexity is O(n). • What would it mean if it was larger than O(n)? Would it be useful? If it’s larger than O(n) it means that one element is stored more than once. Lets say that tomorrow SFU’s server fails (not totally unrealistic…). If the file for each student was stored only once, then the one copy is gone so now everything disappeared. If the file was stored more than once then there are backups. A space complexity > O(n) means redundancy for security purposes. Questions

Complexity For all data structures that we have seen, if there are n elements to store then the space complexity is O(n). • What would it mean if it was smaller than O(n)? Would it be useful? • What would it mean if it was larger than O(n)? Would it be useful? If it’s less than O(n) it means that not all elements are stored. High particle physics experiments already produced 500 Gbits/s a decade ago: you cannot store all of it, you have to work on it as it is being produced and store at most a very small subset on which you’re currently working. When data is produced at a very high rate then we can only store a fragment (of size O(log n) at most). Questions

You were given many exercises Answers will not be posted, but your work can be corrected if you hand it in (and let me know so I come pick it up at the reception desk). For example… 2) Find the maximum difference that two elements have in a given array. • What’s the time complexity using sorting? • How would you do it without sorting? Questions
- Slides: 12