---
title: Open, closed and compact sets
heading: The kind of set where maxima are guaranteed and sequences can't escape
eyebrow: A14 · Lesson 11 · Open, closed and compact sets
xp: 40
---

::: hook
Why does a continuous function on [0, 1] always reach a largest value, while $\frac1x$ on (0, 1] doesn't, and x on [0, ∞) doesn't either? The answer is a property of the set, not the function: [0, 1] is **compact**, meaning closed (it contains its edge points) and bounded. Compactness is what guarantees that optimization problems have solutions, that a bounded sequence of designs has a limiting design, and that a continuous error stays uniformly small. This lesson sets up the vocabulary: open sets, closed sets, limit points and compactness.
:::

::: play
- The Cantor set: start with [0, 1], remove the open middle third, then the middle third of each piece, forever. Stage n has $2^n$ closed intervals, each $3^{-n}$ long.
- The stems give the total remaining length, $(2/3)^n$, which tends to 0. Yet the set left at the end is closed, bounded, and so compact, and it contains uncountably many points.
- Each stage removes open intervals, so what's left is always closed: a closed set's complement is open.
:::
:::

```sim plot
view: graph
x: n = 0..20
y: L = 0..1
plot: (2/3)^n | stems | label: total length left, (2/3)ⁿ
readout: intervals at stage 10 = 2^10
readout: length of each at stage 10 = 3^(-10)
```

::: predict
## Which of these sets is compact?

- [ ] (0, 1]
- [ ] [0, ∞)
- [x] $\{0\} \cup \{\frac1n : n \ge 1\}$
- [ ] $\{\frac1n : n \ge 1\}$

::: reveal
$\{0\} \cup \{\frac1n\}$: it's bounded, and closed because its only limit point, 0, is included. Leave out 0 and the sequence $\frac1n$ converges to a point outside the set, so it isn't closed. (0, 1] has the same problem at 0, and [0, ∞) is unbounded.
:::
:::

::: nameit
## Open and closed

- A set U is **open** if every point x in it has a neighborhood $(x - r, x + r)$ inside U.
- A point x is a **limit point** of S if every neighborhood of x contains a point of S other than x.
- A set F is **closed** if it contains all its limit points; equivalently, its complement is open; equivalently, whenever a sequence in F converges, its limit is in F.
- The **closure** $\bar S$ is S together with its limit points.

Unions of any number of open sets, and intersections of finitely many, are open. Intersections of any number of closed sets, and unions of finitely many, are closed. ℝ and ∅ are both open and closed; [0, 1) is neither.

## Compactness

For a set $K \subseteq \mathbb R$, these are equivalent:

1. K is closed and bounded.
2. Every sequence in K has a subsequence converging to a point of K.
3. Every cover of K by open sets has a finite subcover.

(The equivalence of 1 and 3 is the **Heine–Borel theorem**.) In infinite-dimensional spaces of functions, closed and bounded is no longer enough, which matters in lesson 32 onward.

## Why it matters

On a compact set a continuous function attains its maximum and minimum, and is uniformly continuous (lesson 13). Sequences in it can't escape to infinity or to a missing edge point.
:::

::: example
## Show that the open cover $\{(\frac1n, 2) : n \ge 1\}$ of (0, 1] has no finite subcover.

::: solution
1. **Take any finite subcollection.** Any finite collection $(\frac1{n_1}, 2), \ldots, (\frac1{n_k}, 2)$ is contained in $(\frac1N, 2)$, with $N = \max n_i$.
2. **Find a point it misses.** That interval misses the point $\frac{1}{2N}$ of (0, 1], so no finite subcollection covers (0, 1].
3. **Conclude.** So (0, 1] isn't compact.
4. **What 0 would do.** Adding the point 0 would break the cover, since no $(\frac1n, 2)$ contains 0.

**Answer:** every finite subcollection misses a point $\frac{1}{2N}$, so there's no finite subcover and (0, 1] isn't compact.
:::
:::

::: example
## For x = 0.3 in the open interval (0, 1), what's the largest radius r with $(x - r, x + r) \subseteq (0, 1)$?

::: solution
1. **Distance to the nearer end.**
   $$\begin{aligned} r &= \min(0.3, 0.7) \\ &= 0.3 \end{aligned}$$
2. **What it shows.** Every point of an open interval has such a positive r, which is what makes the interval open; at the point 0 of [0, 1) there's none.

**Answer:** r = 0.3, the distance to the nearer end.
:::
:::

::: example
## Find the interior, closure and boundary of $A = [0, 1) \cup \{2\}$.

::: solution
1. **Interior.** These are the points with a whole neighborhood inside A. That's (0, 1): 0 fails because points just left of it are outside, and 2 is isolated.
2. **Closure.** A together with its limit points is $[0, 1] \cup \{2\}$; the point 1 is added, since $1 - \frac1n \in A$ converges to it.
3. **Boundary.** Closure minus interior is $\{0, 1, 2\}$.
4. **Neither open nor closed.** A isn't open (0 isn't interior) and isn't closed (it misses its limit point 1).
5. **The closure is compact.** It's closed and bounded, hence compact, so a continuous function on the closure attains its maximum, which A alone doesn't guarantee.

