Answer
See explanation
Work Step by Step
Below is a simple algorithmic approach to check **surjectivity** (onto property) of a function \(f\) whose domain and codomain are both finite. We assume:
- The domain is a finite set \(X = \{x_1, x_2, \dots, x_m\}\).
- The codomain is a finite set \(Y = \{y_1, y_2, \dots, y_n\}\).
- We have access to a procedure \(\mathrm{Compute}(f,x)\) that returns \(f(x)\) for any \(x \in X\).
---
## Idea of the Algorithm
A function \(f\) is **onto** if **every element** of \(Y\) has at least one preimage in \(X\). Equivalently, as you evaluate \(f(x)\) for all \(x \in X\), you must produce **every** element of \(Y\) at least once.
### Pseudocode
```
Algorithm IsOnto(f, X, Y):
1. Create an empty set S to store images of f.
2. For each element x in X (the domain):
a. y ← Compute(f, x) // Get the image of x under f
b. Add y to S
3. // After processing all x, check if S covers Y:
4. If S contains all elements of Y:
return "f IS onto"
Else:
return "f is NOT onto"
```
### Explanation
1. **Initialize a set \(S\)** to keep track of values (in \(Y\)) that we have seen so far.
2. **Loop** over each element \(x\) in the domain \(X\).
- Compute \(f(x)\).
- Insert \(f(x)\) into \(S\).
3. **After** visiting every \(x \in X\), check if \(S\) contains all elements of \(Y\).
- If yes, then for every \(y \in Y\), there is an \(x\) with \(f(x) = y\). Hence \(f\) is onto.
- If no, then at least one \(y \in Y\) is missing, so \(f\) is not onto.
---
## Complexity
- We make **one pass** over all elements of the domain \(X\).
- Each pass involves:
1. **Computing** \(f(x)\) (done by the existing procedure).
2. **Inserting** \(f(x)\) into a data structure \(S\).
- Finally, we check whether \(S\) contains all elements of \(Y\).
If we use an efficient data structure (like a hash set) for \(S\), insertion and membership checks can be done in \(O(1)\) average time, making the overall process \(O(m + n)\) where \(m=|X|\) and \(n=|Y|\).
Either way, since the sets are finite, this procedure will definitively determine whether \(f\) is onto.