Alphabet and Strings
COS3701 - Theoretical Computer Science III · Formal Languages
Alphabet and Strings
In theoretical computer science, an alphabet is a finite set of symbols. These symbols are the basic building blocks used to create strings. A string is a finite sequence of symbols from an alphabet. For example, if we have an alphabet Σ = {a, b}, then some possible strings are 'a', 'b', 'ab', 'ba', and 'aa'.
Defining an Alphabet
Let us define an alphabet formally. An alphabet is denoted by the symbol Σ (the Greek letter sigma). For example, consider the following alphabet:
Σ = {0, 1}This alphabet contains two symbols, 0 and 1. You can create strings from this alphabet. The empty string, denoted by ε, is also considered a string.
Remember: The empty string ε has a length of 0.
Creating Strings
A string is formed by concatenating symbols from the alphabet. The concatenation operation combines two strings to form a new string. For example, if you concatenate the strings '0' and '1', you get '01'.
Example of String Creation
Let’s use the alphabet Σ = {a, b}. The following are examples of strings that can be formed:
- Single symbols: 'a', 'b'
- Two symbols: 'aa', 'ab', 'ba', 'bb'
- Three symbols: 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb'
The number of possible strings increases as you add more symbols or increase the length of the strings.
Length of a String
The length of a string is the number of symbols it contains. The length is denoted by the notation |s|, where s is the string. For example:
Let s = 'ababa'Then the length of the string is:
|s| = 5Operations on Strings
There are several operations you can perform on strings:
- Concatenation: Combining two strings to form a new string.
- Reversal: The reverse of a string s, denoted by sR, is the string formed by reversing the order of symbols in s. For example, if s = 'abc', then sR = 'cba'.
- Substring: A substring is a sequence of consecutive symbols within a string. For example, in the string 'abcde', 'bc' is a substring.
Example of String Operations
Let s = 'hello' and t = 'world'.
- Concatenation: s + t = 'helloworld'
- Reversal: sR = 'olleh'
- Substring: 'lo' is a substring of 'hello'
Watch out: When performing concatenation, ensure you do not confuse the order of strings. The operation is not commutative, meaning that s + t is not the same as t + s.
Finite and Infinite Strings
Strings can be classified as finite or infinite. A finite string has a limited length, while an infinite string continues indefinitely. In formal languages, we primarily work with finite strings. Infinite strings are theoretical and are not typically represented in practical applications.
Languages and Strings
A language is a set of strings formed from an alphabet. For example, if we define a language L over the alphabet Σ = {0, 1} as:
L = {0, 1, 01, 10, 001}This language consists of five strings. Each string in the language is a valid combination of symbols from the alphabet.
Formal Language Definition
A formal language is defined as a set of strings over a given alphabet. The strings in a formal language can be finite or infinite, but the language itself must be well-defined. The rules that govern the formation of strings in a language are typically specified by a grammar, which will be discussed in the next topic.
Example of a Language
Consider the alphabet Σ = {a, b}. We can define a language L as follows:
L = {a, b, ab, aab, abb, ba, bba}This language includes all the strings formed by combinations of the symbols 'a' and 'b'.
Tip: When defining a language, ensure that it is closed under concatenation. This means that if you take two strings from the language and concatenate them, the resulting string should also belong to the language.
Chomsky Hierarchy
The Chomsky hierarchy classifies formal languages into four types based on their generative power:
- Type 0: Recursively enumerable languages
- Type 1: Context-sensitive languages
- Type 2: Context-free languages
- Type 3: Regular languages
In the context of this topic, we will focus on Type 3 languages, which are generated by regular grammars and can be represented by finite automata.
Conclusion
Understanding alphabets and strings is fundamental to the study of formal languages. Strings are the building blocks of languages, and the operations on strings form the basis for more complex structures and grammars.
Summary
- An alphabet is a finite set of symbols.
- A string is a finite sequence of symbols from an alphabet.
- The length of a string is the number of symbols it contains.
- A language is a set of strings formed from an alphabet.
- The Chomsky hierarchy classifies formal languages based on their generative power.
Check your understanding
- What is the definition of an alphabet?
- How do you calculate the length of a string?
- Provide an example of a language using the alphabet {x, y}.
- What is the difference between finite and infinite strings?