Foundations of Computing – Discrete Mathematics Solutions to

advertisement

Foundations of Computing –

Discrete Mathematics

Solutions to exercises for week 9

Agata Murawska (agmu@itu.dk)

November 10, 2013

Exercise (13.2.2) .

Give the state tables for the finite-state machines with these state diagrams.

a) f g

State 0 1 0 1 s

0 s

1 s

2 s

3 s

1 s

0 s s

2

3

1

1

0

0 s

3 s

0

0 0 s

1 s

2

1 1 b) f g

State 0 1 0 1 s

0 s

1 s

2 s

1 s

2 s

2 s

2 s

1 s

0

0

0

1

1

0

0 c) f g

State 0 1 0 1 s

0 s

1 s

2 s

3 s

3 s

1 s

1

0 1 s

0 s

1

0 1 s

3 s

1

0 1 s

3

0 0

Exercise (13.2.4) .

Find the output generated from the input string 10001 for the finite-state machine with the state diagram in a) Exercise 2 a)

We start in s

0 with 1, that moves us to s

2 and produces 0 as an output.

1

In s

2

In s

3 we have input 0, that means we move to again we have 0, so we move to have input 0, moving to s

0 s

1 s

3 and produce 0 as output.

with 1 on the output. In and 1 on the output. Finally in s

0 s

1 we we have 1, so we move to s

2 and produce 0. The output string was therefore 00110

(and we finish in state s

2 after consuming this input).

b) Exercise 2 b)

The output is 11110.

c) Exercise 2 c)

The output is 10001.

Exercise (13.2.18) .

Construct a finite-state machine that determines whether the input string read so far ends in at least five consecutive 1s.

The solution will be similar to the one for example 7. We will have 5 states: s

0 s

1 s

2

– end of the input string contains no 1s

– there is exactly one 1 at the end of the input string

– there are exactly two 1s at the end of the input string s

3 s

4

– there are exactly three 1s at the end of the input string

– there are at least four 1s at the end of the input string

We will print 1 on the tape as long, as the input string read so far ends in at least five consecutive 1s – this happens only when we read an input of 1 while being in the state s

4

. Therefore, function g can be defined as: g( s

4

, 1) = 1, otherwise g( s i

, b ) = 0.

Next, function f describes the change of state: when we have 1 as the input we increase the state number by 1 or stay in state s

4

(so, e.g. f( s

1

, 1) = s

2

, f( s

4

, 1) = s

4

), when we have 0 as input we reset the counter of 1s: f( s i

, 0) = s

0

. This makes the transition function consistent with our description of the states.

Finally, M = ( { s

0

, s

1

, s

2

, s

3

, s

4

} , { 0 , 1 } , { 0 , 1 } , f, g, s

0

).

0, 0

0, 0

1, 0

0, 0 s

1

1, 0 1, 0 1, 0 start s

0 s

2 s

3

0, 0

0, 0

Correctness of the construction can be argued by first noting that our informal description of 5 states actually holds. This will give us knowledge that in particular the machine is in the state s

4 if and only if the input string

2 s

4

1, 1

ends with 1111. Finally, we observe that only in this state and upon seeing

1 on the input tape, we produce 1 as an output.

Exercise (13.2.20) .

Construct the state diagram for the Moore machine with this state table.

0 start s

0

; 0

1

1

1

1 s

2

; 1

0 s s

1

3

; 1

0

; 1

0

Exercise (13.2.24) .

Construct a Moore machine that gives an output of 1 whenever the number of symbols in the input string read so far is divisible by 4 and an output of 0 otherwise.

The machine will have 4 states (with outputs): s

0

, 1 – initial state; the string’s length is divisible by 4 s

1

, 0 – state where string’s length has a remainder 1 modulo 4 s

2

, 0 – state where string’s length has a remainder 2 modulo 4 s

3

, 0 – state where string’s length has a remainder 3 modulo 4

