Presentation is loading. Please wait.

Presentation is loading. Please wait.

Correction of quizzes. ADTs and implementations Hash tables Graphs.

Similar presentations


Presentation on theme: "Correction of quizzes. ADTs and implementations Hash tables Graphs."— Presentation transcript:

1 Correction of quizzes

2 ADTs and implementations Hash tables Graphs

3 ADTs and implementation

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

5 Correction of quizzes ADTs: what can they do? Which ADTs have we seeen? ListDictionaryPriorityQueueQueueStack add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast add(key) get(key) remove(key) add(i) get(i) remove(i) Example 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());

6 Correction of quizzes ADTs: what can they do? Which ADTs have we seeen? ListDictionaryPriorityQueueQueueStack add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast add(key) get(key) remove(key) add(i) get(i) remove(i) Example 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.

7 Correction of quizzes ADTs: what can they do? Which ADTs have we seeen? ListDictionaryPriorityQueueQueueStack add getMax removeMax addLast getFirst removeFirst addLast getLast removeLast add(key) get(key) remove(key) add(i) get(i) remove(i) Example 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.

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

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

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

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

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

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

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

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

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

17 Hash tables

18 Correction of quizzes 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

19 Correction of quizzes 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 Double hashing: h (x) = (h(x) + i.s(x)) mod n 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 i 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.

20 Correction of quizzes 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).

21 Graphs

22 Correction of quizzes A I C FB N HG L DK E J M 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) ANHCBLKGIFDMJE

23 Correction of quizzes A I C FB N HG L DK E J M 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. How many nodes do you visit?|V| How many edges do you visit? |E| O(|V| + |E|)

24 Correction of quizzes 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.


Download ppt "Correction of quizzes. ADTs and implementations Hash tables Graphs."

Similar presentations


Ads by Google