2024-05-19 21:07:38 +00:00
|
|
|
---
|
|
|
|
title: Direct Addressing
|
|
|
|
TARGET DECK: Obsidian::STEM
|
|
|
|
FILE TAGS: hashing::direct
|
|
|
|
tags:
|
|
|
|
- hashing
|
|
|
|
---
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
|
|
|
Given a universe of keys $U = \{0, 1, \ldots, m - 1\}$, a **direct-address table** has $m$ **slots**. Each slot corresponds to a key in universe $U$.
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Given universe $U$, how many slots must a direct-address table have?
|
|
|
|
Back: $|U|$
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153762-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
What name is given to each position in a direct-address table?
|
|
|
|
Back: A slot.
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153766-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Given a direct-address table, the element at slot $k$ has what key?
|
|
|
|
Back: $k$.
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153770-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Given a direct-address table, an element with key $k$ is placed in what slot?
|
|
|
|
Back: The $k$th slot.
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153775-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Write pseudocode to test membership of $x$ in direct-address table `T[0:m-1]`.
|
2024-05-20 19:52:48 +00:00
|
|
|
Back:
|
2024-05-19 21:07:38 +00:00
|
|
|
```c
|
|
|
|
bool membership(T, x) {
|
|
|
|
return T[x.key] != NIL;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153781-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-21 18:40:04 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
What is the worst-cast runtime complexity of direct-address table searches?
|
|
|
|
Back: $O(1)$
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716307180982-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-19 21:07:38 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Write pseudocode to insert $x$ into direct-address table `T[0:m-1]`.
|
2024-05-20 19:52:48 +00:00
|
|
|
Back:
|
2024-05-19 21:07:38 +00:00
|
|
|
```c
|
|
|
|
void insert(T, x) {
|
|
|
|
T[x.key] = x;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153785-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-21 18:40:04 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
What is the worst-case runtime complexity of direct-address table insertions?
|
|
|
|
Back: $O(1)$
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716307180983-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-19 21:07:38 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
Write pseudocode to delete $x$ from direct-address table `T[0:m-1]`.
|
2024-05-20 19:52:48 +00:00
|
|
|
Back:
|
2024-05-19 21:07:38 +00:00
|
|
|
```c
|
|
|
|
void delete(T, x) {
|
|
|
|
T[x.key] = NIL;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716046153789-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-21 18:40:04 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
What is the worst-cast runtime complexity of direct-address table deletions?
|
|
|
|
Back: $O(1)$
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716307180984-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
In what situation does direct addressing waste space?
|
|
|
|
Back: When the number of keys used is much less than the size of the universe.
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716307180986-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
In what situation is direct addressing impossible?
|
|
|
|
Back: When the size of the universe is too large to hold in memory.
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1716307180987-->
|
|
|
|
END%%
|
|
|
|
|
2024-05-19 21:07:38 +00:00
|
|
|
## Bibliography
|
|
|
|
|
|
|
|
* Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|