TITLE: Show that $f:\mathbb R^n\to \mathbb R$ defined as $f(x)=\inf_{y\in K}||x-y||$ continuous on $\mathbb R^n$
QUESTION [6 upvotes]: A question from my analysis course reads

Let $K$ be a compact subset of $\mathbb R^n$. Define $f:\mathbb R^n\to \mathbb R$ as
$$f(x)=\text{d}(x,K)=\inf_{y\in K}||x-y||$$
Show that $f$ is continuous on $\mathbb R^n$ and $f^{-1}\left(\{0\}\right)=K$.
Is the compactness of $K$ necessary?

Now, I can very well understand that the described function has to be continuous because $f(x)=0$ when $x\in K$ and as $x$ slowly moves out of $K$, the distance increases continuously. But, of course, that's quite far away from a real mathematical proof, and as far as I understand, we need to use the property

If $f:\mathbb A\to \mathbb B$ is continuous and $O\subset B$ is an open set, then $f^{-1}\left(O\right)$ is open in $\mathbb A$

to arrive at the formal proof.
But, I'm quite new to functions whose domain is not $\mathbb R$. Until now, I have only studied functions $f:\mathbb R\to \mathbb R$. So, I'm a little confused about how to construct the proofs. Please help me to do it.
I was asked in the comments whether I'm allowed to use the $\epsilon-\delta$ property to prove continuity. Well, it wasn't explicitly mentioned in the exercise that I need to use the preimage property, but this question came just a couple of pages after this property was introduced. That's why I assumed they expect me to use that technique. Although I will also appreciate a detailed $\epsilon-\delta$ proof, it would be better if I can have a proof using the preimage method.

REPLY [2 votes]: May I use two standard notations that help with your question (and that you may already know). If $x, y \in \Bbb{R}^n$, let me write $d(x, y)$ for the distance betwwen $x$ and $y$ defined by:
$$
d(x, y) = \|x - y\|
$$
If $K$ is a non-empty subset of $\Bbb{R}^n$, let me write $d(x, K)$ for the distance of a point $x$ from the set $K$ defined by:
$$
d(x, K) = \inf_{y \in K} d(x, y)
$$
So your $f(x)$ is $d(x, K)$ for the given fixed non-empty set $K$.
Then for any fixed non-empty set $K$, $d(x, K)$ is a continuous function of $x$ (more formally $x \mapsto d(x, K)$ is a continuous function $\Bbb{R}^n \to \Bbb{R}$). To see this, let's use the $\epsilon$-$\delta$ approach. So take any $x$ and let $d(x, K) = t$. From the definition of $\inf$, what that means is that for any $\delta > 0$, there is a $y_\delta \in K$ such that $d(x, y_\delta) < t + \delta$ . Now, to prove continuity, let $\epsilon > 0$ be given. If we put $\delta = \epsilon/2$, then for any $x'$ with $d(x, x') < \delta$, we have $d(x', y_\delta) \le d(x', x) + d(x, y_\delta) < t + 2\delta$ and $|t + 2\delta - t| = 2\delta \le \epsilon$. Using the definition of $\inf$ again, this shows us that $|d(x', K) - d(x, K)| < \epsilon$ for every $x'$ with $d(x', x) < \delta$. So $d(x, K)$ is indeed a continuous function of $x$.
Now it may happen that $d(x, K) = 0$ even if $K$ is non-empty and $x \not\in K$. E.g., if $n = 1$ and $K$ is the open interval $(0, 1)$, then $d(0, K) = d(1, K) = 0$, but neither $0$ nor $1$ belongs to $K$. However, if $K$ is a closed subset of $\Bbb{R}^n$ this cannot happen. If $K$ is closed, and $d(x, K) = 0$, then by the definition of $\inf$, for each $n \in \Bbb{N}^+$, we can choose $y_n \in K$ such that $d(x, y_n) < 1/n$. But then then $y_n \to x$ as $n \to \infty$ implying that $x \in K$, because $K$ is closed.
So compactness is not required for the first part of your problem (the continuity of $f$) and is sufficient, but not necessary, for the  second part ($f^{-1}[\{0\}] = K$). For future reference, you can also note that the above only uses the fact that $d(x, y)$ makes $\Bbb{R}^n$ into a metric space: the results hold in any metric space.