Correction of quizzes

1 / 24

# Correction of quizzes - PowerPoint PPT Presentation

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)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Correction of quizzes' - jaimie

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Correction of quizzes

Hash tables

Graphs

Correction of quizzes

List

Dictionary

PriorityQueue

Queue

Stack

get(i)

remove(i)

get(key)

remove(key)

getMax

removeMax

getFirst

removeFirst

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

List

Dictionary

PriorityQueue

Queue

Stack

get(i)

remove(i)

get(key)

remove(key)

getMax

removeMax

getFirst

removeFirst

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

List

Dictionary

PriorityQueue

Queue

Stack

get(i)

remove(i)

get(key)

remove(key)

getMax

removeMax

getFirst

removeFirst

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

List

Dictionary

PriorityQueue

Queue

Stack

get(i)

remove(i)

get(key)

remove(key)

getMax

removeMax

getFirst

removeFirst

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

Stack

Lets consider a stack using pointers:

• Remove

• RemoveMax

• Get

• GetMax

N/A

getLast

removeLast

N/A

all 3 in O(1)

O(1)

N/A

What if we used an array?

N/A

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

Queue

Lets consider a queue using pointers:

• Remove

• RemoveMax

• Get

• GetMax

N/A

getFirst

removeFirst

N/A

O(1)

O(1)

N/A

What if we used an array?

N/A

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

Lets consider a List using pointers, with a shortcut to the tail:

• Remove

• RemoveMax

• Get

• GetMax

And implemented via an array?

List

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

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

AVL (balanced)

Balanced BST

Linear probing

Hashtable

Correction of quizzes

Dictionaries have many implementations.

Dictionary

• 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

Dictionaries have many implementations.

Dictionary

• 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

O(log m)

Balanced BST

N/A

Correction of quizzes

Dictionaries have many implementations.

Dictionary

• 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

AVL (balanced)

O(m)

N/A

Correction of quizzes

Dictionaries have many implementations.

Dictionary

• 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

We have seen only one implementation for priority queue: the family of heaps, out of which we only looked at the binary heap.

PriorityQueue

getMax

removeMax

O(log n)

• Remove

• RemoveMax

• Get

• GetMax

N/A

N/A

N/A

O(log n)

N/A

O(1)

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

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

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

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

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

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

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