+ All Categories
Home > Documents > Correction of quizzes

Correction of quizzes

Date post: 21-Jan-2016
Category:
Upload: jaimie
View: 55 times
Download: 0 times
Share this document with a friend
Description:
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) - PowerPoint PPT Presentation
24
Correction of quizzes
Transcript
Page 1: Correction of quizzes

Correction of quizzes

Page 2: Correction of quizzes

Correction of quizzes

Correction of quizzes

ADTs and implementations

Hash tables

Graphs

Page 3: Correction of quizzes

ADTs and implementationADTs and implementation

Page 4: Correction of quizzes

Correction of quizzes

ADTs: what can they do?Which ADTs have we seeen?

List Dictionary PriorityQueue Queue Stack

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.

Page 5: Correction of quizzes

Correction of quizzes

ADTs: what can they do?Which ADTs have we seeen?

List Dictionary PriorityQueue Queue Stack

add

getMax

removeMax

addLast

getFirst

removeFirst

addLast

getLast

removeLast

add(key)

get(key)

remove(key)

add(i)

get(i)

remove(i)

ExampleExample: 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());

Page 6: Correction of quizzes

Correction of quizzes

ADTs: what can they do?Which ADTs have we seeen?

List Dictionary PriorityQueue Queue Stack

add

getMax

removeMax

addLast

getFirst

removeFirst

addLast

getLast

removeLast

add(key)

get(key)

remove(key)

add(i)

get(i)

remove(i)

ExampleExample: 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.

Page 7: Correction of quizzes

Correction of quizzes

ADTs: what can they do?Which ADTs have we seeen?

List Dictionary PriorityQueue Queue Stack

add

getMax

removeMax

addLast

getFirst

removeFirst

addLast

getLast

removeLast

add(key)

get(key)

remove(key)

add(i)

get(i)

remove(i)

ExampleExample: 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.

Page 8: Correction of quizzes

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

N/A

N/A

N/A

N/A

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

Page 9: Correction of quizzes

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

N/A

N/A

N/A

N/A

N/A

O(1)

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)

Page 10: Correction of quizzes

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(1)

O(n)

N/A

O(n)

N/A

And implemented via an array?

O(n)

O(n)

O(n)

O(n)

O(1)

N/A

N/A

Page 11: Correction of quizzes

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 probing Using chaining

Double hashing

Linear probing

Quadratic probing

LinkedList

Balanced BST

Hashtable

arraypointers

Page 12: Correction of quizzes

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

N/A

O(1)

N/A

O(1)

N/A

Using chaining

Balanced BST

Page 13: Correction of quizzes

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

N/A

O(log m)

N/A

O(log m)

N/A

Using chaining

Balanced BST

LinkedListWhere m is the

maximum number of elements for any

given cell.

Page 14: Correction of quizzes

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

N/A

O(m)

N/A

O(m)

N/A

Using chaining

LinkedListWhere m is the

maximum number of elements for any

given cell.

Binary Search Tree (BST)

AVL (balanced)

pointers

Page 15: Correction of quizzes

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

N/A

O(log n)

N/A

O(log n)

N/A

Page 16: Correction of quizzes

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

N/A

N/A

O(log n)

N/A

O(1)

Page 17: Correction of quizzes

Hash tablesHash tables

Page 18: Correction of quizzes

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 ni

• Quadratic probing: h (x) = (h(x) + i²) mod ni

Page 19: Correction of quizzes

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.

Page 20: Correction of quizzes

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

Page 21: Correction of quizzes

GraphsGraphs

Page 22: Correction of quizzes

Correction of quizzes

A

I

C

F B

N

H G

L

D K

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)

A N H C B L K G I F D M J E

Page 23: Correction of quizzes

Correction of quizzes

A

I

C

F B

N

H G

L

D K

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|)

Page 24: Correction of quizzes

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.


Recommended