1324 Algorithmics Flashcards
What is an algorithm?
Uses a finite sequence of computational steps to produce the output using an input.
How do we evaluate algorithms?
Is it correct?
Is it efficient?
What are data structures?
A standardised way to store and organise data for ease of access and modification.
What is the complexity of searching a binary search tree?
theta (log n)
What happens if we use a key to lookup items?
Search, insert and delete become O(1)
Why is hashing wasteful?
Nearly all memory locations end up empty.
What is a hash function?
A function that takes an objects x and returns a positive integer
Where the table size is 2^n what can we use for efficiency?
A bitmask
What are the features of a desirable hash function?
Fast to compute
Keys must be distributed as uniformly as possible
Consistency with equality testing functions
What are the two methods of collision resolution?
Separate Chaining
Open addressing
What is separate chaining?
At each table entry we have a singly linked-list. When there is a collision, we add a new element to these lists. This means when we search we have to iterate through the list.
What is the complexity of Separate Chaining?
O(n) with the list
O(1) without the list
What is open addressing?
When a collision occurs we find the nearest open spot in the table and then add it there.
Overtime, entries tend to cluster up, and increase the number of probes needed to locate the item.
What is the loading factor?
The proportion of full entries in the table is known as the loading factor.
lambda = 0.9 means that 1 in ten locations is free
How can we avoid clustering?
Quadratic Probing
Double Hashing
What is Quadratic Probing?
We try locations h(x) + d_i where d_i = i^2, we therefore search in steps of 1,4,9,16,…
This prevents primary clustering, but we might not be able to add an element even if the table isn’t full.
What is double hashing?
h(x) = d_i, d_i = i*h_2(x)
h_2(x) is a second hash function that depends on the key. h_2(x) = R- (x MOD R) where R is a prime smaller than the table size. h_2(x) cannot be a divisor of the table size, so the table size must be prime
How to remove with open addressing?
Compute the array index, if the location is empty it is not present
If it contains the key the search succeeds
Otherwise, find a new address using open addressing and restart.,
What is the problem with deleting an element in open addressing?
If we delete an entry between the first address and the actual value address, the actual value becomes inaccessible.
We can solve this by inserting a dummy value when we delete.
How do we resize a hash table?
Create a hash table, twice the size
Iterate through the old hash table, adding each element to the new table.
The requires recomputing all the hash codes.
What does the UNION command do?
Connects two objects.
What does isConnected do?
Determine if two objects are connected via a path
What does find do?
Check which class a given element belongs to
How can we implement Union, Find, and isConnected?
An array of Size N, ID. P and Q are connected if their IDs are the same
To merge, id[q] = id[p]