$g \circ f$ injective and $f$ surjective $\implies$ $g$ injective. Show that "$f$ surjective" is necessary? The first problem of a training exercise is:

Show that if $g \circ f$ injective and $f$ surjective, then $g$ injective.

I solved this problem similar to this solution https://math.stackexchange.com/q/845295. However, the second problem is:

Show with an counterexample that the condition "$f$ surjective" is necessary.

I was not able to find an counterexample. This might be on account to the fact that I do not quite understand why it is necessary. I would be thankful if


*

*someone can give me such an counterexample.

*explain why it is necessary. (maybe it becomes clear to me when I see the counterexample.  

 A: The key here is that the composition $g\circ f$ does not always give you full information about $g$, but you can reconstruct the whole $g$ if $f$ is surjective.
To explain that, I put some terminology. Let $X,Y,Z$ be arbitrary sets, $f:X\to Y$ and $g:Y\to Z$. The image of $X$ through $f$, $f(X)$, is a subset in $Y$, and it coincides with the whole space $Y$ if $f$ is surjective (by definition). Suppose that $f$ is not surjective, then we have $f(X)\subset Y$ properly.
When you define $g$, you must give a value of $g$ to every point of $Y$. But, when you apply $g$ to $f(X)$, you are restricting your function to that set, so you are not studying the whole function.
A counterexample? Suppose $X=\{x_1,x_2\},Y=\{y_1,y_2,y_3\},Z=\{z_1,z_2\}$. We define $f(x_1)=y_1,f(x_2)=y_2$ and $g(y_1)=g(y_3)=z_1$, $g(y_2)=z_2$. You see that $f$ is not surjective, and $g$ is not injective. What about $g\circ f$? We answer applying it to every point of $X$:
$$
g(f(x_1))=g(y_1)=z_1,\quad g(f(x_2))=g(y_2)=z_2.
$$
You see that $g\circ f$ is surjective and injective, even if $g$ is not injective.
A: Let me go over the proof:
We want to show, that $g$ is injectiv. Therefore we have to show, that for $g(y_1)=g(y_2)$ it is $y_1=y_2$
Let $f,g$ be functions with $f: X\to Y$ surjective and $g: Y\to Z$, also we know, that $g\circ f: X\to Z$ is injective. That means this functions has the poperty above.
Proof:
Let $g(y_1)=g(y_2)$ where $y_1, y_2\in Y$. We know that $f$ is surjective. Therefore $f$ maps onto $y_1$ and $y_2$ for some $x_1$ and $x_2$, so we can rewrite $y_1=f(x_1)$ and $y_2=f(x_2)$.
This is were we need that $f$ is surjective. Else this would not work!
We can proceed: $g(y_1)=g(y_2)$ now we know $g(f(x_1))=g(f(x_2))=g\circ f(x_1)=g\circ f(x_2)$, but $g\circ f$ is injective by assumtion.
So we know $f(x_1)=f(x_2)$ and this was just an other name for $y_1=y_2$ which we had to show!
So the proof is done.
For a counterexample we take an injective function $g\circ f$ and $f$ which is not surjective and show, that $g$ is not injective.
Take $X=\{1,2\}$, $Y=\{1,2,3\}$ and $Z=\{1,2\}$
$f: X\to Y$ is defined by $f(1)=1, f(2)=2$. This function is not surjective, because there is no $x\in X$ which maps onto 3!
$g: Y\to Z$ defined by $g(1)=1$, $g(2)=2$ and $g(3)=1$. $g$ is not injective, because it is $g(1)=g(3)=1$ but $1\neq 3$
And $g\circ f: X\to Z$ with $g(f(1))=g(1)=1$, $g(f(2))=g(2)=2$ is injective.
But $g$ was not!
