Counter Machines
David Galles
Department of Computer Science
University of San Francisco
14-0: Counter Machines
Give a Non-Deterministic Finite Automata a
counter
Increment the counter
Decrement the counter
Check to see if the counter is zero
14-1: Counter Machines
A Counter Machine M = (K, Σ, ∆, s, F )
K is a set of states
Σ is the input alphabet
s ∈ K is the start state
F ⊂ K are Final states
∆ ⊆ ((K × (Σ ∪ ǫ) × {zero, ¬zero}) × (K ×
{−1, 0, +1}))
Accept if you reach the end of the string, end in an
accept state, and have an empty counter.
14-2: Counter Machines
Give a Non-Deterministic Finite Automata a
counter
Increment the counter
Decrement the counter
Check to see if the counter is zero
Do we have more power than a standard NFA?
14-3: Counter Machines
Give a counter machine for the language an bn
14-4: Counter Machines
Give a counter machine for the language an bn
(a,zero,+1)
(a,~zero,+1) (b,~zero,−1)
(b,~zero,−1)
0 1
14-5: Counter Machines
Give a 2-counter machine for the language an bn cn
Straightforward extension – examine (and
change) two counters instead of one.
14-6: Counter Machines
Give a 2-counter machine for the language an bn cn
(a,zero,zero,+1,0)
(a,~zero,zero,+1,0) (b,~zero,~zero,-1,+1)
0 (b,~zero,zero,−1,+1) 1
(c,zero,~zero,0,-1)
2
(c,zero,~zero,0,-1)
14-13: Counter Machines
Give a Non-Deterministic Finite Automata two
counters
We can use two counters to simulate a stack
How?
HINT: We will simulate a stack that has two
symbols, 0 and 1
HINT2: Think binary
14-14: Counter Machines
We can use two counters to simulate a stack
One counter will store the contents of the stack
Other counter will be used as “scratch space”
Stack will be represented as a binary number, with
the top of the stack being the least significant bit
How can we push a 0?
How can we push a 1?
14-15: Counter Machines
How can we push a 0?
Multiply the counter by 2
How can we push a 1?
Multiply the counter by 2, and add 1
14-16: Counter Machines
How can we multiply a counter by 2, if all we can
do is increment
Remember, we have a “scratch counter”
14-17: Counter Machines
How can we multiply a counter by 2, if all we can
do is increment
Set the “Scratch Counter” to 0
While counter is not zero:
Decrement the counter
Increment the “Scratch Counter” twice
14-18: Counter Machines
To Push a 0:
While Counter1 6= 0
Increment Counter2
Increment Counter2
Decrement Counter1
Swap Counter1 and Counter2
14-19: Counter Machines
To Push a 1:
While Counter1 6= 0
Increment Counter2
Increment Counter2
Decrement Counter1
Increment Counter2
Swap Counter1 and Counter2
14-20: Counter Machines
To Pop:
While Counter1 6= 0
Decrement Counter1
If Counter1 = 0, popped result is 1
Decrement Counter1
If Counter1 = 0, popped result is 0
Increment Counter2
Swap Counter1 and Counter2
14-21: Counter Machines
How do we check if the simulated stack is empty?
We need to use 1 (not zero) to represent an
empty stack (why?)
Stack is empty if (counter1−1 = 0)
14-48: Counter Machines
Two stacks can simulate a Turing Machine:
Stack 1: Everything to the left of the read/write
head
Stack 2: Everything to the right of the read/write
head
Tape head points to top of Stack 1
Turing Machine
a b c d e f g ...
c d
b e
a f
g
Stack 1 Stack 2
14-49: Counter Machines
To write a new symbol at the Tape Head
Pop old value off the top of Stack 1
Push new value on the top of Stack 1
Turing Machine
a b c d e f g ...
c d
b e
a f
g
Stack 1 Stack 2
14-50: Counter Machines
To write a new symbol at the Tape Head
Pop old value off the top of Stack 1
Push new value on the top of Stack 1
Turing Machine
a b X d e f g ...
X d
b e
a f
g
Stack 1 Stack 2
14-51: Counter Machines
To move the tape head to the left
Pop symbol off Stack 1
Push it on Stack 2
Turing Machine
a b X d e f g ...
X d
b e
a f
g
Stack 1 Stack 2
14-52: Counter Machines
To move the tape head to the left
Pop symbol off Stack 1
Push it on Stack 2
Turing Machine
a b X d e f g ...
X
d
b e
a f
g
Stack 1 Stack 2
14-53: Counter Machines
To move the tape head to the right
Pop symbol off Stack 2
Push it on Stack 1
Turing Machine
a b X d e f g ...
X
d
b e
a f
g
Stack 1 Stack 2
14-54: Counter Machines
To move the tape head to the right
Pop symbol off Stack 2
Push it on Stack 1
Turing Machine
a b X d e f g ...
X d
b e
a f
g
Stack 1 Stack 2
14-55: Counter Machines
To move the tape head to the right, if Stack 2 is
empty ...
Turing Machine
a b c ...
c
b
a
Stack 1 Stack 2
14-56: Counter Machines
To move the tape head to the right, if Stack 2 is
empty ...
Push a Blank Symbol on Stack 1
Turing Machine
a b c ...
c
b
a
Stack 1 Stack 2
14-57: Counter Machines
To move the tape head to the right, if Stack 2 is
empty ...
Push a Blank Symbol on Stack 1
Turing Machine
a b c ...
c
b
a
Stack 1 Stack 2
14-58: Counter Machines
Four Counters ⇒ Two Stacks ⇒ Turing Machine
If we can simulate a 4-counter machine with a
2-counter machine ...
Two Counters ⇒ Four Counters ⇒ Two Stacks ⇒
Turing Machine
14-59: 2 Counter ⇒ 4 Counter
We can represent 4 counters using just one
counter
Counters have values i, j, k, l
Master Counter: 2i 3j 5k 7l
When all counters have value 0, master counter
has value 1
14-60: 2 Counter ⇒ 4 Counter
Master Counter: 2i 3j 5k 7l
To increment counter j , multiply Master
Counter by 3
Decrement Master Counter
Increment Scratch Counter 3 times
Repeat until Master Counter = 0
Move Scratch Counter to Master Counter
14-61: 2 Counter ⇒ 4 Counter
Master Counter: 2i 3j 5k 7l
To decrement counter j , divide Master Counter
by 3
Decrement Master Counter 3 times
Increment Scratch Counter
Repeat until Master Counter = 0
Copy Scratch Counter to Master Counter
14-62: 2 Counter ⇒ 4 Counter
Master Counter: 2i 3j 5k 7l
To check of counter j is zero, see if MC mod 3
=0
Decrement Master Counter 3 times (if we hit
zero in the middle of this operation, MC mod
3 6= 0, if he hit zero at the end, MC mod 3 =
0)
Increment Scratch Counter 3 times
Repeat until Master Counter = 0
Use Scratch Counter to restore Master
Counter