Continuous with compact support implies uniform continuity

This might be a duplicate but I tried googling the MSE site and could not find a satisfactory answer.

Let $(X, d)$ be a metric space and $f$ be a real valued continuous function on $X$. Suppose $f$ has a compact support. Does this imply the uniform continuity of $f$?

I tried proving this statement, but only for locally connected spaces have I succeeded in doing so. Is thus true for general metric spaces? I just couldn't provide a proof (or a counterexample) by myself. Please enlighten me.


Solution 1:

Let $f:X\to\mathbf{R}$ satisfying your hypotheses. There exists $K$ compact s.t. $f_{|X \setminus K}=0$.

Let's show $f$ is uniformly continuous. Let $\epsilon>0$. By Heine theorem applied to $f_{|K}$, $f$ is uniformly continuous on $K$:

$$\exists \delta_1>0 \; \forall (x, y) \in K^2 \; d(x,y) < \delta_1 \implies |f(x)-f(y)| < \epsilon$$

If $(x, y) \in (X\setminus K)^2$, $\delta_1$ works.

Now suppose by contradiction that there exists $\epsilon>0$ s.t. for all $n \in \mathbf{N}$, there exists $x_n \in K$, $y_n \in X\setminus K$ s.t. $d(x_n, y_n) < 2^{-n}$ and $|f(x_n) - f(y_n)| > \epsilon$. $(x_n)$ lies in a compact space, so there exists $\sigma$ strictly increasing and $x \in K$ s.t. $x_{\sigma(n)} \to x$. Then $y_{\sigma(n)} \to x$, but $|f(x_{\sigma(n)}) -f(y_{\sigma(n)})| > \epsilon$, thus $f$ is not continuous in $x$, contradiction.

Hence:

$$\exists \delta_2>0 \; \forall (x, y) \in K \times (X \setminus K) \; d(x,y) < \delta_2 \implies |f(x)-f(y)| < \epsilon$$

Finally $\delta = \min(\delta_1, \delta_2)$ works.