CS 70
Discrete Mathematics for CS
Spring 2008
David Wagner
Note 5
Invariants
We have a robot that lives on an infinite grid. Initially, it is at position
(
0
,
0
)
. At any point, it can take a
single step in one of four directions: northeast, northwest, southwest, or southeast. In other words, if it is at
position
(
x
,
y
)
, it can move to one of the four positions
(
x
+
1
,
y
+
1
)
,
(
x
−
1
,
y
+
1
)
,
(
x
−
1
,
y
−
1
)
,
(
x
+
1
,
y
−
1
)
in a single step. Can the robot ever reach position
(
1
,
0
)
?
A bit of experimentation will suggest that the robot can never reach position
(
1
,
0
)
, no matter how many
steps it takes. Let’s prove this.
Theorem:
If the robot can reach position
(
x
,
y
)
, then
x
+
y
is even.
Proof
: We will prove this by induction on the number
n
of steps the robot has taken. Let
P
(
n
)
denote the
proposition that after any sequence of
n
steps, the robot can only reach positions of the form
(
x
,
y
)
such that
x
+
y
is even.
Base case:
After 0 steps, the robot can only be at the position
(
0
,
0
)
, and 0
+
0 is indeed even. Therefore
P
(
0
)
is true.
Inductive hypothesis:
Suppose that
P
(
n
)
is true. In other words, after any sequence of
n
steps, the robot can
only reach positions
(
x
,
y
)
where
x
+
y
is even.
Induction step:
Consider any sequence of
n
+
1 steps. Let
(
x
,
y
)
denote the position of the robot after the
first
n
of these steps, and
(
x
′
,
y
′
)
denote its position after the
n
+
1
st
step. By the induction hypothesis,
x
+
y
is even. Now there are four cases, corresponding to which direction the robot moved in the
n
+
1
st
step:
1.
(
x
′
,
y
′
) = (
x
+
1
,
y
+
1
)
: then
x
′
+
y
′
=
x
+
y
+
2, which is the sum of two even numbers (namely,
x
+
y
and 2) and hence is even.
2.
(
x
′
,
y
′
) = (
x
−
1
,
y
+
1
)
: then
x
′
+
y
′
=
x
+
y
, which is even.
3.
(
x
′
,
y
′
) = (
x
−
1
,
y
−
1
)
: then
x
′
+
y
′
=
x
+
y
−
2, which is even.
4.
(
x
′
,
y
′
) = (
x
+
1
,
y
−
1
)
: then
x
′
+
y
′
=
x
+
y
, which is even.
Consequently, after any sequence of
n
+
1 steps, the robot is at some position
(
x
′
,
y
′
)
such that
x
′
+
y
′
is even.
In other words, we have proven that
P
(
n
+
1
)
follows from
P
(
n
)
. By induction,
P
(
n
)
is true for all
n
∈
N
.
a
Corollary:
The robot can never reach position
(
1
,
0
)
.
The key idea of this proof was to identify an