**Answer:** the interior is (0, 1), the closure is $[0, 1] \cup \{2\}$, and the boundary is $\{0, 1, 2\}$.
:::
:::

::: check
given: n = 3 | 8 | 15
answer: (2/3)^n
tolerance: 0.5%

## How much total length is left after {n} stages of the Cantor construction?

::: right
Right. $(2/3)^n$ = {answer}.
:::

::: misses
- [(1/3)^n] Each stage keeps two thirds, not one third.
- [any] Not yet. Each stage keeps $\frac23$ of what's left.
:::
:::

::: check
given: n = 4 | 10 | 20
answer: 2^n
tolerance: 0

## How many intervals make up stage {n} of the Cantor construction?

::: right
Right. $2^n$ = {answer}, each of length $3^{-n}$.
:::

::: misses
- [3^n] Each interval splits into 2 kept pieces, not 3.
- [any] Not yet. Every interval is replaced by two.
:::
:::

::: check
given: a = 0 | 0.5; b = 2 | 5; x = 0.7 | 1.8
answer: (b - a - abs(2*x - a - b))/2
tolerance: 0.1%

## For x = {x} in the open interval ({a}, {b}), what's the largest r with $(x - r, x + r)$ inside it?

::: right
Right. The distance to the nearer end: {answer}.
:::

::: misses
- [(b - a)/2] That's half the width; the distance is to the nearer end.
- [any] Not yet. $\min(x - a, b - x)$.
:::
:::

::: check
kind: match

## Match each set with its description.

- the interval (0, 1) :: open, not closed
- the interval [0, 1] :: closed and bounded, so compact
- the interval [0, 1) :: neither open nor closed
- the half-line [0, ∞) :: closed, not bounded
- the whole line ℝ :: both open and closed

::: right
Right. Openness and closedness aren't opposites: a set can be both, or neither.
:::
:::

::: check
kind: choice

## Which statement about the set $\{\frac1n : n \ge 1\}$ is true?

- [ ] It's closed
- [x] Its closure is $\{\frac1n\} \cup \{0\}$
- [ ] It's open
- [ ] Every sequence in it converges within it

::: misses
- [1] It's missing its limit point 0.
- [3] Each point has neighborhoods containing points outside the set.
- [4] $\frac1n \to 0$, which isn't in the set.
:::
:::

::: check
kind: parts
given: r = 0.2 | 0.5; n = 5 | 10

## A Cantor-like set removes the middle fraction {r} of every remaining interval at each stage.

::: part
answer: (1 - r)^n
tolerance: 0.5%

### What total length is left after {n} stages?

::: misses
- [1 - r*n] The removals compound: each stage keeps a fraction $1 - r$.
- [any] $(1 - r)^n$.
:::
:::

::: part
answer: ln(2)/ln(2/(1 - r))
tolerance: 0.5%

### What is its fractal dimension, $\frac{\ln 2}{\ln(1/s)}$, where each piece is scaled by $s = \frac{1 - r}{2}$?

::: misses
- [any] Two pieces, each scaled by $\frac{1 - r}{2}$.
:::
:::
:::

::: check
kind: derivation
vars: r = 0.1..0.9, n = 1..20

## Each stage of a Cantor construction removes the middle fraction r of every interval.

::: step
answer: (1 - r)^n

### What length remains after n stages?
:::

::: step
answer: 1 - (1 - r)^n

### What total length has been removed?
:::

::: right
Right. As n grows the removed length approaches 1, yet uncountably many points remain.
:::
:::

::: explain
## Why is a closed and bounded set of real numbers the right setting for "a continuous function has a maximum"?

::: model
If the set is unbounded, the function can grow forever, like x on [0, ∞). If the set is missing an edge point, the function can approach a value it never reaches there, like $\frac1x$ near 0 on (0, 1], or like x on [0, 1), which gets arbitrarily close to 1 without reaching it. A closed and bounded set rules out both escapes: any sequence of points whose function values approach the supremum has a subsequence converging to a point of the set (bounded), that point is in the set (closed), and continuity carries the values along, so the supremum is actually attained there.
:::
:::

::: practice

::: check
given: n = 2..40 int
answer: (2/3)^n
tolerance: 0.5%

## Cantor length after {n} stages?

::: misses
- [any] $(2/3)^n$.
:::
:::

::: check
given: n = 2..15 int
answer: 3^(-n)
tolerance: 0.5%

## Length of each Cantor interval at stage {n}?

::: misses
- [any] $3^{-n}$.
:::
:::

::: check
given: a = -1 | 2; b = 4 | 9; x = 2.5 | 3.2
answer: (b - a - abs(2*x - a - b))/2
tolerance: 0.1%

## Largest radius around {x} inside ({a}, {b})?

::: misses
- [any] $\min(x - a, b - x)$.
:::
:::
:::
