Kernel of composition of linear transformations

Let $f : U \to V$ and $g : V \to W$ be linear transformations on the vector spaces $U$, $V$, and $W$. Supposedly,

$$ \dim(\ker(g \circ f)) = \dim(\ker(f)) + \dim(\ker(g) \cap \operatorname{im}(f)). $$

How might I go about proving that?


(Attempt:) The $\dim(\ker(g)\cap\operatorname{im}(f))$ term suggests to me that I should define a vector space $V' = \ker(g) + \operatorname{im}(f)$ to invoke the theorem that

$$ \dim(V') = \dim(\ker(g)) + \dim(\operatorname{im}(f)) - \dim(\ker(g) \cap \operatorname{im}(f)), $$

but I don't really see where to go from there.


Here's one possible approach. If a vector is in $\ker f$, then it will certainly be in $\ker(g\circ f)$, but a vector $v$ could also "survive" $f$ (that is $f(v)\neq0$, so $v\notin\ker(f)$), but have $f(v)\in\ker g$ so that still $g(f(v))=0$. The term $\dim(\operatorname{im}(f)\cap\ker g)$ in a sense measures how much this second possibility adds to the dimension of $\ker(g\circ f)$, because in that case $f(v)\in\operatorname{im}(f)\cap\ker g$.

To make the idea precise, one may note that for the purpose of determining $g\circ f$, one may replace $g$ by its restriction to $\operatorname{im}(f)$ (since any vector to which $g$ gets applied in the setting of $g\circ f$ lies in $\operatorname{im}(f)$) $$ g\circ f = g|_{\operatorname{im}(f)}\circ f $$ from which its follows that $$ \operatorname{im}(g\circ f) = \operatorname{im}(g|_{\operatorname{im}(f)}). $$ Now you can apply the rank-nullity theorem successively to $g\circ f$, to $g|_{\operatorname{im}(f)}$, and to $f$ to obtain the required identity of dimensions: $$\begin{aligned} \dim\ker(g\circ f)&=\dim U-\operatorname{rk}(g|_{\operatorname{im}(f)})\\ &=\dim U-(\dim\operatorname{im}(f)-\dim\ker(g|_{\operatorname{im}(f)}))\\ &=\dim\ker(f)+\dim(\ker(g)\cap\operatorname{im}(f)) \end{aligned} $$

Another approach is to observe that the space $\ker(g)\cap\operatorname{im}(f)$ is precisely the image of $\ker(g\circ f)$ by $f$ (again the observations of the first paragraph apply; you can show both inclusions easily). Then apply the rank-nullity theorem to the restriction of $f$ to $\ker(g\circ f)$.


HINT: Use two different expressions for $\text{dim}(U)$ in terms of the maps as well as an expression for $\text{dim}(\text{Im}(f))$ in terms of the map $g$.

HINT 2: You can view $g$ as acting on $\text{Im}(f)$ alone. What does that tell you about $\text{dim}(\text{Im}(f))$ ?