1 / 24

Correction of quizzes

Correction of quizzes. Correction of quizzes. ADTs and implementations. Hash tables. Graphs. Correction of quizzes. ADTs and implementation. ADTs: what can they do?. Which ADTs have we seeen?. List. Dictionary. PriorityQueue. Queue. Stack. add( i ) get (i) remove (i). add (key)

jaimie
Download Presentation

Correction of quizzes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Correction of quizzes

  2. Correction of quizzes ADTs and implementations Hash tables Graphs Correction of quizzes

  3. ADTs and implementation

  4. ADTs: what can they do? Which ADTs have we seeen? List Dictionary PriorityQueue Queue Stack add(i) get(i) remove(i) add(key) get(key) remove(key) add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast Each of these ADTs declares a set of functions that an implementation must be able to do. If there is no way to perform one function given the ones that are declared, then it’s N/A. Correction of quizzes

  5. ADTs: what can they do? Which ADTs have we seeen? List Dictionary PriorityQueue Queue Stack add(i) get(i) remove(i) add(key) get(key) remove(key) add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast Example: Can we perform addLast using a List? Yes. In a list you can indicate where you want the element to be added and you have access to the size for all data structures, so for a list L call L.add(o,L.size()); Correction of quizzes

  6. ADTs: what can they do? Which ADTs have we seeen? List Dictionary PriorityQueue Queue Stack add(i) get(i) remove(i) add(key) get(key) remove(key) add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast Example: Can we perform addLast using a Dictionary? No. There is no sense of « first » or « last » in a dictionary because it is not a linear structure using an index i, but a structure that uses a key. Correction of quizzes

  7. ADTs: what can they do? Which ADTs have we seeen? List Dictionary PriorityQueue Queue Stack add(i) get(i) remove(i) add(key) get(key) remove(key) add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast Example: Can we perform removeMax using a List? No. You can only use the index if you want to remove a specific object, but you do not know which element is the max. Yes you could always have a loop that finds where is the max, keeps its index and ask to remove it. But this is not something you can do directly. Correction of quizzes

  8. ADTs: what can they do? Which ADTs have we seeen? Stack Lets consider a stack using pointers: • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax N/A addLast getLast removeLast N/A all 3 in O(1) O(1) N/A What if we used an array? N/A addLast: O(n) due to resize N/A getLast: O(1) because the index tells us directly where the last is N/A removeLast: O(1) if at the end Correction of quizzes

  9. ADTs: what can they do? Which ADTs have we seeen? Queue Lets consider a queue using pointers: • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax N/A addLast getFirst removeFirst N/A O(1) O(1) N/A What if we used an array? N/A addLast: O(n) due to resize N/A getFirst: O(1) thanks to index N/A removeFirst: O(n) due to shifting (if first is in the first cell) Correction of quizzes

  10. ADTs: what can they do? Which ADTs have we seeen? Lets consider a List using pointers, with a shortcut to the tail: • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax And implemented via an array? List add(i) get(i) remove(i) O(n) O(n) O(n) O(1) O(n) O(1) O(n) O(n) N/A N/A O(1) O(n) N/A N/A Correction of quizzes

  11. ADTs: what can they do? Which ADTs have we seeen? Dictionaries have many implementations. Dictionary pointers array Binary Search Tree (BST) Hash tables Using probing Using chaining Basic (not balanced) Red black (balanced) Double hashing Quadratic probing LinkedList AVL (balanced) Balanced BST Linear probing Hashtable Correction of quizzes

  12. ADTs: what can they do? Which ADTs have we seeen? Dictionaries have many implementations. Dictionary • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax O(n) array N/A Hash tables N/A Using probing Using chaining O(1) Double hashing N/A O(1) Balanced BST N/A Correction of quizzes

  13. ADTs: what can they do? Which ADTs have we seeen? Dictionaries have many implementations. Dictionary • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax O(log m) array N/A Hash tables N/A Using chaining O(log m) Where m is the maximum number of elements for any given cell. N/A LinkedList O(log m) Balanced BST N/A Correction of quizzes

  14. ADTs: what can they do? Which ADTs have we seeen? Dictionaries have many implementations. Dictionary • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax O(1) pointers array N/A Binary Search Tree (BST) Hash tables N/A Using chaining O(m) Where m is the maximum number of elements for any given cell. N/A LinkedList AVL (balanced) O(m) N/A Correction of quizzes

  15. ADTs: what can they do? Which ADTs have we seeen? Dictionaries have many implementations. Dictionary • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax O(log n) pointers N/A Binary Search Tree (BST) N/A O(log n) N/A AVL (balanced) O(log n) N/A Correction of quizzes

  16. ADTs: what can they do? Which ADTs have we seeen? We have seen only one implementation for priority queue: the family of heaps, out of which we only looked at the binary heap. PriorityQueue add getMax removeMax O(log n) • Add • AddFirst • AddLast • Remove • RemoveMax • Get • GetMax N/A N/A N/A O(log n) N/A O(1) Correction of quizzes

  17. Hash tables

  18. 1) Explain what is improved when we use quadratic probing instead of linear. Linear probing tends to create clusters (i.e. groups of contiguous occupied cells) and thus the distribution is not very uniform. In quadratic probing, collisions are solved by going to further cells instead of the next one, which yields a more uniform distribution. As the efficiency depends on the distribution (i.e. the likeliness of collisions), it is improved. • Linear probing: h (x) = (h(x) + i) mod n i • Quadratic probing: h (x) = (h(x) + i²) mod n i Correction of quizzes

  19. 2) Give a secondary hash function such that the probing will behave like a linear probing. • Linear probing: h (x) = (h(x) + i) mod n i • Double hashing: h (x) = (h(x) + i.s(x)) mod n i where s(x) is a secondary hashing function. If we want both functions h (x) to be the same, what should s(x) be? i s(x) = 1 Note that a hashing function takes a key and returns a position. That’s all it does. It does not look for collisions and how to resolve them: that’s what the probing does. Correction of quizzes

  20. 3) If the structure used for chaining is also a hash table, what is the time complexity of a lookup using O notation? • You get your key, you hash it: you’ll get the cell where the structure is, since there are no collisions. So everything depends on the complexity of lookup in the structure used for chaining. • What is the complexity of a lookup in a hash table? ∙ We assume that we have the best type of probing (double hashing). ∙ We proved that the complexity of lookup is O(1) in a hash table if the probing behaves like random (almost the case for double hashing). • Thus the result is O(1). Correction of quizzes

  21. Graphs

  22. 1) Write the names of the nodes in the order in which you visit them in a breadth-first search from A to M. (i.e. the name of each node when you poll it from the queue) I H G J E A D C N K F B L M A N H C B L K G I F D M J E Correction of quizzes

  23. 2) If there are |E| edges and |V| nodes, explain what is the worst case time complexity of finding a path using breadth-first search. Which situation leads to the worst case? The target node is not reachable. I H G J E A D C N K F B L M How many nodes do you visit? |V| O(|V| + |E|) How many edges do you visit? |E| Correction of quizzes

  24. 3) What does it change to a breadth-first search when the collection of nodes is stored in an ArrayList instead of a LinkedList? Nothing. • You access the starting node from the main collection: the access is O(n) for a LinkedList or an ArrayList. • You will never need to access the main collection again: you move locally by following the edges. Correction of quizzes

More Related