Function f is the defined as f( s i

, b ) = s

(( i +1) mod 4)

.

Function g is defined as g( s

0

) = 1, otherwise g( s i

) = 0.

Finally, M = ( { s

0

, s

1

, s

2

, s

3

} , { 0 , 1 } , { 0 , 1 } , f, g, s

0

).

1 1 start s

0

, 1 s

1

, 0 s

2

, 0

0 0

0 1

0

1 s

3

, 0

3

Correctness of construction can be argued again by noting that our informal description of the states is accurate.

Exercise (13.3.10) .

Determine whether the string 01001 is in each of these sets.

a) { 0 , 1 } ∗

– yes, this set contains all possible bit strings.

b) { 0 } ∗ { 10 }{ 1 } ∗

– no, we can produce a prefix 010, but the remaining 01 is impossible to generate.

c) { 010 } ∗ { 0 } ∗ { 1 } – yes, we take { 010 } ∗ and { 1 } to generate 1.

to produce 010, { 0 } ∗ to produce 0 d) { 010 , 011 }{ 00 , 01 } – yes, we get 010 from { 010 , 011 } and 01 from { 00 , 01 } .

e) { 00 }{ 0 } ∗ { 01 } – no, we are forced to start with two 0s, and the string

01001 starts with 01.

f) { 01 } ∗ { 01 } ∗

– no, we can create a prefix 01, but later we need to create two 0s and { 01 } ∗ cannot generate that.

Exercise (13.3.20) .

Find the language recognized by the given deterministic finite-state automaton.

0

0, 1

0, 1 start s

0

0 s

1

1 s

2 s

3

1

We have two final states: s

1

State s

1 and s can be reached using { 1 } ∗

3

.

{ 0 } and we stay in s

1 as long as we have

0s as input. Once we are out of this state we cannot go back to it. Therefore language recognized by s

1

The input for reaching s

3 is { 1 is { 1 } ∗

} ∗

{

{ 0 }{ 0 } ∗

0 }{ 0 } ∗ {

.

1 }{ 0 , 1 } . When state s

3 is reached, we never come out of it.

Therefore language recognized by s

3 is { 1 } ∗ { 0 }{ 0 } ∗ { 1 }{ 0 , 1 }{ 0 , 1 } ∗

.

The language recognized by this automaton is a sum of the languages that are recognized by two accepting states

{ 1 } ∗ { 0 }{ 0 } ∗ ∪ { 1 } ∗ { 0 }{ 0 } ∗ { 1 }{ 0 , 1 }{ 0 , 1 } ∗

{ 1 n 00 m | n, m ∈

N

} ∪ { 1 n 00 m 1 w | n, m ∈

=

N

, w ∈ { 0 , 1 } + }

4

Exercise (13.3.32) .

Construct a deterministic finite-state automaton that recognizes the set of all bit string that contain an even number of 1s.

Since we care only about tracking one thing, evenness of the number of 1s in the string, it is enough to have only two states: s

0

– an accepting state, in which the string read as the input has an even number of 1s s

1

– string read as the input as an odd number of 1s

The transition function is easy to imagine: reading 0 from the input does not change the state, reading 1 flips it. That is, precisely: f( s i

, 0) = s i

, f( s

0

, 1) = s

1 and f( s

1

, 1) = s

0

.

0 0

1 start s

0 s

1

1

Exercise (13.3.44) .

Find the language recognized by the given nondeterministic finite-state automaton.

0, 1 start s

0 s

1

0 s

2

1

1

The accepting states are s

0 and s

1

. In s input string. There are 2 ways of reaching s

1

0 we can accept only the empty

– either through reading 0 or 1 in the state s

0

, or by reading 0 while being in the state s

2

. The latter allows us to produce arbitrarily long strings on 1s, followed by a single 0. Therefore the language recognized by this automaton is { λ, 0 , 1 } ∪ { 11 n 0 | n ∈

N

} .

5

Download