### CS30 - Spring 2016 - Class 24

#### Lecture notes

- assignment 10
- remember, will use many different starting piles (all with 4 piles, though)

- NIM tournament in class next Tuesday

• number representation
- when you type:

>> x = 17

how does the computer actually store this information, specifically the number 17?

- if you look far enough down into the details of what's going on when you run your computer, almost everything is represented using binary, i.e. some combination of 1's and 0's
- why binary?
- a combination of ease of use and cost
- basic idea: "1" electrical current is flowing, "0" current is not flowing
- transistors can do built to do this efficiently, cheaply and at a extremely small scale
- could do other representations, but this is what is most effective right now... (and has been for a long time)

• decimal numbers
- most commonly used number systems have a "base"
- each digit in the number can range from 0...base-1
- each digit in the number has an index, starting at the right-most digit with 0 and working up to the left
- the contribution of the digit is:

base^index * value

- by summing up the contribution of all of the digits, we get the value of the number

- what is the base for our numbering system?
- 10

- for example
- 245 = 5*10^0 + 4*10^1 + 2*10^2
- 80498 = 8*10^0 + 9*10^1 + 4*10^2 + 0*10^3 + 8*10^4

• binary numbers
- we can represent numbers using any base we want
- binary numbers are numbers represented as base 2
- digits can only be a 0 or a 1

- for example, the following binary numbers:
- 101 = 1*2^0 + 0*2^2 + 1*2^2 = 5
- 11 = 1*2^0 + 1*2^1 = 3
- 10111 = 1*2^0 + 1*2^1 + 1*2^2 + 0*2^3 + 1*2^4 = 23
- 100001 = 1*2^0 + 1*2^5 = 33

• some number examples (DFA):
- greater_5.jff: all numbers 5 or greater (represented as binary)

- odd_number.jff: all odd numbers (represented as binary)
- (O|1)*1

• non-deterministic finite automata (NFA)
- almost identical definition to DFA except:
- for a given state and input, can go to two or more states (rather than just a single one for DFAs)

- do not require that there is a transition for every alphabet letter for every state
- if you encounter a state without a transition for a particular letter, it does not accept that path

- can have epsilon (or sometimes called lambda) transitions from one state to another
- doesn't read anything from the input, just transitions

- an NFA accepts if *some* path exists (i.e. some choice of transitions) for the input that ends in a final state

- tend to be a bit easier to work with than DFAs

- NFA's do not actually have any more expressive power
- in particular, for any NFA, there is a corresponding DFA that accepts the same language (i.e. same set of strings)

• some NFA examples (found in NFA examples)
- start_end_a (1): start and end with a
- a(a|b)*a

- 2_or_3_a (2): strings of a's that have lengths divisible by 2 OR 3
- (aa)*|(aaa)*

- end_aa (3): ends in two a's
- (a|b)*aa

- aa_bb (4): has either aa or bb as a substring
- (a|b)*(aa|bb)(a|b)*

• regular language
- any language that can be described by a DFA (or an NFA)
- any language that can be described by a regular expression!

• play with JFLAP

• design a DFA that accepts 0^n1^n
- First hint that this is a problem is that we can't represent this as a regular expression
- seems like we need to count the number of zeros
- problem is we could have any number of 0s

• turing machines
- similar to DFAs
- they have states with transitions defined over the alphabet
- they have a starting state and one or more final/accepting states
- key differences:
- Can *write* to the input tape as well as read
- Can move right *and* left on the input tape
- The input tape is infinite!

• look at the 0n1n example (found in Turing examples)
- transitions are labeled with three things:
1) the input character read
2) the output character to write
3) which way to move the head (L, R or S)

- basic idea of this example:
- put Xs over the 0s. For each X you put on a 0, go find the next 1 and put a Y over it
- if you get to the point where everything is X and Y (and no 0s or 1s) you're done!

- slightly more detailed
- read a 0: put an X down and move right
- keep reading 0s and Ys to the right until you find a 1
- put a Y on that 1 and move left
- keep reading Ys and 0s until you find an X
- move right
- repeat
- if you get to a point where the first character is a Y
- read all the Ys and move right
- make sure that you get a blank (and not a 1 or a 0). If so, we're done!

• look at eq1s0s (found in turing_examples)

• look at palindrome (found in turing_examples)