Discrete MathematicsLanguages, Automata and Computability1 of 6
Languages, Automata and Computability · 1
Strings, Languages and Regular Expressions
We need a simple, exact way to describe which sequences of symbols follow a rule, even when there are infinitely many valid ones.
1
Alphabets, Strings and the Empty String
Before we can state a rule for valid sequences, we have to fix the set of symbols we are allowed to use and see how those symbols line up into strings.
Alphabets, Strings and the Empty String
We write a string by placing symbols from the alphabet side by side in order, and its length is the number of symbols in it:
alphabetΣ={0,1},string of length ∣w∣=4w=0100,empty string, ∣ε∣=0ε
The symbol ε (you might also see it written as λ) is not a character inside Σ; it is our name for the string with zero symbols. Because ε has no symbols, gluing it onto any string by concatenation leaves that string unchanged:
01⋅11101⋅ε=01111,=01
An exponent on a string tells us how many copies of that string to glue together, not to multiply numbers:
03020100=000,=00,=0,=ε
2
Languages and the Empty Set Trap
Once we can form strings from an alphabet Σ, we collect every possible finite string into an infinite set called Σ∗. A language is simply any subset of strings we choose from Σ∗.
Languages and the Empty Set Trap
Listing Σ∗ by length shows how the empty string ε sits at length 0 before the strings of length 1,2, and so on:
To build large or infinite languages from small ones, we combine sets of strings using union, concatenation, and the star operation.
Operations on Languages
Text solution
Combine sets with union (+ or ∣)Collect every string that belongs to A={0,11} or to B={a,b} into a single language.
A∪B=A+B={0,11,a,b}
Pair strings in order for concatenation (AB)Pick one string from A followed by one string from B in every possible combination.
AB={0a,0b,11a,11b}
Make a fresh choice on each pick in A∗Take the union of picking 0,1,2,… strings from A={0,11}, where A2 chooses independently from A on each pick rather than repeating the same string twice.
Concatenate and star {ε} and ∅Gluing ε changes nothing, gluing from ∅ gives nothing because there is no string to pick from ∅, and picking 0 strings from ∅ gives ε.
L{ε}L∅∅∗=L,=∅,={ε}∅0∪∅∅1∪∅∅2∪⋯={ε}
See if you can use these rules to evaluate each language expression before flipping the card:
See how changing the strings in a set changes its products and powers, while pairing with the empty set always gives nothing and zero picks always leaves the empty string.
4
Regular Expressions and Order of Operations
Instead of writing out infinite sets of strings, we use a regular expression as a compact formula that builds a language from ∅,ε, and single symbols in Σ using +, concatenation, and ∗.
Regular Expressions and Order of Operations
Just like algebra, regular expressions drop extra parentheses by following a fixed order of operations from highest priority to lowest:
⟶⟶1. star (like an exponent)R∗2. glue (like multiplication)RS3. or (like addition)R+S
Text solution
Apply the star to the piece right next to it firstIn 0+10∗, the star only applies to the 0 right next to it, not 10.
0+1(0∗)
Concatenate before taking the unionGlue 1 to 0∗ before splitting at +, giving either a single 0 or a 1 followed by any number of 0s.
0+(1(0∗))⟶{0,1,10,100,1000,…}
Put + inside (0+1)∗ to make a fresh choice each timeIn 0∗+1∗, you choose 0∗ or 1∗ once at the start, whereas (0+1)∗ lets you pick a fresh 0 or 1 on each repetition to build every binary string in Σ∗.
Check the order of operations and where the star sits on the card below:
A star only repeats what sits right to its left, so moving parentheses changes whether a string can repeat 0, stick to one symbol, or switch between 0 and 1 on every lap.
5
Writing Regular Expressions with Slots and Blanks
To turn a rule over Σ={0,1} into a regular expression, we put the required symbols into fixed slots first and then fill the blanks around them with whatever symbols are still allowed.
Writing Regular Expressions with Slots and Blanks
Text solution
Fix the ends and put the wildcard in the middleFor strings starting with 0 and ending with 11, put 0 at the front and 11 at the back with any binary string in between; when the front and back symbols match, as in starting and ending with 0, add the length-1 string 0 with +.
starts with 00any middle(0+1)∗ends with 1111,length ≥20(0+1)∗0+length 10
Surround a required substring with wildcardsFor strings containing 00, write 00 once and allow any binary string before and after it.
before(0+1)∗00after(0+1)∗
Fill blanks with 0∗ when 1s are countedFor strings with exactly two 1s, write the two 1s first and allow only 0∗ in the three surrounding blanks so the blanks never add a third 1.
only 0s0∗1only 0s0∗1only 0s0∗
Once you write an expression with required start and end slots, always check the shortest strings of length 0 and 1(ε,0, and 1), since two separate end slots force a string to have length at least 2.
6
Parity and Forbidden Substrings
Fixed slots work when we know the exact count of a symbol, but when a rule asks for an even count or forbids a substring like 00, we build the string out of repeatable blocks that keep the rule true on every repetition.
Parity and Forbidden Substrings
Text solution
Repeat a two-symbol block for even or odd lengthGlue two (0+1) slots into a length-2 block and star it for even length, or glue one extra (0+1) slot onto the end for odd length.
Put 0∗ in front of a pair-of-1s loopFor an even number of 1s, start with 0∗ to cover strings with zero 1s (such as 000) before repeating the two-1 block 10∗10∗.
zero 1s allowed0∗adds 1s in pairs(10∗10∗)∗
Pair each restricted symbol with the symbol that must follow itTo forbid 00, require every 0 before the end of the string to be followed right away by 1 as the block 01, and allow at most one 0 at the very end.
safe blocks(1+01)∗optional final 0(ε+0)
Test this safe-block idea on the card below:
Every 0 before the end pairs with the 1 to its right, so two 0s in a row leave the first 0 without a 1.
Key concepts
In language operations, keep the empty language ∅(0 strings) separate from {ε}(1 string of length 0): concatenating with {ε} changes nothing (L{ε}=L), concatenating with ∅ gives the empty set (L∅=∅), and starring the empty set gives ∅∗={ε} because there is one way to pick 0 strings.
When reading or writing regular expressions over Σ={0,1}, star goes first, then concatenation, then +, so 0+10∗ means 0+(1(0∗)). Putting the star outside parentheses in (0+1)∗ gives a fresh choice on every repetition to build all of Σ∗, whereas 0∗+1∗ chooses once at the start and only builds strings of all 0s or all 1s. Match the rule to the way you build the string:
Required start, end, or substring (no count limit)
Put the required pieces in place and fill the blanks with the wildcard (0+1)∗, as in (0+1)∗00(0+1)∗, adding short length-1 strings separately as in 0(0+1)∗0+0.
Fixed count of 1s (do not use the wildcard (0+1)∗ in blanks)
Place the required 1s in slots and fill the blanks with 0∗ only, as in 0∗10∗10∗ for exactly two 1s.
Even or odd count
Repeat a block that adds symbols or 1s in pairs: ((0+1)(0+1))∗ for even length, and 0∗(10∗10∗)∗ with a leading 0∗ for an even number of 1s.
Forbidden substring
Pair the restricted symbol with the symbol that must immediately follow it and handle the end of the string with an optional ending, as in (1+01)∗(ε+0) for no 00.
7
Practice
1
Let Σ={0,1},u=10, and v=011. Find the strings uv,u3, and v0εu, and give the length of each.
What string do you get when any string is raised to the 0th power, and how many symbols does ε add when glued onto u?
Gluing u and v gives uv=10011 with ∣uv∣=5, and gluing three copies of u gives u3=101010 with ∣u3∣=6. For v0εu, do not treat v0 or ε as symbols of length 1 (which would give length 4) or write the symbol 0 for v0 (which would give 010 of length 3); both v0=ε and ε are the empty string of length 0, so v0εu=ε⋅ε⋅10=10 with ∣v0εu∣=2.
2
Let L={0,11}. List the elements of each language and state how many strings it contains: (a) L{ε}, (b) L∅, (c) (L∅)∗, (d) L∪∅∗.
Which of ∅ and {ε} acts like multiplying by 0 in concatenation, and what does picking 0 strings from an empty set give?
In (a), gluing ε onto each string in L leaves it unchanged, and in (b), no pair can be formed with ∅. In (c) and (d), watch out for the mistake of writing ∅∗=∅ (which would give 0 strings for (c) and 2 strings for (d)): picking 0 strings from ∅ gives ε, so ∅∗={ε}.
Let A={ε,0} and B={1,01}. List all strings in AB and all strings in B2, and state how many strings are in each language.
For AB, the four pairs are ε⋅1=1,ε⋅01=01,0⋅1=01, and 0⋅01=001; do not count the duplicate 01 twice to get 4 strings.
Write out all 2×2=4 glued pairs for each operation, then check whether two different pairs give the exact same string.
For AB, the four pairs are ε⋅1=1,ε⋅01=01,0⋅1=01, and 0⋅01=001; do not count the duplicate 01 twice to get 4 strings. For B2, do not just repeat the same string twice to get {11,0101}(2 strings), because each pick from B is a fresh choice.
Add parentheses to the regular expression 01∗+10∗ to show the order of operations, and determine which of the strings ε,0,1,10,011, and 0110 belong to its language.
Does each star apply to the two-symbol string to its left, or only to the single symbol right next to it?
Do not group the stars around two symbols as (01)∗+(10)∗ (which would wrongly include ε and leave out 0,1, and 011), and remember that + picks one side for the entire string so 0110 cannot use both sides. Each star applies only to the symbol right next to it, then concatenation, then +:
(0(1∗))+(1(0∗)), and the strings from the list in the language are 0,1,10, and 011.
5
Over Σ={0,1}, write a regular expression for all strings that start and end with the same symbol.
Split the rule into two cases with + (starts and ends with 0, or starts and ends with 1), and check whether strings of length 1 are included.
Writing only 0(0+1)∗0+1(0+1)∗1 forces every string to have at least two symbols and misses the length-1 strings 0 and 1, each of which starts and ends with the same symbol. Including the length-1 strings gives:
0(0+1)∗0+1(0+1)∗1+0+1.
6
Over Σ={0,1}, write a regular expression for all strings that contain at most two 1s.
"At most two 1s" means zero 1s, one 1, or two 1s. What is the only symbol allowed in the blanks around those 1s?
Do not fill the blanks with the wildcard (0+1)∗, as in (0+1)∗1(0+1)∗1(0+1)∗, which allows extra 1s in the blanks and describes strings with at least two 1s. Allowing only 0∗ in every blank across the cases of zero, one, or two 1s gives:
0∗+0∗10∗+0∗10∗10∗ (or 0∗(ε+1)0∗(ε+1)0∗).
7
Over Σ={0,1}, write a regular expression for all strings that have an odd number of 1s.
How do you combine our expression for an even number of 1s with one required 1?
An odd number of 1s is one required 1 followed by an even number of additional 1s; be careful not to write 0∗1(10∗10∗)∗ without a 0∗ after the first 1, which would miss strings like 100 where 0s follow the single 1. Putting 0∗ on both sides of the first 1 before the pair loop gives:
0∗10∗(10∗10∗)∗ (or (0∗10∗1)∗0∗10∗).
8
Over Σ={0,1}, write a regular expression for all strings that do not contain 111 (no three 1s in a row).
Before the end of the string, any 1 or 11 must be followed right away by a 0. What are the three safe blocks that end in 0, and what can sit at the very end?
Every 1 or 11 before the end of the string must be followed by 0, giving the repeatable blocks 0,10, and 110. Do not stop at (0+10+110)∗, which forces every non-empty string to end in 0 and misses valid strings ending in 1 or 11. Allowing up to two 1s at the end gives: